Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I would like to upload an Excel xlsm file to a php script from VBA. I found the following code:

Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")

Dim strURL As String
Dim StrFileName As String
Dim FormFields As String
Dim path As String
Dim name As String

   StrFileName = "c:\temp\ctc1output.xls"
   strURL = "http://www.tri-simulation.com/P3/"


   WinHttpReq.Open "POST", strURL, False


   ' Set the header
  WinHttpReq.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"


   FormFields = """fileulp=" & StrFileName & """"
   FormFields = FormFields + "&"
   FormFields = FormFields + """sfpath=P3"""

   WinHttpReq.Send FormFields


   ' Display the status code and response headers.
   MsgBox WinHttpReq.GetAllResponseHeaders
   MsgBox WinHttpReq.ResponseText
  1. Should I handle the file as a binary file or another type of file?
  2. Can I upload the file while it is still open (I want to upload the file from which the VBA is running from)?

I am not sure if I'm on the right track. I'm also not sure about what the headers and form fields should be.

Thx for any help.

share|improve this question
1  
HI, from your code, you are only sending the file name to server. You need to somehow convert the file into binaries and include them in your POST message. – Larry Mar 14 at 9:33
Hi, I can see that the file has to be uploaded as a binary file. I'm using the code from here. It also seems i dont need to make a copy. My problem now is that I need send some text with the post. Will that be a seperate request or can I include it somehow with the first one? Sorry if I seem redundant, but this is my first time with this topic. – Albertus Mar 14 at 10:53
2  
you'll have to simulate a browser assembling a POST request and base64-encode your file, which can be done but is a bit tedious. another option would be to run curl which only needs the filename and the upload form parameters (the curl executable can be run from vba). – collapsar Mar 15 at 19:37

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.