0

Is there any way to click on Save button when IE.visible=False?

I tried this option that only worked when ie.visible =True Excel VBA to Save As from IE 11 Download Bar

Generic example:

Sub scrape_ex()
   
    
    Dim oHTML_Element As IHTMLElement
    Dim oHTML_Element_signo As IHTMLElement
    Dim oBrowser As InternetExplorer
    Dim ie As Variant
    
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = False
    ie.navigate "URL"
    
        While ie.readyState <> READYSTATE_COMPLETE And ie.readyState <> READYSTATE_LOADED
            DoEvents
        Wend
        
    For Each oHTML_Element In ie.document.getElementsByClassName("class")
        If oHTML_Element.Title = "my_download" Then
              oHTML_Element.Click
              Application.Wait (Now + TimeValue("00:00:03"))' Download Bar starts
                Exit For
        
        End If
    Next
''clicking on Save that work with ie.visible=False ???

Edit:

Clicking the button in the page works perfect, it is not a problem in the web page. I want to click on "Save" when IE download bar starts (with IE.visible=False, so I cant use SendKeys):

enter image description here

mantanam
  • 27
  • 4

1 Answers1

0

If you just use automation to click the button in the page, then it should work, even in the case of ie.Visible = False.

This is my simple test and it works fine:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
</head>
<body>

    <button type="button" id="ieDown" onclick="onSaveInIE()">In IE</button>

    <script>
        function onSaveInIE() {
            var csvWindow = openNewWindowObj();
            csvWindow.document.execCommand('SaveAs', null, 'abc.html');
            csvWindow.close();
        }

        function openNewWindowObj() {
            var newWindowObj = window.open("window.html", "New popup Window", 'height=200,width=150')
            newWindowObj.focus();

            return newWindowObj;
        }
    </script>
</body>
</html>

VBA code:

Sub scrape_ex()
   
    Dim oHTML_Element As IHTMLElement
    Dim oHTML_Element_signo As IHTMLElement
    Dim oBrowser As InternetExplorer
    Dim ie As Variant
    
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = False
    ie.navigate "https://localhost:44315/Index.html"
    
    
        While ie.readyState <> READYSTATE_COMPLETE And ie.readyState <> READYSTATE_LOADED
            DoEvents
        Wend
        
    For Each oHTML_Element In ie.document.getElementsByTagName("button")
        If oHTML_Element.ID = "ieDown" Then
              oHTML_Element.Click
              Application.Wait (Now + TimeValue("00:00:03")) ' Download Bar starts
                Exit For
        
        End If
    Next
End Sub

Note: I am not sure how your page is designed, so I modified some VBA code to get page elements in this test.

Edit:

If you need to use automation to click a button in the `IE Application UIwhen it is not visible, it is impossible. Because the keys will go to the window that has focus. If the window is invisible it cannot have the focus, therefore cannot receive the keys.

Xudong Peng
  • 1,463
  • 1
  • 4
  • 9