Need Help Creating Programmatic Post in VBA. Sample code that works in a browser is attached.

Hello,

The attached code is a working example of how to upload a file to Ebay's file exchange server.
All you need to do (after adding your token) is open the code with a browser, navigate to a file by clicking the CHOOSE FILE button, and then upload that file to Ebay by clicking the UPLOAD button.

I would like to create a routine in MS Access 2000 VBA that does the same thing when I pass it a file path and name.

I'm using MS Access 2000 on an XP SP3 platform.

Lets say the name of the full path and name of the file I want to upload is stored in strFileName.  Can anyone help?

Thanks
SampleEBAYpostfromtechsupport.html
pcalabriaAsked:
Who is Participating?
 
Robert ShermanOwnerCommented:
Was surprised this didn't have an answer yet... then I did a google search for doing HTTP POST uploads via VBA and was even more surprised by how little this has been covered.  

Below is untested code, but it might be enough to get you there.  

The one thing I did not work out is dealing with the content-type of the file you are uploading, which I noted in a comment in the code.   If it's always going to be a specific file-type, you can just hard code that in where the code currently is using "text/xml", but a better solution would be to either detect the type automatically, or use a more generic binary content type.


Function FormUpload(strFileName As String) As String
    
    Dim WinHttpReq As WinHttp.WinHttpRequest
    Dim strBody As String
    Dim strFile As String
    Dim aPostBody() As Byte
    
    Dim bound As String
    Dim boundSeparator As String
    Dim boundFooter As String
    
    Dim strPostToURL As String
    strPostToURL = "http://bulksell.ebay.com/ws/eBayISAPI.dll?FileExchangeUpload"
    
    Dim strMyToken As String
    strMyToken = "MY-TOKEN-GOES-HERE"   ' set accordingly
    
    ' Here is where some additional thought is needed..
    Dim strContentType As String
    strContentType = "text/xml"
    
    bound = "AaB03x"
    boundSeparator = "--" & bound & vbCrLf
    boundFooter = "--" & bound & "--" & vbCrLf
    
    Set WinHttpReq = New WinHttpRequest
    WinHttpReq.Open "POST", strPostToURL, False
    
    WinHttpReq.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & bound
    strBody = boundSeparator

    strFile = GetFile(strFileName)


    strBody = strBody & "Content-Disposition: form-data; name=""token""" & vbCrLf & vbCrLf & strMyToken & vbCrLf & boundSeparator
    strBody = strBody & "Content-Disposition: form-data; name=""" & "fname" & """; filename=""" & strFileName & """" & vbCrLf & _
        "Content-Type: " & strContentType & vbCrLf & vbCrLf & strFile & vbCrLf

    strBody = strBody & boundFooter
    
    'convert to byte array
    aPostBody = StrConv(strBody, vbFromUnicode)

    WinHttpReq.send aPostBody
    
    ' If posting asynchronously, you'll need to wait for the response to come back using something like the following: 
    ' Do Until WinHttpReq.Status = 200
    '     DoEvents
    ' Loop
    
    FormUpload = WinHttpReq.responseText
    Set WinHttpReq = Nothing
End Function


'Returns file contents As a binary data
Function GetFile(FileName)
  Dim Stream: Set Stream = CreateObject("ADODB.Stream")
  Stream.Type = 1 'Binary
  Stream.Open
  Stream.LoadFromFile FileName
  GetFile = Stream.Read
  Stream.Close
End Function

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.