Created
February 11, 2024 12:53
-
-
Save valuex/9e1ece93e9e697ec706971a870ca64f6 to your computer and use it in GitHub Desktop.
using vba to control Chrome by UIA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' tools -> reference -> UIAutomationClient | |
Function GetChrome(ByRef uia As CUIAutomation) As IUIAutomationElement | |
'Dim uia As New CUIAutomation | |
Dim el_Desktop As IUIAutomationElement | |
Set el_Desktop = uia.GetRootElement | |
Dim el_ChromeWins As IUIAutomationElementArray | |
Dim el_ChromeWin As IUIAutomationElement | |
Dim cnd_ChromeWin As IUIAutomationCondition | |
' check the window with class name as "Chrome_WidgetWin_1" | |
Set cnd_ChromeWin = uia.CreatePropertyCondition(UIA_ClassNamePropertyId, "Chrome_WidgetWin_1") | |
Set el_ChromeWins = el_Desktop.FindAll(TreeScope_Children, cnd_ChromeWin) | |
Set el_ChromeWin = Nothing | |
If el_ChromeWins.Length = 0 Then | |
Debug.Print """Chrome_WidgetWin_1"" not found" | |
Exit Function | |
End If | |
Dim count_ChromeWins As Integer | |
For count_ChromeWins = 0 To el_ChromeWins.Length - 1 | |
CurWinTitle = el_ChromeWins.GetElement(count_ChromeWins).CurrentName | |
If (InStr(1, CurWinTitle, "Chrome")) Then | |
Set el_ChromeWin = el_ChromeWins.GetElement(count_ChromeWins) | |
Exit For | |
End If | |
Next | |
If el_ChromeWin Is Nothing Then | |
Debug.Print "No Chrome Window Found" | |
Exit Function | |
End If | |
Set GetChrome = el_ChromeWin | |
End Function | |
Sub Chrome_NavigateTo() | |
Dim strURL As String | |
strURL = "file:///C:\Users\wei_x\Desktop\Nepcon2024\0a31e3a5-1386-4086-826e-17b162d076a1.mhtml" | |
Clipboard strURL | |
Dim uia As New CUIAutomation | |
Dim el_ChromeWin As IUIAutomationElement | |
Set el_ChromeWin = GetChrome(uia) | |
Debug.Print "1-" & el_ChromeWin.CurrentName | |
If el_ChromeWin Is Nothing Then | |
Debug.Print "Chrome doe NOT exist" | |
Exit Sub | |
End If | |
Dim cnd As IUIAutomationCondition | |
Set cnd = uia.CreatePropertyCondition(UIA_NamePropertyId, "地址和搜索栏") | |
Dim AddressBar As IUIAutomationElement | |
Set AddressBar = el_ChromeWin.FindFirst(TreeScope_Subtree, cnd) | |
Debug.Print AddressBar.CurrentName | |
Debug.Print AddressBar.GetCurrentPropertyValue(UIA_ValueValuePropertyId) | |
AddressBar.SetFocus | |
SendKeys "^a" | |
SendKeys "{DEL}" | |
SendKeys "^V" | |
SendKeys "{ENTER}" | |
End Sub | |
Function Clipboard$(Optional s$) | |
'https://stackoverflow.com/questions/14219455/excel-vba-code-to-copy-a-specific-string-to-clipboard/60896244#60896244 | |
Dim v: v = s 'Cast to variant for 64-bit VBA support | |
With CreateObject("htmlfile") | |
With .parentWindow.clipboardData | |
Select Case True | |
Case Len(s): .setData "text", v | |
Case Else: Clipboard = .GetData("text") | |
End Select | |
End With | |
End With | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment