VBA - Go to website and download file from save prompt

前端 未结 1 883
感情败类
感情败类 2020-12-30 17:30

I\'ve been spending the last few hours trying to figure out how to save a file onto the computer using VBA. The code template below that I found on another forum seems promi

相关标签:
1条回答
  • 2020-12-30 18:07

    Try below code :

    Copied from here (Not tested)

    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    
    Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    
    Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Long) As Long
    
    Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
    
        Private Sub Save_Over_Existing_Click_Yes()
    
            Dim hWnd As Long
            Dim timeout As Date
    
            Debug.Print "Save_Over_Existing_Click_Yes"
    
            'Find the Download complete window, waiting a maximum of 30 seconds for it to appear.  Timeout value is dependent on the
            'size of the download, so make it longer for bigger files
    
            timeout = Now + TimeValue("00:00:30")
            Do
                hWnd = FindWindow(vbNullString, "Save As")
                DoEvents
                Sleep 200
            Loop Until hWnd Or Now > timeout
            Debug.Print "   Save As window "; Hex(hWnd)
    
            If hWnd Then
                'Find the child Close button
    
                hWnd = FindWindowEx(hWnd, 0, "Button", "&Yes")
                Debug.Print "   Yes button "; Hex(hWnd)
            End If
    
            If hWnd Then
    
                'Click the Close button
    
                SetForegroundWindow (hWnd)
                Sleep 600  'this sleep is required and 600 miiliseconds seems to be the minimum that works
                SendMessage hWnd, BM_CLICK, 0, 0
            End If
        End Sub
    
    0 讨论(0)
提交回复
热议问题