Net download through VBA - wrong extension?

corquando

Board Regular
Joined
Jan 8, 2009
Messages
82
Greetings, Board.

I use this:

Rich (BB code):
Sub Downlodez()
 
    Dim FileNum As Long
    Dim FileData() As Byte
    Dim MyFile As String
    Dim WHTTP As Object
 
            On Error Resume Next
                Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5")
                If Err.Number <> 0 Then
                    Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
                End If
            On Error GoTo 0
 
            MyFile = "http://thatsite.com/sites/NoBigFile.xlsm"
 
            WHTTP.Open "GET", MyFile, False
            WHTTP.Send
            FileData = WHTTP.ResponseBody
            Set WHTTP = Nothing
 
            If Dir("G:\Huge\Medium\Small", vbDirectory) = Empty Then _
                    MkDir "G:\Huge\Medium\Small"
 
            FileNum = FreeFile
            Open "G:\Huge\Medium\Small\NoBigFile.xlsm" For Binary Access Write As #FileNum
                Put #FileNum, 1, FileData
            Close #FileNum
 
 
End Sub

And it downloads the files I specify quite nicely.

However, when I attempt to open the downloaded files with a "Workbooks.Open" commend, I get a Runtime 1004 error sayng the file format or extension is not valid. I am naming the exact file and extension in the dowload code, as you can see. It also advises to insure the file has not been corrupted and that the extension matches the format. Unknown how to do that . . .

When I manually download the file it opens just fine so, I am corn-fused.

The file does contain macros and Pivot Tables, but the process doesn't demand links, as the macros obtain the data necessary through simple copy-n-paste commands.

What's the incredibly obvious part I can't see?

Thanks.
 
Last edited:

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Yeah - that's a pretend name. My company has problems with the public at large accessing their actual internal files. Can't imagine why . . .

May hap's the fact it's a SharePoint site may be important, though. So, in fact, thanks for asking. That small tidbit might just be the crux of the biscuit.
 
Upvote 0
Consider:

Code:
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" _
                                           (ByVal pCaller As Long, _
                                            ByVal szURL As String, _
                                            ByVal szFileName As String, _
                                            ByVal dwReserved As Long, _
                                            ByVal lpfnCB As Long) As Long

Sub DownloadCPIFile()
    
    On Error Resume Next
    URLDownloadToFile _
                    0, _
                    "http://thatsite.com/sites/NoBigFile.xlsm", _
                    "C:/NoBigFile.xlsm", 0, 0
                    
    If Err.Number <> 0 Or Dir("C:\NoBigFile.xlsm") = "" Then
        MsgBox "Downloading the file produced an error: " & Err.Description, vbCritical, "Status"
    Else
        MsgBox "Downloading the file was a success.", vbInformation, "Status"
    End If
    
End Sub
 
Upvote 0
I tried the second one first, because I liked the simplicity of just opening the book from the source, but it was much too reasonable and logical, so it didn't work.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
But the first one did. All I had to do was disable the UpdateLinks on Open after downloading and viola! So, chalk up another reason that this is the Best. Website. Ever.

Thanks bzillions!<o:p></o:p>
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,618
Messages
6,179,917
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top