我终于找到了。
视窗 API....
Private Delegate Function EnumWindowsProc(ByVal hwnd As IntPtr, ByVal lParam As IntPtr) As Boolean
Private Declare Function EnumChildWindows Lib "user32.dll" (ByVal hWndParent As IntPtr, ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As IntPtr) As Boolean
Private Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hwnd As IntPtr, ByVal lpClassName As String, ByVal nMaxCount As Int32) As Int32
Private Const MAX_TITLE As Int32 = 256
Private Const S_OK As Int32 = &H0
Private Function GetClassName(ByVal hwnd As IntPtr) As String
Dim name As New String(" "c, MAX_TITLE)
Dim len = GetClassName(hwnd, name, MAX_TITLE)
If len = 0 Then Return Nothing
Return name.Remove(len)
End Function
Private Declare Function AccessibleObjectFromWindow Lib "OLEACC.dll" (ByVal hwnd As IntPtr, ByVal dwId As Int32, ByVal riid As Byte(), ByRef ppvObject As IntPtr) As Integer
Private Const OBJID_NATIVEOM As Int32 = &HFFFFFFF0
Private IID_IDispatch As New Guid("{00020400-0000-0000-C000-000000000046}")
功能...
Function EnumCWindows(ByVal hwnd As IntPtr, ByVal lParam As IntPtr) As Boolean
Dim name = GetClassName(hwnd)
If name = "EXCEL7" OrElse name = "EXCEL10" Then
Dim ptr As IntPtr
If AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), ptr) = S_OK Then
Dim win = CType(Marshal.GetObjectForIUnknown(ptr), Excel.Window)
Dim sheet = CType(win.ActiveSheet, Excel.Worksheet)
Dim book = CType(sheet.Parent, Excel.Workbook)
If book.FullName.ToLower.Contains("wpp tools") Then
FoundTheFile = True
WPPFilePath = book.FullName
Exit Function
End If
End If
End If
Return True
End Function
然后在你的代码中......
For Each p In Process.GetProcesses
If p.ProcessName = "EXCEL" Then
EnumChildWindows(p.MainWindowHandle, AddressOf EnumCWindows, IntPtr.Zero)
If FoundTheFile = True Then Exit For
End If
Next
If FoundTheFile = False Then
MsgBox("Targeted file was not found.", MsgBoxStyle.OkOnly, "File Find Error")
End
End If
我希望这可以帮助其他人,因为我已经搜索了一周!