| VBA - Web Automation - Part 1 |
| Sub YouTube() |
| Dim i As Long |
| Dim IE As Object |
| Dim objElement As Object |
| Dim objCollection As Object |
| ' Create InternetExplorer Object |
| Set IE = CreateObject("InternetExplorer.Application") |
| ' You can uncoment Next line To see form results |
| IE.Visible = True |
| ' Send the form data To URL As POST binary request |
| IE.Navigate "https://www.youtube.com/feed/trending" |
| ' Statusbar |
| Application.StatusBar = "www.excely.com is loading. Please wait..." |
| ' Wait while IE loading... |
| Do While IE.Busy |
| Application.Wait DateAdd("s", 1, Now) |
| Loop |
| ' Find 2 input tags: |
| ' 1. Text field |
| ' <input type="text" class="textfield" name="s" size="24" value="" /> |
| ' |
| ' 2. Button |
| ' <input type="submit" class="button" value="" /> |
| Application.StatusBar = "Search form submission. Please wait..." |
| Set objCollection = IE.Document.getElementsByTagName("a") |
| i = 0 |
| While i < objCollection.Length |
| ThisWorkbook.Sheets("YouTube").Cells(i + 1, 1) = objCollection(i).href |
| i = i + 1 |
| Wend |
| 'objElement.Click ' click button to search |
| ' Wait while IE re-loading... |
| Do While IE.Busy |
| Application.Wait DateAdd("s", 1, Now) |
| Loop |
| ' Show IE |
| IE.Visible = True |
| ' Clean up |
| Set IE = Nothing |
| Set objElement = Nothing |
| Set objCollection = Nothing |
| Application.StatusBar = "" |
| End Sub |
VBA Reference to be added: VBA - UI Automation Project sample code Dim MyElement As UIAutomationClient.IUIAutomationElement Dim MyElement1 As UIAutomationClient.IUIAutomationElement 'Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Public Enum oConditions eUIA_NamePropertyId eUIA_AutomationIdPropertyId eUIA_ClassNamePropertyId eUIA_LocalizedControlTypePropertyId End Enum Sub Test() Dim AppObj As UIAutomationClient.IUIAutomationElement Dim oInvokePattern As UIAutomationClient.IUIAutomationInvokePattern Dim oAutomation As New CUIAutomation ' the UI Automation API\ Dim oPattern As UIAutomationClient.IUIAutomationLegacyIAccessiblePattern Set AppObj = WalkEnabledElements("BTB Portal 3.0 KA") Set MyElement = AppObj.FindFirst(TreeScope_Children, PropCon...
To close an internet explorer instance
ReplyDeleteDim IE As Object
Set IE = createobject("internet explorer.application")
Set IE= getobject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")
'DO STUFF
'CLEAN UP
IE.QUIT
SET IE = NOTHING