Internet Explorer download using Visual Basic - vba

I have a script that opens a URL in Internet Explorer, enters the username and password and clicks Submit. I can't get past the default Internet Explorer open/save prompt. Is this possible?
Call Main
Function Main
Set IE = WScript.CreateObject("InternetExplorer.Application", "IE_")
IE.Visible = True
IE.Navigate "https://www.blahblahblah.com"
Wait IE
With IE.Document
.getElementByID("ctrluser").value = "blahblahblah"
.getElementByID("ctrlpass").value = "blahblahblah"
.getElementByID("frmSubmit").Click
End With
End Function
Sub Wait(IE)
Do
WScript.Sleep 500
Loop While IE.ReadyState < 4 And IE.Busy
End Sub
Also, can you change the default download location to a specific network folder?

Related

VB click button on Website

I'm using the following code to navigate to a web site and then predetermine the drop downs on the page and it works fine...
Private Sub CommandButton4_Click()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "https://postimage.io/"
IE.Visible = True
While IE.busy
DoEvents
Wend
IE.document.All("optsize").Value = "8"
IE.document.All("expire").Value = "0"
End Sub
... however there is a button on the page Choose images i would like my script to click that and in the pop-up enter a address of an image then press open
is this possible? if so could someone please help me achieve it

VBA, get and manage the active web page after browser closing and re-opening

in my Access & VBA application i Have to get and manage a web page of a web order application. Normally, I use the solution indicated in this post, which normally works perfectly:
VBA Open web page, login and get the opened page
But, now I have another old web application to connect with my Access software. In this web application, after login, the web page closes itself and open an other browser session with the first user page. In other words:
1) The user fill the login form with the user credentials
2) After login, the login web page closes the browser
3) An other browser session opens with the web app control panel
If I use the linked solution, I see an error. How can I get the new web page, in the new browser session, which opens it self after login?
Thanks!
EDIT
I have tried the following code (my purpose is set a text value in the web page). But, very strange, it works only if I put a text box in my code: maybe the code must wait some event! How can I solve it?
Dim SWs As SHDocVw.ShellWindows, vIE As SHDocVw.InternetExplorer
Dim winShell As Shell
Dim dt As Date
dt = DateAdd("s", 10, DateTime.Now)
Dim ieA As InternetExplorer
Dim IeDocA As HTMLDocument
Do While dt > DateTime.Now
Set winShell = New Shell
For Each ieA In winShell.Windows
If ieA.LocationURL = sURL Then
ieA.Visible = True
ieA.Silent = True
Do While ieA.Busy: DoEvents: Loop
Do Until ieA.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Set IeDocA = ieA.Document
//only with this msgbox it works, how can I do this w/out it?
MsgBox IeDocA.title
With IeDocA
.getElementsByName("text").value="something"
End With
Set winShell = Nothing
Exit Do
End If
Next ieA
Set winShell = Nothing
DoEvents
Loop
You can Look for the URL in the window. You will need a reference to Microsoft Shell Controls and Automation.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Desc: The Function gets the Internet Explorer window that has the current
' URL from the sURL Parameter. The Function Timesout after 30 seconds
'Input parameters:
'String sURL - The URL to look for
'Output parameters:
'InternetExplorer ie - the Internet Explorer window holding the webpage
'Result: returns the the Internet Explorer window holding the webpage
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function GetWebPage(sUrl As String) As InternetExplorer
Dim winShell As Shell
Dim dt As Date
dt = DateAdd("s", 30, DateTime.Now)
Dim ie As InternetExplorer
Do While dt > DateTime.Now
Set winShell = New Shell
'loop through the windows and check the internet explorer windows
For Each ie In winShell.Windows
'check the URL
If ie.LocationURL = sUrl Then
'set some properties
ie.Visible = True
ie.Silent = True
Set GetWebPage = ie
'loop while IE is busy
Do While ie.Busy
DoEvents
Loop
Set winShell = Nothing
Exit Do
End If
Next ie
Set winShell = Nothing
DoEvents
Loop
End Function

VBA - how to use google form on IE.navigate with process in the background

