Webpage Automation using VBA - vba

I'm new to VBA and programming in general, and I have found some code online to automate webpage processing. Right now I'm just trying to log into a webpage for e-Oscar:
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now())
Loop
Set objCollection = IE.document.getElementsByTagName("input")
Set objName = IE.document.getElementsByTagName("name")
i = 0
While i < objCollection.Length
If objCollection(i).ID = "companyId" Then
' Set text for search
objCollection(i).Value = "companyId"
Else
If objCollection(i).ID = "userId" Then
objCollection(i).Value = "uId"
Else
If objCollection(i).ID = "password" Then
objCollection(i).Value = "pwd"
Else
If objCollection(i).ID = "securityMsgAck1" Then
objCollection(i).Value = True
'Else
'If objName(i).Name = "securityMsgAck" Then
'objName(i).Value = True
'Else
'If objCollection(i).ID = "button" Then
'objCollection(i).Value = True
' "Search" button is found
Set objElement = objCollection(i)
End If
End If
End If
End If
'End If
'End If
i = i + 1
Wend
objElement.Click ' click button to search
When ran AS-IS. This code opens up the webpage I'm trying to automate, fills out company id, user id, password, and seems to check the box that I acknowledge the T&C of the page. However, whenever I uncomment out "securityMsgAck" it no longer checks the box and gives me an error:
Run-time error '91': Object variable or With block variable not set.\
It also does this if I comment out "securityMsgAck" and uncomment out "button". It seems that my code is trying to check the box whenever these two pieces are not executed, but when they are the statement either fails before it can attempt to press it, or they are blocking it altogether. I'm not really sure... any advice would be appreciated. Thanks in advance.
Edit: Whenever I uncomment out either section of that code, my program also will no longer fill out company Id, userId, or Password, along with failing to check the security message box...

to check the checkbox use the following code
ElseIf objcollection(i).ID = "securityMsgAck1" Then
objcollection(i).Checked = True

Related

Firing events associated with input box

I am having trouble firing events associated with an input box. These are Event listeners in the "Events" window of "DOM Explorer" when I inspect the relevant text box. They are however not listed in the HTML of the input box itself.
Of the several events listeners listed (e.g. "input", "blur", "paste") they all fire the same javascript function "x". Whilst my code does insert the text, the field does not validate on pressing the submit button and highlights red. A manual entry or a cut and paste will solve this.
I tried .fireEvent ; .dispatchEvent and even attempted to call the javascript directly. It is a public website and the address is in the code. I include below the code to fill in the first 3 fields.
Below is the HTML of the field:
<input class="office-form-question-textbox office-form-textfield-input form-control office-form-theme-focus-border border-no-radius" aria-labelledby="QuestionId_r57b9be42eed24e63b4d2af31c178f530 QuestionInfo_r57b9be42eed24e63b4d2af31c178f530" spellcheck="true" maxlength="4000" placeholder="Enter your answer">
The screenshot of the DOM explorer window:
Private Sub Test()
myAddress = "https://forms.office.com/Pages/ResponsePage.aspx?id=q4GdsF1ZzU2rJGpWVuGNiROj17Ac1mtIt6V80jIX_PFUMEs1VTZaQ0VRV1NNNVI4Vzg3V0RMOE83TS4u"
'create new instance of IE.
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate myAddress
'wait for loading
Do While IE.Busy = True Or IE.readyState <> 4: DoEvents: Loop
WasteTime (0.1) 'Custom function to pause for 0.1 sec
Dim myData(1 To 3, 1 To 3) As String
myData(1, 1) = "Member Surname"
myData(2, 1) = "QuestionId_r57b9be42eed24e63b4d2af31c178f530"
myData(3, 1) = "Chowdhary"
myData(1, 2) = "Member Forename"
myData(2, 2) = "QuestionId_rb88bf6d86f284dbaa14e5f3f27ee9f5b"
myData(3, 2) = "Saqib"
myData(1, 3) = "Member DOB"
myData(2, 3) = "QuestionId_r38d6e5656a6b4fbf86f586d10cbd8cb6"
myData(3, 3) = "19/04/1971"
Set objItem = IE.document.getElementsByTagName("input")
x = 0
Do While x < objItem.Length
Set ele = objItem(x)
mySearch = ele.outerHTML
For c = 1 To UBound(myData, 2)
If InStr(mySearch, myData(2, c)) > 0 Then
ele.Focus
ele.Value = myData(3, c)
Set event_onChange = IE.document.createEvent("HTMLEvents")
event_onChange.initEvent "paste", True, False
ele.dispatchEvent event_onChange 'This does not fire event
'ele.FireEvent ("onpaste") '- This did not work either
'IE.document.parentWindow.execScript code:="x(c)" ' Tried to directly call the javascript function
Exit For
End If
Next c
x = x + 1
Loop
End sub
The event in the search box can be fired with SendKeys. You can simulate user input using SendKeys to set value of the input boxes then click Submit to submit the form.
You can refer to the code snippet below. I have tested and it works:
Set objItem = IE.document.getElementsByTagName("input")(0)
objItem.Focus
SendKeys ("Chowdhary")
Application.Wait (Now + TimeValue("00:00:02"))
IE.document.getElementsByTagName("button")(4).Click
So I did finally get the code to work based on the same .initEvent and .dispatchEvent methods. I just needed to introduce a short 0.1 sec pause (using a custom "Wastetime" function) to allow the element to fully load. For interest the changed relevent code snippet is:
Set objItem = IE.Document.getElementsByTagName("input")
x = 0
Do While x < objItem.Length
Set ele = objItem(x)
mySearch = ele.outerHTML ' Could just enumerate the input indexes in order
For c = 1 To UBound(myInternalArray, 2)
If InStr(mySearch, myInternalArray(2, c)) > 0 Then
If myInternalArray(3, c) = "click" Then
ele.Click
WasteTime (0.1) 'Time for form to expand
Exit For
Else
ele.Value = myInternalArray(3, c)
ele.Focus
Set event_onInput = IE.Document.createEvent("HTMLEvents")
event_onInput.initEvent "input", True, False
ele.dispatchEvent event_onInput
Exit For
End If
End If
Next c
x = x + 1
Loop

