Wesley
Wesley

Reputation: 300

Click "Save as" in IE using excel VBA

I have following code to store a list of links:

Dim allHREFs As New Collection
Dim TableName As String

    For iRow = 0 To 20
        TableName = "docTypeForm:documentTbl:" & iRow & ":j_idt250"
        On Error GoTo DownloadFiles
        Set allLinks = obJIE.Document.getElementById(TableName).getElementsByTagName("a")
            For Each link In allLinks
                allHREFs.Add link.href
            Next link
    Next iRow

DownloadFiles:
    For j = 1 To allHREFs.Count
        obJIE.Navigate Url + allHREFs(j)
        'Application.Wait (Now + TimeValue("0:00:04"))
   Download_default
    Next

I search a lot of websites and they always lead to the same sort of code. I am until the point that the file has to be downloaded via the pop up window you get(in IE). Preferably 'save as' so it will save in the correct folder. For the code below I get an error on the hWnd = find window. The error that I have is sub of function not defined. I feel like it has something to do with the LongPtr but when I search it, it looks like it should work. Any suggestions about this error? Or maybe another way to save or download the files from a href link?

Private Sub Download_Default()

    Dim AutomationObj As IUIAutomation
    Dim WindowElement As IUIAutomationElement
    Dim Button As IUIAutomationElement
    Dim hWnd As LongPtr

    Set AutomationObj = New CUIAutomation

    Do While oBrowser.Busy Or oBrowser.readyState <> 4: DoEvents: Loop
    Application.Wait (Now + TimeValue("0:00:05"))
    hWnd = oBrowser.hWnd
    hWnd = FindWindowEx(hWnd, 0, "Frame Notification Bar", vbNullString)
    If hWnd = 0 Then Exit Sub

    Set WindowElement = AutomationObj.ElementFromHandle(ByVal hWnd)
    Dim iCnd As IUIAutomationCondition
    Set iCnd = AutomationObj.CreatePropertyCondition(UIA_NamePropertyId, "Save")

    Set Button = WindowElement.FindFirst(TreeScope_Subtree, iCnd)
    Dim InvokePattern As IUIAutomationInvokePattern
    Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
    InvokePattern.Invoke

End Sub

Update: I tried to copy paste the links into google chrome because it will download automatically when the link is clicked but this also doesn`t work. It opens chrome but then the server is not accessible.

Upvotes: 0

Views: 409

Answers (1)

NickSlash
NickSlash

Reputation: 5077

Have you included the API Definitions?

You need to have the following line in a module to be able to use FindWindowEx

Public Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hwndParent As Long, _
    ByVal hwndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As Long

Upvotes: 1

Related Questions