0

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




  • It can be possible that you are navigating to some other page while downloading the file. That is not using the same IE document object and that's why it is again asking for credentials. We need the sample code to check what actually causing this issue. If possible for you then you can try to provide the sample code. We will try to check it and try to provide further suggestions. Thanks for your understanding. – Deepak-MSFT May 28 '20 at 03:14
  • Hi @Deepak-MSFT, I've edited my question and answer to you there. Thx. – Mike Hillflag May 29 '20 at 11:21
  • Can you please inform us, how do you manually download the file? Can file downloaded by clicking any button? I can see that you are using XMLHTTP and ADODB stream objects and you are not using Internet Explorer object. Let us know about the manual steps to download that file. – Deepak-MSFT Jun 01 '20 at 09:06
  • Yes, there is a link to download the file and Yes I'm not using IE object , but webbrowser control (please re-read my question)., that's the whole point, how to use any downloading method I listed using IE object without hard-coding credentials or use any userform for that. – Mike Hillflag Jun 01 '20 at 14:33
  • Here I want to inform you that the IE browser shows the download popup whenever a user downloads any file. It is not possible to automate that popup or disable that popup using VBA. So I think that you will get stuck on this point as user needs to manually click on the popup to download the file. I suggest you can try to check the Selenium IE web driver. It has a proper way to handle this scenario. Ref: https://www.selenium.dev/downloads/ – Deepak-MSFT Jun 01 '20 at 15:11
  • Two comments:1.- I'm afraid you are wrong when you say IE save pop-ups windows can't be automated via VBA. (https://stackoverflow.com/questions/32145437/controlling-ie11-do-you-want-to-open-save-dialogue-window-buttons-in-vba) 2.- That is not my point at all, Thanks anyway for trying. – Mike Hillflag Jun 01 '20 at 18:18
  • If you check that thread properly then you can notice that solution is using the third-party library to automate the UI. I understand your point but whenever IE download the file it will show that popup. I suggest you try to test that using IE VBA automation to check that issue. It may help you to get detailed idea about it. – Deepak-MSFT Jun 02 '20 at 08:37

0 Answers0