Private Sub CommandButton1_Click()
' CREATE_IEAPP Macro
' Testing signing in creating an internet explorer application and passing username and pw to it
'
'THIS MACRO IS EDITED FROM IEAPP MACRO ,trying to pass values directly to the control boxes on the sign in form and not use send keys
Dim i As Long
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Dim shape As Excel.shape
Application.DisplayAlerts = False
Application.ScreenUpdating = False
' Statusbar
Application.StatusBar = "MY CODE is loading. Please wait, will take a moment..."
Worksheets("Auto SSD Here").Cells.ClearContents
For Each shape In Worksheets("Auto SSD Here").Shapes
shape.Delete
Next
' 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 Sheets("Auto Todays SSD").Range("A1").Value 'Changed from MACRO WORKAROUND A13 due to Web Querry not working....trying to go straight to the SSD to login
' Wait while IE loading...
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
'on error skip to scraping data because the error is likely that the internet explorer object is already signed in
On Error GoTo ScrapingData
' Wait while IE loading...
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
'the line below is working fine, take out the ' to login hard coded again, but trying to use a cell to pass the value to the login form, SEEMS TO WORK ON LINE BELOW IT
'IE.Document.getElementByID("Ecom_User_ID").Value = "HARD CODED USERNAME GOES HERE" 'looks like username ID is the same as name, inspecting the source did not have an ID
IE.document.getElementByID("Ecom_User_ID").Value = Range("B14")
' Wait while IE loading...
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
'the line below is working fine, take out the ' to login hard coded again, but trying to use a cell to pass the value to the login form, SEEMS TO WORK ON LINE BELOW IT
'IE.Document.getElementByID("password-password").Value = "HARD CODED PASSWORD GOES HERE" 'using get elements by ID here
IE.document.getElementByID("password-password").Value = Range("B15")
' Wait while IE loading...
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
IE.document.getElementByID("loginButton").Click
' Wait while IE loading...
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
'THIS SCRAPING DATA PART IS WHAT IS GIVING ME TROUBLE
'SCRAPING THE DATA USING A WEB QUERY DOES NOT SEEM TO WORK, BECAUSE THE WEB QUERY IS NOT RECOGNIZED AS LOGGED IN, EVEN IF EXCEL OPENED UP AN INTERNET EXPLORER OBJECT AND LOGGED IN A FEW SECONDS BEFORE.
' THEREFORE TRYING TO SCRAPE THE DATA INSIDE OF THE SAME INTERNET EXPLORER OBJECT WHICH EXCEL VBA USES TO LOG IN. USING COPY/PASTE DOES ACTUALLY USUALLY SEEM TO WORK (USING SEND KEYS) BUT UNFORTUNATELY
'SOMETIMES INSTEAD OF COPYING AND PASTING THE SSD PART, IT WILL SOMEHOW SKIP THE COPY PORTION, AND SIMPLY PASTE WHATEVER WAS LAST COPIED TO THE CLIPBOARD IN CELL A1
'Furthermore and even more unfortunately, a windows security dialog box pops up which requires clicking on cancel like 39 times before the SSD appears....
'NOW TRYING TO USE A DIFFERENT CODE THAT DOES NOT UTILIZE THE CLIPBOARD OR SENDKEYS, AND COMMENTING AWAY ALL OF THIS TEMPORARILY.
'ScrapingData:
'
'Application.DisplayAlerts = False
'
' SendKeys "^a"
'
' Do While IE.Busy
' Application.Wait DateAdd("s", 1, Now)
' Loop
'
' SendKeys "^c"
'
' Do While IE.Busy
' Application.Wait DateAdd("s", 1, Now)
' Loop
'
'
' Worksheets("Auto SSD Here").Cells.UnMerge
' Worksheets("Auto SSD Here").Paste Destination:=Worksheets("Auto SSD Here").Range("A1")
' Worksheets("Auto SSD Here").Cells.UnMerge
'
' ' Clean up
' 'IE.Quit 'Closes the window,deactivate if you want to leave ssd window open to make sure pasted properly, etc...
' Set IE = Nothing
' Set objElement = Nothing
' Set objCollection = Nothing
'
' Application.StatusBar = ""
' Application.DisplayAlerts = True
'
'Exit Sub
'ErrorMessageBox:
' 'MsgBox "Error, you are probably already signed in, OR the code is no longer working and needs attention, contact Me."
'This is the new code for scraping that does not use the clipboard and sendkeys, the portion above has all been commented INOP so that if needed it can be re-activated
'This code below has a problem too though, it only populates into the A column, all data is in one column.... inquiring on Mr Excel
'ScrapingData:
'
'With IE
' x = .document.body.innertext
' x = Replace(x, Chr(10), Chr(13))
' x = Split(x, Chr(13))
' Worksheets("Auto SSD Here").Range("A1").Resize(UBound(x)) = Application.Transpose(x) 'WHAT DO THE LAST 3 LINES DO???
'
' .Quit
' End With
'
' 'Cleaning up
' Set IE = Nothing
' Set objElement = Nothing
' Set objCollection = Nothing
' Application.StatusBar = ""
' Application.DisplayAlerts = True
'
'
'The problem with this code above is that every line of the SSD populates into only 1 column. It does get all of the data, and put each row on a different row (but also scrapes notes).
'Got this code below off of Mr Excel after inquiring
ScrapingData:
'On Error GoTo ErrorMessageBox
Dim table As Object, tRow As Object, tCell As Object
Dim rowIndex
Set table = IE.document.getElementsByTagName("TABLE")(3) '0 = 1st table, 1 = 2nd table, etc.
For Each tRow In table.Rows
For Each tCell In tRow.Cells
On Error Resume Next
'On Error GoTo SKIPCELL 'reactivate this if deactivate on error resume next
Worksheets("Auto SSD Here").Range("A1").Offset(tRow.rowIndex, tCell.cellIndex).Value = tCell.innerText 'this line is erroring after doing like 3 rows
On Error GoTo 0 'GET RID OF THIS LINE OF CODE ONCE DONE TRYING TO TROUBLESHOOT!!!!!!!!!!!!!!!!!!!!!!!
'GoTo NOERRORNEXT 'reactivate this if deactivate on error resume next
'SKIPCELL: 'reactivate this if deactivate on error resume next
'Resume SKIPCELL2 'reactivate this if deactivate on error resume next
'SKIPCELL2: 'reactivate this if deactivate on error resume next
'NOERRORNEXT: 'reactivate this if deactivate on error resume next
Next tCell
Next tRow
' Clean up
'IE.Quit 'Closes the window,deactivate if you want to leave ssd window open to make sure pasted properly, etc...
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
Application.StatusBar = ""
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Exit Sub
'ErrorMessageBox:
' MsgBox "Error, exit Excel completely and retry, if still throwing this error, coding needs attention, contact Me."
' Clean up
'IE.Quit 'Closes the window,deactivate if you want to leave ssd window open to make sure pasted properly, etc...
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
Application.StatusBar = ""
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub