删除运行代码时出现的不必要的窗口



问题是指longvh (Apr 25 at 4:27)对如何在不下载Selenium的情况下使用VBA自动化Edge浏览器的问题的回答。方法2似乎是最好的。方法可以工作,代码可以工作,但是出现了额外的和不必要的窗口:

  • identity_helper.exe(黑色窗口)。过了一会儿,它自己消失了。
  • msedge.exe(黑色窗口).

是否有可能使代码隐藏这些窗口?

提前感谢:PJ

这是一个丑陋的解决方案,但我已经"解决了";用我自己的方式:

Public Declare PtrSafe Function ShowWindow Lib "user32" (ByVal Hwnd As Long, ByVal nCmdShow As Long) As Long
Public Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassname As String, ByVal lpWindowName As String) As Long
Private Declare PtrSafe Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal Hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare PtrSafe Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal Hwnd As Long, ByVal lpClassname As String, ByVal nMaxCount As Long) As Long

Public Sub looking_for_windows()
Dim strComputer As String
strComputer = "."
Set objWMIService = GetObject("winmgmts:\" & strComputer & "rootCIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process", , 48)

'All open processes
For Each objItem In colItems

If objItem.ExecutablePath Like "*msedge.exe" Or objItem.Name Like "*msedge.exe" Then
Call hide_my_window(objItem.ExecutablePath)
End If

If objItem.ExecutablePath Like "*identity_helper.exe" Or objItem.Name Like "*identity_helper.exe" Then
Call hide_my_window(objItem.ExecutablePath)

'I can't kill it, so leave
'Call Shell("TaskKill /F /PID " & objItem.ProcessId, vbHide)
'Call Shell("TaskKill /IM ""identity_helper.exe"" /F", vbHide)
End If

Next
End Sub

Private Sub hide_my_window(ByVal Ret)
If IsNull(Ret) = False Then
Dim WinWnd As LongPtr, RetVal As Long, lpClassname As String
WinWnd = FindWindow(vbNullString, Ret)
ShowWindow WinWnd, SW_FORCEMINIMIZE
If Ret Like "*identity_helper.exe" Then
ShowWindow WinWnd, SW_HIDE
End If
End If

End Sub

(我也一直在评论中讨论:https://www.codeproject.com/Tips/5307593/Automate-Chrome-Edge-using-VBA?msg=5879883#xx5879883xx)

最新更新