this might seem a question asked many times, however I was not able to find one that fits with my requirement.
The macros I'd need to create will be used for many users and everybody must use their standard company credentials. I cannot create any userform asking for that or hard code anything as it would breach the company security rules.
Nowadays, I'm using Webbrowser control in combination with DownloadURLtoFile function or winHTTReq to download files. Users launch a userform with a Webbrowser, enter their credentials, then I test if the login was successful and hide the userform. Then while I surf to the right URL file, I scrape some other useful information and finally download the report smoothly. Both file download methods use the loged-in webcontrol session already in place and don't need anything else.
Unfortunately, the behavior with IE object is not the same, when I run any of both methods after the user login successfully and I hide the screen, the file downloaded has the http code asking for a user id and password.
Does anybody know if it's possible to bind any of these two file download method to use an IE object session?
Sorry for the long explanation, hope I've been clear with my request.
P.S.: If you ask why I'm moving to IE if Webbrowser control works is because, I need to scrape different pages with java script containers and it's way more easier with IE.
Hello @Deepak-MSFT to to your question, the answer is No, the file I've got when the session is not logged comes from the right page. Here you go the macros which works correctly:
Sub Login()
URL = "myapplication.com"
UserForm1.WebBrowser1.navigate URL '<- This request is redirected to the global login page.
'Wait until page is fully loaded
Do While UserForm1.WebBrowser1.ReadyState = 4: DoEvents: Loop
Do Until UserForm1.WebBrowser1.ReadyState = 4: DoEvents: Loop
While UserForm1.WebBrowser1.Busy
DoEvents
Wend
UserForm1.show
End Sub
Private Sub WebBrowser1_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
'This Macro is located in the UserForm1. Will leave Webbrowser opened until the user
'loggin succesffuly. After that I hide the userform and continue with the download.
'Check if appls is logged and hide the browser
LoginStatement = "Welcome to the Intranet" '<= String which appears when appls is logged
Document = WebBrowser1.Document.DocumentElement.innerText
pos1 = InStr(Document, LoginStatement)
If pos1 Then
Me.hide
End If
End Sub
Sub Download()
'Main procedure
Dim myURL As String: myURL = "myapplication.com"
Call Login
sFile_Name = "test.csv"
sPath = "c:\temp"
sFile_Path = sPath & "\" & sFile_Name
Dim winHttpReq As Object
Set winHttpReq = CreateObject("Microsoft.XMLHTTP")
winHttpReq.Open "GET", myURL, False
winHttpReq.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
On Error GoTo URL_Unreachable
winHttpReq.Send
On Error GoTo Wrong_Directory
If winHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write winHttpReq.ResponseBody
oStream.SaveToFile sFile_Path, 2 ' 1 = no overwrite, 2 = overwrite
oStream.Close
Else
MsgBox "Something went wrong... file couldn't be downloaded"
End If
Set winHttpReq = Nothing
Exit Sub
URL_Unreachable:
MsgBox "URL unreachable. Please check your connection and try again"
Application.StatusBar = False
Set winHttpReq = Nothing
End
Wrong_Directory:
Application.StatusBar = "Reporting path directory inaccessible"
MsgBox "The Reporting path: " & sPath & " is not accesible or you don't have enough permisions." & _
vbCrLf & vbCrLf & "Please update it and try again"
Application.StatusBar = False
Set winHttpReq = Nothing
End
End Sub
Then, if I replace the previous login macro by the following, the file is not downloaded as Excel does not see any open session established.
Sub login()
Dim HTMLDoc As MSHTML.HTMLDocument
Dim URL, LoginStatement As String: URL = "myapplication.com"
Dim pos1 As Integer: pos1 = 0
LoginStatement = "Welcome to the Intranet" '<= String which appears when appls is logged
Set IE = New InternetExplorerMedium
With IE
.Navigate URL: .visible = True
Do While .ReadyState <> READYSTATE_COMPLETE Or .Busy
Loop
On Error Resume Next
Set HTMLDoc = .Document.DocumentElement.innerText
If (pos1 = InStr(Document, LoginStatement)) Then
.visible = False
Exit Sub
End If
Wend
End Sub