How to send focus to a text box of a VBA form during its initialization/activate event?

I have a VBA form in Corel. Behaving exactly like a similar one in Excel...
Initially, when the form initialize event used to contain only some lines of code, the simple ending line me.txtCsv.Setfocus used to send the focus on it. I mean, it appeared being in edit mode with the cursor blinking inside.
After a period of time, after the application became complex, I am not able to send the focus to the text box in discussion.
I know that Activate event goes last and I also have in it the line me.txtCsv.Setfocus. But without expected result. Inside the Initialization event code I inserted that line Debug.Print Me.ActiveControl.Name & " - 1", changing 1 in 2, 3 up to 6 in many places, including the last line and all the time the name of the text box in discussion (txtCsv) appears in Immediate Window.
So, the control in discussion is the activate one, but the cursor is not inside it when the form is loaded.
TabStop is set to True. I set the TabIndex to 0.
The control is enabled and not blocked. I have created a new simple form with three text boxes and it works well.
I mean the text box which I want to send the focus, has the focus when the form is loaded, keeping a similar code in its Initialize or Activate events.
I compared all properties of the two forms and all text box controls and they are the same...
When I send the focus from another control on the form, the text box in discussion receives it.
It does not receive the focus (anymore) only when the form is shown, the focus being sent by Initialize or Activate evens.
Events code:
Private Sub UserForm_Activate()
Me.txtCsv.SetFocus
End Sub
Private Sub UserForm_Initialize()
Dim P As Printer, i As Long, NrImp As Long, prDefault As String, strJustEngr As String
Dim Printers() As String, n As Long, s As String, boolFound As Boolean
Dim strEng As String, MEngr As Variant, m As Variant, el As Variant, defSize As String
Dim strDropbox As String
boolOpt = True: boolFound = False
Me.cbPrinters.Clear
If Me.chkNewStyle.Value = True Then boolNewStyle = True
prDefault = Application.Printers.Default.Name
strEng = GetSetting(ECA_K, ECA_set, ECA_Engr, "No settings...")
If strEng <> "No settings..." Then
boolSelectedEngravers = True ' only adding engraver is possible...
MEngr = Split(strEng, "|")
'Incarcare in combo:
Me.cbPrinters.Clear
For Each el In MEngr
m = Split(el, ":")
Me.cbPrinters.AddItem m(0)
If m(0) = prDefault Then
boolFound = True
defSize = m(1)
End If
Next
Me.cbPrinters.Value = Me.cbPrinters.List(0)
With Me.btChoosePrinters
.Caption = "Add an Engraver"
.ControlTipText = "Add another Engraver(must be installed)"
End With
Me.btEliminatePrinters.Enabled = True
Me.lblPrinters.Caption = "Engravers: "
Me.cbPrinters.ControlTipText = "Select Engraver to be used!"
Else
Printers = GetPrinterFullNames()
For n = LBound(Printers) To UBound(Printers)
Me.cbPrinters.AddItem Printers(n)
If Printers(n) = prDefault Then boolFound = True
Next n
boolSelectedEngravers = False
End If
Debug.Print Me.ActiveControl.Name & " - 1"
If boolFound Then
Me.cbPrinters.Value = prDefault
Else
Me.lblStatus.Caption = "The default printer (""" & prDefault & """) is not a laser Engraver..."
End If
If GetSetting(ECA_K, ECA_set, "LowRAM", "No settings...") <> "No settings..." Then
boolLowRAM = CBool(GetSetting(ECA_K, ECA_set, "LowRAM", "No settings..."))
End If
If boolLowRAM = True Then
Me.chkLowRAM.Value = True
Else
Me.chkLowRAM.Value = False
End If
Debug.Print Me.ActiveControl.Name & " - 2"
'Direct engrave setting:
Dim strDirectEngrave As String
strDirectEngrave = GetSetting(ECA_K, ECA_set, ECA_Direct_Engrave, "Nothing")
If strDirectEngrave <> "Nothing" Then
Me.chkDirectEngrave.Value = CBool(strDirectEngrave)
If CBool(strDirectEngrave) = True Then
boolDirectEngrave = True
Else
boolDirectEngrave = False
End If
End If
'_______________________________________
strJustEngr = GetSetting(ECA_K, ECA_set, ECA_Just_Engrave, "Nothing")
If strJustEngr <> "Nothing" Then
'Application.EventsEnabled = False
boolChangeEngr = True
Me.chkJustEngrave.Value = CBool(strJustEngr)
boolChangeEngr = False
'Application.EventsEnabled = True
If CBool(strJustEngr) = True Then
Me.chkDirectEngrave.Enabled = True
boolJustEngrave = True
Me.frLocFoldPath.Enabled = True
Else
Me.frLocFoldPath.Enabled = False
Me.chkDirectEngrave.Enabled = False
End If
End If
Debug.Print Me.ActiveControl.Name & " - 3"
If boolSelectedEngravers Then
Application.EventsEnabled = False
Me.btGo.ForeColor = RGB(45, 105, 7)
Me.txtCsv.BackColor = RGB(153, 255, 51)
Me.btGo.Enabled = False
Me.txtCsv.SetFocus
Application.EventsEnabled = True
End If
strDropbox = GetSetting(ECA_K, ECA_set, ECA_Dropbox, "No value")
If strDropbox <> "No value" Then
If CBool(strDropbox) = True Then
Me.chkDropbox.Value = True
End If
End If
AllRefresh
Me.chkCloseDoc.Value = True
Me.txtCsv.SetFocus
Debug.Print Me.ActiveControl.Name & " - 4"
End Sub
Private Sub AllRefresh()
Application.Optimization = False
Application.EventsEnabled = True
If Documents.Count > 0 Then
ActiveWindow.Refresh
ActiveDocument.PreserveSelection = True
End If
Application.Refresh
End Sub
Is there something else, crossing your mind, to be tested?
In the meantime I did some more tests, respectively:
I created a new project (.GMS file) and I imported the form in discussion.I started commenting all the Initialize event code, except the last two code lines.
It didn't set the focus! Commenting everything, letting only the Activate event code, it worked.
I started to un-comment lines in Initialize event code and I found a line not allowing the focus to be sent to that text box.
Setting the value of the combo: Me.cbPrinters.Value = Me.cbPrinters.List(0), moving it in the Activate event code, before the part pointing to txtCSV, worked well.
Now, I tried to do the same in the original form and it does not work...
The above question has been solved by Disabling followed by Enabling of the text box in discussion, but only doing that in Form Activate event. It did not work in Initialize event...
Private Sub UserForm_Activate()
Me.txtCsv.Disable: Me.txtCsv.Enable
Me.txtCsv.SetFocus
End Sub

Excel is waiting for another application to complete an OLE action

Before you go for the obvious: Application.DisplayAlerts = False has not solved my problem.
I have written a VBA procedure (initiated in Excel 2010) which loops around an array containing different Excel files. The loop opens the file, refreshes the data, saves and closes the file for each item in the array. I have written an error catch sub routine so I log which excel files have failed to open/refresh/save etc so a user can manually check them.
Some files are quite large and involve a large amount of data moving across the network; sometimes I get a dialog box with: Excel is waiting for another application to complete an OLE action.
I could use Application.DisplayAlerts = False to disable the message but this would presumably disable all alerts so I couldn't catch the errors?
Further I have tested using the line and it doesn't stop the dialog box pop-up. If I press enter it carries on but will likely pop-up again a few minutes later.
Is there a way to stop is message specifically without stopping other alerts?
NB. My process has a control instance of Excel which runs the VBA and opens the workbooks to be refreshed in a separate instance.
Thanks for your help
An extract of my code is below which contains the refresh elements
Sub Refresh_BoardPivots_Standard()
' On Error GoTo Errorhandler
Dim i
Dim errorText As String
Dim x
Dim objXL As Excel.Application
Set objXL = CreateObject("Excel.Application")
GetPivotsToRefresh ' populate array from SQL
For Each i In StandardBoardPiv
DoEvents
'If File_Exists(i) Then
If isFileOpen(i) = True Then
errorText = i
Failed(failedIndex) = errorText
failedIndex = failedIndex + 1
Else
objXL.Visible = True 'False
objXL.Workbooks.Open FileName:=i
If objXL.ActiveWorkbook.ReadOnly = False Then
BackgroundQuery = False
Application.DisplayAlerts = False
objXL.ActiveWorkbook.RefreshAll
objXL.Application.CalculateFull
objXL.Application.DisplayAlerts = False
objXL.ActiveWorkbook.Save
objXL.Application.DisplayAlerts = True
objXL.Quit
Else
errorText = i
Failed(failedIndex) = errorText
failedIndex = failedIndex + 1
objXL.Application.DisplayAlerts = False
objXL.Quit
Application.DisplayAlerts = True
End If
End If
' Else
' errorText = i
' Failed(failedIndex) = errorText
' failedIndex = failedIndex + 1
' End If
DoEvents
If Ref = False Then
Exit For
End If
Next i
Exit Sub
'Errorhandler:
'
'errorText = i
'Failed(failedIndex) = errorText
'failedIndex = failedIndex + 1
'Resume Next
End Sub
"Waiting for another application to complete an OLE action" isn't an alert message you can just turn off and forget, sometimes the macro will be able to continue on after, but in my experience if you are getting that error its only a matter of time until the problem crashes/freezes your whole macro so it should definitely be troubleshot and corrected.
I only get that error when I am using additional Microsoft Office Applications (other than the Excel that is running the code) as objects and one of them has an error- the Excel running the code doesn't know that an error occurred in one of the other applications so it waits and waits and waits and eventually you get the "Waiting for another application to complete an OLE action" message...
So to troubleshoot this sort of problem you got to look for the places you use other MSO apps... In your example, you have an additional instance of Excel and you are pulling data from Access, so its most likely one of those two that is causing the problems...
Below is how I would re-write this code, being more careful with where the code interacts with the other MSO apps, explicitly controlling what is happening in them.. The only piece I couldn't really do much is GetPivotsToRefresh because I cant see what exactly youre doing here, but in my code I just assumed it returned an array with a list of the excel files you want to update. See code below:
Sub Refresh_BoardPivots_Standard()
Dim pivotWB As Workbook
Dim fileList() As Variant
Dim fileCounter As Long
Application.DisplayAlerts = False
fileList = GetPivotsToRefresh 'populate array from SQL
For fileCounter = 1 To UBound(fileList, 1)
Set pivotWB = Workbooks.Open(fileList(fileCounter, 1), False, False)
If pivotWB.ReadOnly = False Then
Call refreshPivotTables(pivotWB)
pivotWB.Close (True)
Else
'... Error handler ...
pivotWB.Close (False)
End If
Next
End Sub
Public Sub refreshPivotTables(targetWB As Workbook)
Dim wsCounter As Long
Dim ptCounter As Long
For wsCounter = 1 To targetWB.Sheets.Count
With targetWB.Sheets(wsCounter)
If .PivotTables.Count > 0 Then
For ptCounter = 1 To .PivotTables.Count
.PivotTables(ptCounter).RefreshDataSourceValues
Next
.Calculate
End If
End With
Next
End Sub
So I created my own 'refreshPivotTables' but you could have embedded that into the master sub, I just thought the loops and loop counters might get a little messy at that point...
Hope this helps,
TheSilkCode

Creating Permanent Textbox on an Excel Userform

So I am creating a tool and user guide for my client. I am attempting to integrate the user guide into Excel in the form of a Userform. My first attempt was to insert these as images of the word document, however, if the user guide is ever updated that could mean a lot of work for anyone to update the userform as well. So I was looking to see if I could have a button for the user to click that would clear the userform and recreate it dynamically any time the User Guide is updated. My issue is that when I run my code the textboxes I create which contain the text from the user guide disappear after the userform is closed.
Do I have to have a set number of textboxes or can this be dynamic in the case that the user ever adds a new section to the user guide? Can I create textboxes that stay on the userform once it is closed?
My code is below:
For i = 1 To totPara
If wrdDoc.Paragraphs(i).Style = wrdDoc.Styles("Heading 1") Or wrdDoc.Paragraphs(i).Style = wrdDoc.Styles("Heading 2") Then
headerCtr = headerCtr + 1
If headerCtr = 2 Then
labelCtr = labelCtr + 1
Set tempTxt = Nothing
Set tempTxt = userGuide.Controls.Add("Forms.TextBox.1", "Test" & labelCtr, True)
With tempTxt
.Height = 276
.Width = 288
.Top = 54
.Left = 42
.MultiLine = True
End With
tempTxt.Text = wrdDoc.Paragraphs(i).Range.Text & Chr(13)
ElseIf headerCtr > 2 Then
Exit For
End If
ElseIf labelCtr <> 0 Then
tempTxt.Text = tempTxt.Text & wrdDoc.Paragraphs(i).Range.Text & Chr(13)
End If
Next i
For right now I set it to create a new textbox only when headerCtr is equal to 2 for testing but eventually I would like to create a new textbox for each of the 9 sections.
Thank you in advance for any help.
You have the option Hide the userform instead of Close the userform. When it is hidden the textboxes can still be accessed from the calling form.
Dim frm1 As frmMainInput
Set frm1 = New frmMainInput
frm1.tbProjectNumber.Value = iProject_Number
frm1.txtDocsInUse.Text = sDocsInUse
frm1.Show
If frm1.Proceed = False Then
GoTo Local_Exit
End If
iProject_Number = CInt(frm1.tbProjectNumber.Value)
Eventually call frm.close and set frm = Nothing
In the UserForm:
Private Sub cmdCancel_Click()
Proceed = False
Me.Hide
End Sub
Private Sub cmdOK_Click()
Proceed = True
Me.Hide
End Sub
No clue about the refreshing images etc.c

UPS Automated tracking clicking a button

I want to visit webpage. Then enter a number in test box. I was able to do that.
after that I want to click on the Track Button. Any idea how can I click through vba?
I already tried below commands. Any idea how can i find id of the Track button?
ie.document.getElementByName("track.x").Click
ie.document.getElementByClass("button btnGroup6 arrowIconRight").Click
Lets say we enter the tracking number "1ZW2E2360449018801" and once I click that button, a new webpage opens. I want to click on "shipment Progress" bar and copy the table that appears. any suggestions?
This will get it done..
Sub test()
' open IE, navigate to the website of interest and loop until fully loaded
Set ie = CreateObject("InternetExplorer.Application")
my_url = "http://wwwapps.ups.com/WebTracking/track?loc=en_US"
With ie
.Visible = True
.navigate my_url
.Top = 50
.Left = 530
.Height = 400
.Width = 400
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
End With
' Enter a value in the "Number" text box
ie.Document.getElementById("trackNums").Value = "1ZW2E2360449018801"
' Click the "Submit" button
Set Results = ie.Document.getElementsByTagName("input")
For Each itm In Results
If InStr(1, itm.outerhtml, "Track", vbTextCompare) > 0 Then
itm.Click
exit for
End If
Next
' Click the "Shipment Progress" button
Set Results = ie.Document.getElementsByTagName("h4")
For Each itm In Results
If InStr(1, itm.outerhtml, "Shipment Progress", vbTextCompare) > 0 Then
itm.Click
exit for
End If
Next
' Get the text from the "Shipment Progress Table" and assign to a variable
tbltxt = ie.Document.getElementsByTagName("table")(4).innertext
' process the text as desired
End Sub
Something like this should work:
ie.document.getElementsByName("track.x")(0).Click
Note it's getElement*s*ByName, so it returns a collection, not a single element.