简体   繁体   中英

Upload Excel xlsm file to php script using VBA

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.

You won't need to base64 encode anything. Follow the sample code you have found but before preparing the file (before '---prepare body comment) just add your other texts (form entries) like this

sPostData = sPostData & "--" & STR_BOUNDARY & vbCrLf & _
     "Content-Disposition: form-data; name=""" & Name & """"
sPostData = sPostData & vbCrLf & vbCrLf & _
     pvToUtf8(Value) & vbCrLf

... where Name and Value are the designed name and the actual text that you want to include in service request. For the function pvToUtf8 implementation take a look at this Google Cloud Print service connector . The snippet above is taken from pvRestAddParam function of the same connector.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM