VBA Click Button - vba

Using VBA, how can I click the orange LOGIN button on the below page:
https://www.solaraccreditation.com.au/login.html
Inspect element shows:
<input class="button bg-orange" value="Login" type="submit">
So far my code is:
Option Explicit
Private Declare Sub ShowWindow Lib "user32" _
(ByVal hWnd As Long, ByVal nCmdShow As Long)
Const strTMP As String = "Excel VBA"
Private Const SW_MAXIMIZE = 3
Public Sub Test()
Dim objWindow As Object
Dim objIEApp As Object
Dim objShell As Object
Dim objItem As Object
Dim objInputs As Object
Dim ele As Object
On Error GoTo Fin
Set objShell = CreateObject("Shell.Application")
Set objWindow = objShell.Windows()
For Each objItem In objWindow
If LCase(objItem.FullName Like "*iexplore*") Then
Set objIEApp = objItem
End If
Next objItem
If objIEApp Is Nothing Then
Set objIEApp = CreateObject("InternetExplorer.Application")
objIEApp.Visible = True
End If
With objIEApp
.Visible = True
ShowWindow .hWnd, SW_MAXIMIZE
.Navigate "https://www.solaraccreditation.com.au/login.html"
While Not .ReadyState = 4
DoEvents
Wend
.document.All("username").Value = _
ThisWorkbook.Sheets("sheet1").Range("a1")
.document.All("password").Value = _
ThisWorkbook.Sheets("sheet1").Range("a2")
End With
' <input class="button bg-orange" type="submit" value="Login" />
Fin:
If Err.Number <> 0 Then MsgBox "Error: " & _
Err.Number & " " & Err.Description
Set objWindow = Nothing
Set objShell = Nothing
End Sub
Mary Poppins "Classic Disney" soundtrack. ... Um diddle diddle, diddle um, diddle ay x2 ? Now, you can say it backwards, which is docious-ali-expi-listic-frag
i-cali-rupus.

You could try identifying the button as the first in its class.
.document.getelementsbyclass("button bg-orange")(0).click
But it might be more expedient to simply force the form to submit.
.document.getelementbyid("loginForm").submit

Related

Excel VBA: auto click and open file from website

Thanks to Qharr, I have successfully performed auto search on the website.(My previous question:
Excel VBA: Cannot perform auto search on website)
I have another question concerning the next step: I would always like to click the first link that appears after clicking the search button, and open the file in order to extract certain data. Is there any ways to do that? Thanks!
Codes that I have at present:
Option Explicit
Sub Searchstockcode()
Dim SearchString As String, SearchBox As Object, SearchButton As Object, ie As Object
SearchString = "2828"
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "http://www.hkexnews.hk/listedco/listconews/advancedsearch/search_active_main.aspx"
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
Set SearchBox = ie.document.getElementById("ctl00_txt_stock_code")
SearchBox.Value = SearchString
Set SearchButton = ie.document.querySelector("[src*='/image/search.gif']")
SearchButton.Click
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
'Click the first result
Set TargetFile = ie.document.getElementById("ctl00_gvMain_ctl02_hlTitle")
TargetFile.Click
'Here I would like to open the file in excel, but I am stuck at the "save as" pop up.
'As long as the file can be opened, I should be able to complete the data extraction with my own codes.
ie.Quit
End Sub
You can extract the URL for the file download and binary file download. In the example below, the file is stored in a variable wb for later use.
In the following the filedownload link is extracted via TargetFile.href and passed to a function to perform ADODB binary download. You could also pass the URL for download to URLMon as shown in my answer here.
Option Explicit
Public Sub Searchstockcode()
Dim SearchString As String, SearchBox As Object, SearchButton As Object, ie As Object
SearchString = "2828"
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "http://www.hkexnews.hk/listedco/listconews/advancedsearch/search_active_main.aspx"
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
Set SearchBox = ie.document.getElementById("ctl00_txt_stock_code")
SearchBox.Value = SearchString
Set SearchButton = ie.document.querySelector("[src*='/image/search.gif']")
SearchButton.Click
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
Dim TargetFile As Object
Set TargetFile = ie.document.getElementById("ctl00_gvMain_ctl02_hlTitle")
On Error Resume Next
Dim wb As Workbook
Set wb = Workbooks.Open(DownloadFile("C:\Users\User\Desktop\", TargetFile.href)) '< Replace with your download path here ending in "\"
On Error GoTo 0
'Other stuff
ie.Quit
End Sub
Public Function DownloadFile(ByVal downloadFolder As String, ByVal downloadURL As String) As String
Dim http As Object , tempArr As Variant
Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
http.Open "GET", downloadURL, False
http.send
On Error GoTo errhand
With CreateObject("ADODB.Stream")
.Open
.Type = 1
.write http.responseBody
tempArr = Split(downloadURL, "/")
tempArr = tempArr(UBound(tempArr))
.SaveToFile downloadFolder & tempArr, 2 '< "/" on enter of downloadFolder. 2 for overwrite which is Ok if no file modifications.
.Close
End With
DownloadFile = downloadFolder & tempArr
Exit Function
errhand:
If Err.Number <> 0 Then
Debug.Print Err.Number, Err.Description
MsgBox "Download failed"
End If
DownloadFile = vbNullString
End Function
URLMon version:
Option Explicit
Public Const BINDF_GETNEWESTVERSION As Long = &H10
#If VBA7 And Win64 Then
Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" ( _
ByVal pCaller As LongPtr, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As LongPtr, _
ByVal lpfnCB As LongPtr _
) As Long
#Else
Private Declare Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" ( _
ByVal pCaller As Long, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Long, _
ByVal lpfnCB As Long _
) As Long
#End If
Public Sub Searchstockcode()
Dim SearchString As String, SearchBox As Object, SearchButton As Object, ie As Object
SearchString = "2828"
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "http://www.hkexnews.hk/listedco/listconews/advancedsearch/search_active_main.aspx"
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
Set SearchBox = ie.document.getElementById("ctl00_txt_stock_code")
SearchBox.Value = SearchString
Set SearchButton = ie.document.querySelector("[src*='/image/search.gif']")
SearchButton.Click
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
Dim TargetFile As Object
Set TargetFile = ie.document.getElementById("ctl00_gvMain_ctl02_hlTitle")
On Error Resume Next
Dim wb As Workbook
Set wb = Workbooks.Open(downloadFile("C:\Users\User\Desktop\", TargetFile.href)) '< Replace with your download path here ending in "\"
On Error GoTo 0
'Other stuff
ie.Quit
End Sub
Public Function downloadFile(ByVal downloadFolder As String, ByVal URL As String) As String
Dim tempArr As Variant, ret As Long
tempArr = Split(URL, "/")
tempArr = tempArr(UBound(tempArr))
ret = URLDownloadToFile(0, URL, downloadFolder & tempArr, BINDF_GETNEWESTVERSION, 0)
downloadFile = downloadFolder & tempArr
End Function

VBA: Excel macro code correction

I am looking for a VBA code that can help me to click a button from a web page. I got the below VBA from one of the sites which works as expected.
Sub followWebsiteLink()
Dim ie As InternetExplorer
Dim html As HTMLDocument
Dim Link As Object
Dim ElementCol As Object
Application.ScreenUpdating = False
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate "https://www.google.co.in"
Do While ie.readyState <> READYSTATE_COMPLETE
Application.StatusBar = "Loading website…"
DoEvents
Loop
Set html = ie.document
Set ElementCol = html.getElementsByTagName("a")
For Each Link In ElementCol
If Link.innerHTML = "Google Pixel 2" _ 'Or u can use "Advertising"
Then
Link.Click
End If
Next Link
Set ie = Nothing
Application.StatusBar = ""
Application.ScreenUpdating = True
End Sub
But if I replace it with the site name I want, it wont work.
**The changes I made are as follows **
ie.navigate "https://www.flipkart.com/"
For Each Link In ElementCol
If Link.innerHTML = "Log In" _
And Link.getElementsByClassName = "_2k0gmP" _
Then
Link.Click
End If
Next Link
Try this code, worked for me to click on Log In
Code
#If VBA7 Then
Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If
Sub test()
Dim IE As Object
Dim htmlDoc As Object
Dim sURL As String
' CREATING OBJECT
Set IE = CreateObject("internetexplorer.application")
sURL = "https://www.flipkart.com/"
' WEBPAGE NAVIGATION
With IE
.navigate (sURL)
.Visible = True
End With
WaitIE IE, 2000
' CLICK ON LOG IN
Set htmlDoc = IE.document
Set buttonclick = htmlDoc.getElementsByClassName("_2k0gmP")
buttonclick(7).Click
WaitIE IE, 5000
'IE.Quit
'Set IE = Nothing
End Sub
Sub WaitIE(IE As Object, Optional time As Long = 250)
'Code from: https://stackoverflow.com/questions/33808000/run-time-error-91-object-variable-or-with-block-variable-not-set
Dim i As Long
Do
Sleep time
Debug.Print CStr(i) & vbTab & "Ready: " & CStr(IE.readyState = 4) & _
vbCrLf & vbTab & "Busy: " & CStr(IE.Busy)
i = i + 1
Loop Until IE.readyState = 4 Or Not IE.Busy
End Sub
Edit
You can use this code to get the href:
For Each Element In buttonclick
Debug.Print Element.href
Next
Or to get the InnerText:
For Each Element In buttonclick
Debug.Print Element.innerText & " " & i
i = i + 1
Next
'Where i is the number of the item
Then you can use conditional such as:
For Each Element In buttonclick
If Element.href = "href_link" Then Element.Click
Next
OR
For Each Element In buttonclick
If Element.innerText = "Log In" Then Element.Click
Next

Interact with Active/Single IE11 Session VBA

So I have a loop that exports data from a website. However, for each case, it starts a new session and closes. Is there a method to navigate and download for all the cases in just one IE11 session and then closing out? Below is the code that I have right now:
Public Sub Get_File()
Dim sFiletype As String 'Fund type reference
Dim sFilename As String 'File name (fund type + date of download), if "" then default
Dim sFolder As String 'Folder name (fund type), if "" then default
Dim bReplace As Boolean 'To replace the existing file or not
Dim sURL As String 'The URL to the location to extract information
Dim Cell, Rng As Range
Dim Sheet As Worksheet
'Initialize variables
Set Rng = Range("I2:I15")
Set Sheet = ActiveWorkbook.Sheets("Macro_Button")
For Each Cell In Rng
If Cell <> "" Then
sFiletype = Cell.Value
sFilename = sFiletype & "_" & Format(Date, "mmddyyyy")
sFolder = Application.WorksheetFunction.VLookup(Cell.Value, Sheet.Range("I2:J15"), 2, False)
bReplace = True
sURL = "www.preqin.com"
'Download using the desired approach, XMLHTTP / IE
If Application.WorksheetFunction.VLookup(Cell.Value, Sheet.Range("I2:W15"), 15, False) = 1 Then
Call Download_Use_IE(sURL, sFilename, sFolder, bReplace)
Else
Call Download_NoLogin_Use_IE(sURL, sFilename, sFolder, bReplace)
End If
Else
Exit Sub
End If
Next
End Sub
Private Sub Download_Use_IE(ByRef sURL As String, _
Optional ByRef sFilename As String = "", _
Optional ByRef sFolder As String = "", _
Optional ByRef bReplace As Boolean = True)
Dim oBrowser As InternetExplorer
Dim hDoc As HTMLDocument
Dim objInputs As Object
Dim ele As Object
On Error GoTo ErrorHandler
'Create IE object
Set oBrowser = New InternetExplorer
oBrowser.Visible = True
'Navigate to URL
Call oBrowser.navigate(sURL)
While oBrowser.Busy Or oBrowser.readyState <> 4: DoEvents: Wend
'Skips log in step if already signed into website
On Error GoTo LoggedIn
'Enter username
oBrowser.document.getElementById("ctl00_ctl00_cphSiteHeader_ucLoginForm_user_email").Value = "XXX"
oBrowser.document.getElementById("ctl00_ctl00_cphSiteHeader_ucLoginForm_user_password").Value = "XXX"
'Submit the sign in
oBrowser.document.getElementById("ctl00_ctl00_cphSiteHeader_ucLoginForm_btnLogin").Click
'Wait for website to load
While oBrowser.Busy Or oBrowser.readyState <> 4: DoEvents: Wend
LoggedIn:
'All PE
oBrowser.navigate Range("H3").Value
'Wait for website to load
While oBrowser.Busy Or oBrowser.readyState <> 4: DoEvents: Wend
'Set the htmldocument
Set hDoc = oBrowser.document
'Loop and click the download file button
Set objInputs = oBrowser.document.getElementsbyTagName("input")
For Each ele In objInputs
If ele.Title Like "Download Data to Excel" Then
ele.Click
End If
Next
'Wait for dialogue box to load
While oBrowser.Busy Or oBrowser.readyState > 3: DoEvents: Wend
Application.Wait (Now + TimeValue("0:00:02"))
'IE 9+ requires to confirm save
Call Download(oBrowser, sFilename, sFolder, bReplace)
'Close IE
oBrowser.Quit
Exit Sub
ErrorHandler:
'Resume
Debug.Print "Sub Download_Use_IE() " & Err & ": " & Error(Err)
End Sub
Modify your download_IE procedure to use a Browser that is passed to it:
Private Sub Download_Use_IE(oBrowser As InternetExplorer, _
ByRef sURL As String, _
Optional ByRef sFilename As String = "", _
Optional ByRef sFolder As String = "", _
Optional ByRef bReplace As Boolean = True)
Dim hDoc As HTMLDocument
Dim objInputs As Object
Dim ele As Object
On Error GoTo ErrorHandler
'Create IE object
oBrowser.Visible = True
'Navigate to URL
Call oBrowser.navigate(sURL)
......rest of code
Call Download(oBrowser, sFilename, sFolder, bReplace)
'Do not Close IE
Exit Sub
ErrorHandler:
'Resume
Debug.Print "Sub Download_Use_IE() " & Err & ": " & Error(Err)
End Sub
Then modify your procedure to pass this object:
Public Sub Get_File()
'declare all variables plus:
Dim oBrowser As InternetExplorer
Set oBrowser = New InternetExplorer
.....put additional code here.....
If Application.WorksheetFunction.VLookup(Cell.Value, Sheet.Range("I2:W15"), 15, False) = 1 Then
Call Download_Use_IE(oBrowser, sURL, sFilename, sFolder, bReplace)
Else
Call Download_NoLogin_Use_IE(oBrowser, sURL, sFilename, sFolder, bReplace)
End If
Else
Exit Sub
End If
Next
'Close IE
oBrowser.Quit
End Sub
You will need to do the same thing for the other procedure.

Excel VBA to detect if Outlook is open, if its not ,then open it

I have written code to download an attachment to a specified folder.
Const olFolderInbox = 6
Sub detectpp_plate_record1()
Dim oOutlook As Object
Dim oOlns As Object
Dim oOlInb As Object
Dim unRead, m As Object, att As Object
'~~> Get Outlook instance
Set oOutlook = GetObject(, "Outlook.application")
Set oOlns = oOutlook.GetNamespace("MAPI")
Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox)
'~~> Check if there are any actual unread emails
Set unRead = oOlInb.Items.Restrict("[UnRead] = True")
' File_Path = "D:\Attach\"
File_Path = "C:\Users\Desktop\pocket setter excel\"
If unRead.Count = 0 Then
MsgBox "NO Unread Email In Inbox"
Else
For Each m In unRead
If m.Attachments.Count > 0 Then
For Each att In m.Attachments
If att.Filename Like "plate record*" Then
MsgBox "Unread Email with attachment available In Inbox"
'Like "plate record*.xls"
'~~> Download the attachment
' to the file path and file name
'att.Filename = name of attachement
att.SaveAsFile File_Path & "plate record"
'att.SaveAsFile File_Path & att.Filename
'& Format(plate record)
' mark attachment as read
m.unRead = False
DoEvents
m.Save
WorkFile = Dir(File_Path & "*")
Do While WorkFile <> ""
If Right(WorkFile, 4) <> "xlsm" Then
Workbooks.Open Filename:=File_Path & WorkFile
ActiveWorkbook.SaveAs Filename:= _
File_Path & WorkFile & "", FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
ActiveWorkbook.Close
Kill File_Path & WorkFile
End If
WorkFile = Dir()
Loop
Exit Sub
End If
Next att
End If
Next m
End If
End Sub
The problem : This can be executed only when Outlook is open.
Therefore I have to separately open Outlook.
My requirement is to use Excel VBA code to detect if Outlook is open, if it is not, then it should be opened.
---------------------UDATE-----------------------
I combined the above code with the following code.
#Const LateBind = True
Const olMinimized As Long = 1
Const olMaximized As Long = 2
Const olFolderInbox As Long = 6
Sub detectpp_plate_record()
MyMacroThatUseOutlook
detectpp_plate_record1
End Sub
#If LateBind Then
Public Function OutlookApp( _
Optional WindowState As Long = olMinimized, _
Optional ReleaseIt As Boolean = False _
) As Object
Static oOutlook As Object
#Else
Public Function OutlookApp( _
Optional WindowState As Outlook.OlWindowState = olMinimized, _
Optional ReleaseIt As Boolean _
) As Outlook.Application
Static oOutlook As Outlook.Application
#End If
On Error GoTo ErrHandler
Select Case True
Case oOutlook Is Nothing, Len(oOutlook.name) = 0
Set oOutlook = GetObject(, "Outlook.Application")
If oOutlook.Explorers.Count = 0 Then
InitOutlook:
'Open inbox to prevent errors with security prompts
oOutlook.Session.GetDefaultFolder(olFolderInbox).Display
oOutlook.ActiveExplorer.WindowState = WindowState
End If
Case ReleaseIt
Set oOutlook = Nothing
End Select
Set OutlookApp = oOutlook
ExitProc:
Exit Function
ErrHandler:
Select Case Err.Number
Case -2147352567
'User cancelled setup, silently exit
Set oOutlook = Nothing
Case 429, 462
Set oOutlook = GetOutlookApp()
If oOutlook Is Nothing Then
Err.Raise 429, "OutlookApp", "Outlook Application does not appear to be installed."
Else
Resume InitOutlook
End If
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Unexpected error"
End Select
Resume ExitProc
Resume
End Function
#If LateBind Then
Private Function GetOutlookApp() As Object
#Else
Private Function GetOutlookApp() As Outlook.Application
#End If
On Error GoTo ErrHandler
Set GetOutlookApp = CreateObject("Outlook.Application")
ExitProc:
Exit Function
ErrHandler:
Select Case Err.Number
Case Else
'Do not raise any errors
Set GetOutlookApp = Nothing
End Select
Resume ExitProc
Resume
End Function
Sub MyMacroThatUseOutlook()
Dim OutApp As Object
Set OutApp = OutlookApp()
'Automate OutApp as desired
End Sub
Now, if Outlook is open the code searches for the specified unread email.
If Outlook is closed, it opens it, but afterwards there is an error
Run time error 429:
ActiveX component cant create object.
Therefore once again I have to click on button for the code to search for the specified emails.
How do I get rid of this error and perform this in one go?
Add this to your code:
Dim oOutlook As object
On Error Resume Next
Set oOutlook = GetObject(, "Outlook.Application")
On Error Goto 0
If oOutlook Is Nothing Then
Set oOutlook = CreateObject("Outlook.Application")
End If
I tried and tested it . It works.
Something like this:-
Set oOutlook = GetObject(, "Outlook.application")
If oOutlook is nothing Then
'outlook is not running so start it
set oOutlook = New Outlook.Application
Else
' outlook is running
End If

VBA/Outlook extracting attachments from .eml files

I'm trying to take a folder full of .eml messages with attachments and then extract/rename/save the attachments in another folder. My code :
Sub SaveAttachments()
Dim OlApp As Outlook.Application
Set OlApp = GetObject(, "Outlook.Application")
Dim MsgFilePath
Dim Eml As Outlook.MailItem
Dim att As Outlook.Attachments
Dim Path As String
Path = "C:\Users\richard\Desktop\Inbox\"
If OlApp Is Nothing Then
Err.Raise ERR_OUTLOOK_NOT_OPEN
End If
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
Dim temp As Object
Set temp = fs.GetFolder(Path)
For Each MsgFilePath In temp.Files
Set Eml = OlApp.CreateItemFromTemplate(Path & MsgFilePath.Name)
Set att = Eml.Attachments
If att.Count > 0 Then
For i = 1 To att.Count
fn = "C:\Users\richard\Desktop\cmds\" & Eml.SenderEmailAddress
att(i).SaveAsFile fn
Next i
End If
Set Eml = Nothing
Next
Set OlApp = Nothing
End Sub
But I'm getting straightaway this error on the first file in the loop, ie the line
Set Eml = OlApp.CreateItemFromTemplate(Path & MsgFilePath.Name) :
-2147286960 (80030050) %1 already exists.
Any ideas on what is going on much appreciated !
Try this (TRIED AND TESTED)
Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As _
String, ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWMINIMIZED As Long = 2
Sub SaveAttachments()
Dim OlApp As Outlook.Application
Set OlApp = GetObject(, "Outlook.Application")
Dim MsgFilePath
Dim Eml As Outlook.MailItem
Dim att As Outlook.Attachments
Dim sPath As String
sPath = "C:\Users\richard\Desktop\Inbox\"
If OlApp Is Nothing Then
Err.Raise ERR_OUTLOOK_NOT_OPEN
End If
sFile = Dir(sPath & "*.eml")
Do Until sFile = ""
ShellExecute 0, "Open", sPath & sFile, "", sPath & sFile, SW_SHOWNORMAL
Wait 2
Set MyInspect = OlApp.ActiveInspector
Set Eml = MyInspect.CurrentItem
Set att = Eml.Attachments
If att.Count > 0 Then
For i = 1 To att.Count
fn = "C:\Users\richard\Desktop\cmds\" & i & "-" & Eml.SenderEmailAddress
att(i).SaveAsFile fn
Next i
End If
sFile = Dir$()
Loop
Set OlApp = Nothing
End Sub
Private Sub Wait(ByVal nSec As Long)
nSec = nSec + Timer
While nSec > Timer
DoEvents
Wend
End Sub