Results 1 to 8 of 8

Thread: VBA to open file from HTTPS location

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2003
    Location
    Newark-on-trent, Nottingham
    Posts
    243

    VBA to open file from HTTPS location

    I "borrowed" this from a post online which appears to address the issue i have opening files from our internal https site:

    Code:
    Private Sub CommandButton21_Click()
    Dim FileNum As Long
    Dim FileData() As Byte
    Dim WHTTP As Object
    
    mainUrl = "https://mycompany.com/"
    fileUrl = "https://mycompany.com/sdd_template.docx"
    filePath = "C:\Users\me\Desktop\sdd_template.docx"
    
    myuser = "[email protected]"
    mypass = "******"
    
    strAuthenticate = "start-url=%2F&user=" & myuser & "&password=" & mypass & "&switch=Log+In"
    
    Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
    
    WHTTP.Open "POST", mainUrl, False 'WHTTP.Open "POST", fileUrl, False
    WHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    WHTTP.Send strAuthenticate
    
    'Then you have to GET direct file url
    WHTTP.Open "GET", fileUrl, False
    WHTTP.Send
    
    FileData = WHTTP.ResponseBody
    Set WHTTP = Nothing
    
    'Save the file
    FileNum = FreeFile
    Open filePath For Binary Access Write As #FileNum
        Put #FileNum, 1, FileData
    Close #FileNum
    
    MsgBox "File has been saved!", vbInformation, "Success"
    
    End Sub
    It executes perfectly however the downloaded Word document is corrupt and cannot be used.

    Anybody tried similar and succeeded?

    Thanks

    Steve

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA to open file from HTTPS location

    if you do not open the fie and put filedata is the downloaded file still corrupt?

    you can not really just write data to a word document, as a text file, you should be using Word automation, to do that, or possibly xml
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2003
    Location
    Newark-on-trent, Nottingham
    Posts
    243

    Re: VBA to open file from HTTPS location

    Thanks, at this stage I am not attempting to write anything to the document just download it from the HTTPS location to the local machine.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA to open file from HTTPS location

    try using urldownloadtofile api instead of the above
    there are many examples in this and the vb6 forums
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2003
    Location
    Newark-on-trent, Nottingham
    Posts
    243

    Re: VBA to open file from HTTPS location

    Tried this...
    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
     
    Private Sub CommandButton21_Click()
    
    fileurl = "https://#####.com"
    
    myuser = "######"
    mypass = "#####"
    myResult = URLDownloadToFile(0, fileurl, "C:\Users\" & Environ("username") & "\Desktop\" & "cdm.docx", 0, 0)
    
    If myResult <> 0 Then
        MsgBox "Error downloading " & mylink & Chr(10) & Error(myResult)
    Else
        MsgBox "File has been downloaded"
    End If
    
    End Sub
    Downloads but 'Could not open file as its corrupt' error when trying to open it from my desktop.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA to open file from HTTPS location

    i tested your code to download a docx worked fine, document opened OK

    can you post a sample docx to test with, not sure what other problem can be occurring, could the file be encrypted?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 2003
    Location
    Newark-on-trent, Nottingham
    Posts
    243

    Re: VBA to open file from HTTPS location

    I believe my issue is down to the fact the file location is behind our company Single Sign On authentication. But i cannot find a way to bypass this currently

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA to open file from HTTPS location

    can you use ftp?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width