Question : Download File and Make it the Active Window

I need to do the following with VB starting with a command button in Excel.

1. download a file (Excel or PDF)
2. once the file download is complete, I need to make that the active window
3. if Excel, i need to copy data into a new workbook
4. if PDF save the file

How do I...
1. properly open IE and ensure the file download completes
2. make the downloaded file the active window (file name is always different)

Thanks!!

Answer : Download File and Make it the Active Window

Hi,

try this code( credit to Matty Vidas)

change this line for your full web path to the  excel file

urlXls = "your web path to excel file"

this line for the PDF

urlPDF = "your web path to pdf file"

Cheers

Dave

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
Option Explicit
 
 
Function SaveWebFile(ByVal vWebFile As String, ByVal vLocalFile As String) As Boolean
    Dim oXMLHTTP As Object, i As Long, vFF As Long, oResp() As Byte
 
    'You can also set a ref. to Microsoft XML, and Dim oXMLHTTP as MSXML2.XMLHTTP
    Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP")
    oXMLHTTP.Open "GET", vWebFile, False    'Open socket to get the website
    oXMLHTTP.Send    'send request
 
    'Wait for request to finish
    Do While oXMLHTTP.readyState <> 4
        DoEvents
    Loop
 
    oResp = oXMLHTTP.responseBody    'Returns the results as a byte array
 
    'Create local file and save results to it
    vFF = FreeFile
    If Dir(vLocalFile) <> "" Then Kill vLocalFile
    Open vLocalFile For Binary As #vFF
    Put #vFF, , oResp
    Close #vFF
 
    'Clear memory
    Set oXMLHTTP = Nothing
End Function
 
Sub XLScode()
    Dim wb As Workbook
    Dim urlXls As strng, filePath As String
    urlXls = "your web path to excel file"
    filePath = "C:\test.xls"
    SaveWebFile urlXls, filePath
    Set wb = ActiveWorkbook.Open(filePath)
End Sub
 
Sub PDFcode()
    Dim urlPDF As String, filePath As String
    urlPDF = "your web path to pdf file"
    filePath = "C:\test.pdf"
    SaveWebFile urlPDF, filePath
End Sub
Open in New Window Select All
Random Solutions  
 
programming4us programming4us