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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.