For the life of me I can’t see why my coding does not work. The following coding does give me a message if the file is open in notepad, but it doesn’t if the file is open in word or excel?
Dim apps = 0
Dim Process() As Process = System.Diagnostics.Process.GetProcesses
For Each p As Process In Process
If p.MainWindowTitle.ToString.Contains("test") Then
If p.ProcessName = "notepad" Then
MsgBox("test file is open in notepad")
apps += 1
ElseIf p.ProcessName = "winword" Then
MsgBox("test file is open in word")
apps += 1
ElseIf p.ProcessName = "excel" Then
MsgBox("test file is open in excel")
apps += 1
End If
End If
Next
If apps = 0 Then
'run my code
End If
It just doesn’t seem to check word and excel but both the following snippets of coding work?
Dim Process2() As Process = System.Diagnostics.Process.GetProcessesByName("winword")
For Each p As Process In Process2
If p.MainWindowTitle.Contains("test") Then
MsgBox("test file is open in word")
End If
Next
and
Dim Process2() As Process = System.Diagnostics.Process.GetProcessesByName("excel")
For Each p As Process In Process2
If p.MainWindowTitle.Contains("test") Then
MsgBox("test file is open in excel")
End If
Next
Because the p.ProcessName is “WINWORD” ->UpperCase
you test for “winword”, ->lowercase.
change your tests to
to ignore the case