I'm currently using the following code to open an IE instance and navigate a complete-with-answers google form link. By accessing this link, the google form is immediately answered by the "entry.1102259784=XXXX" strings.
Private Sub Workbook_Open()
Application.ScreenUpdating = False
Dim IE As Object
Call Usernames
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
IE.Silent = True
IE.Navigate Cells(7, 1)
While IE.Busy
DoEvents
Wend
IE.Quit
Set IE = Nothing
End Sub
Its quite simple and works well (hidden and fast) except for this particular instance (google forms), as the IE opens and just stands there with the "Your form has been submitted" webpage on display.
The idea is to submit information about who is opening the file and when to a google spreadsheet.
I would appreciate any help on this matter.
Thank you very much!

Need to use VBA to select an option from a drop down list in Internet Explorer

I have successfully got my code to open IE, navigate to the webpage I need, and login. I now need to select an option from a drop down list - please see the following html code:
html code for the list
How do I select the "TPS Managed Conservative - Dec 11" option from the dropdown.
My code so far:
Sub Strategic_Alpha_Monthly_Pivots_1_MASTER()
' open IE, navigate to the desired page and loop until fully loaded
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
my_url = "http://analytics.financialexpress.net/login.aspx"
With ie
.Visible = True
.navigate my_url
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
End With
' Input the userid and password
ie.document.getElementById("txtPassword").Value = "xxxxx"
' Click the "Search" button
ie.document.getElementById("btnAction").Click
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
ie.document.getElementById("ListPortfolio").Select
End Sub
selectedIndex or Value could be used. According to the screenshot the value 983678630 can be used. HTH
If Not VBA.IsNull(ie.document.getElementById("ListPortfolio")) Then
Dim htmlSelect
Set htmlSelect = ie.document.getElementById("ListPortfolio")
' htmlSelect.selectedIndex = 6
htmlSelect.Value = 983678630
Else
MsgBox "Element 'ListPortfolio' was not found", vbExclamation
End If

VBA to open URL, wait for 5 seconds, then open another URL

I have a webpage I want to open (URL is generated out of cell values in Excel), but going to that URL directly requires logging in to open. But if I first open the mainpage of the server, I have automatic login, and then I'm able to open the first URL without the need for user/pass.
So far I have this code, which opens IE with both URLs at the same time, in the same window, but different tabs, exactly as I want it to do, except URL2 requires the login.
To get around the login, I would like to add a pause between the navigate and navigate2, so the first page can complete loading before the second URL opens.
Can anyone help me with this?
Edit:
I have tried the suggestions from below, but it still needs the login. I have tried another solution, which is not optional, but it works. It consists of two buttons, running different macros, where the first one opens the main page to get the login, and the second one opens the next URL.
I have written them as follows:
First one:
Sub login()
Dim IE As Object
Const navOpenInNewTab = &H800
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://www.example.mainpage.com"
End Sub
Second one:
Sub search()
Dim IE As Object
Const navOpenInNewTab = &H800
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate2 "http://www.example.mainpage.com" & Range("w1").Cells.Value & Range("W2").Cells.Value, CLng(navOpenInNewTab)
End Sub
Is it possible to have a third macro running the other two with a delay between them?
Original code:
Sub open_url()
Dim IE As Object
Const navOpenInNewTab = &H800
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://www.example.mainpage.com"
'here I would like to add a pause for 5 seconds
IE.Navigate2 "http://www.example.mainpage.com" & Range("w1").Cells.Value & Range("W2").Cells.Value, CLng(navOpenInNewTab)
End Sub
Maybe it would be better to wait until the first page is fully loaded:
IE.Navigate "http://www.example.mainpage.com"
Do While IE.Busy Or Not IE.readyState = IE_READYSTATE.complete: DoEvents: Loop
IE.Navigate2 "http://www.example.mainpage.com" & Range("w1").Cells.Value & Range("W2").Cells.Value, CLng(navOpenInNewTab)
Note that the ReadyState enum READYSTATE_COMPLETE has a numerical value of 4. This is what you should use in the case of late binding (always the case in VBScript).
Do you mean something like:
Application.Wait(Now + TimeValue("00:00:05"))