0

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.

Wesley
  • 190
  • 9
  • Where is `oBrowser` declared? If I create a InternetExplorer object I can access the hWnd property with it visible and not visible. – NickSlash Mar 15 '20 at 16:19
  • ok I got it to work. Now the problem is that my filters are still active when I run the macro again which results in failing from the macro because is doesnt find the id anymore. Is there a way around this? I also need to save as directly in the folder – Wesley Mar 15 '20 at 18:37
  • 1
    If the link's you harvest are direct links, and there are no security requirements (login, session cookies etc) then you might be better using xmlhttprequests ( something like https://stackoverflow.com/a/59494513/212869 ) – NickSlash Mar 15 '20 at 22:38
  • I went with this approach indeed, thanks :) No I just have to see that the IE page resets every time because I can only run the macro once. The second time I run the macro, some filters are still active from the first run and then my actions via the macro will fail... – Wesley Mar 15 '20 at 23:07

1 Answers1

1

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
NickSlash
  • 4,758
  • 3
  • 21
  • 38
  • Yes , then there is an error on the ```hWnd = oBrowser.hWnd``` line. It gives hWnd as 0. But this option would only works if I set the browser to visible. I would prefer to work with visible=false – Wesley Mar 15 '20 at 15:33