3

I am still a beginner with VBA, so if you need further explanation or I am not describing my problem correctly please let me know.

I am trying to achieve the following: Upload a file from my computer to a website (You can only try to upload if you login, so sadly I cannot share a link)

In order to achieve this I need to do three things: 1) Click the "Upload" Button 2) Insert the Filepath into the search field of the PopUp Window 3) Click the "Open" Button

The website looks like this: enter image description here

The PopUp Window looks like this: enter image description here

The HTML code of the upload field is the following:

<div class="button-wrapper">
    <input class="design-file-input" type="file">
    <a class=" button prio1" href="javascript:void(0);">Design hochladen</a>
</div>

I guess there might be two solutions two my problem, however, I am not able to realize my plans.

IDEA 1

Somehow get the filepath into the input field and the page to download it Therefore I tried the vba following VBA codes:

objIE.document.getElementsByClassName("design-file-input")(0).Value
objIE.document.getElementsByClassName("design-file-input")(0).innerText

And then try to somehow make the website submit my entry.

IDEA 2

Click the "Design Hochladen" Button.

objIE.document.getElementsByClassName("button-wrapper")(0).Click

But then the PopUp window comes up and I don't know how to control it with VBA

I am happy to hear and try your suggestions!! If you need any further details, just let me know! Thank you so much if you can give me any advice

braX
  • 11,506
  • 5
  • 20
  • 33
Websitewichtel
  • 125
  • 3
  • 10
  • I am guessing you actually tried passing a value to objIE.document.getElementsByClassName("design-file-input")(0).Value for example? – QHarr Mar 01 '18 at 22:05
  • I tried the entering the following: objIE.document.getElementsByClassName("design-file-input")(0).Value = "C:\Filepath" but nothing happens after that. – Websitewichtel Mar 01 '18 at 22:12
  • 2
    You cannot script the value of a "file"-type input, for obvious security reasons. – Tim Williams Mar 01 '18 at 22:52
  • Did you give url? I cannot see it. – S Meaden Mar 01 '18 at 22:56
  • Related, https://stackoverflow.com/questions/8659808/how-does-http-file-upload-work – S Meaden Mar 01 '18 at 22:57
  • Related, https://stackoverflow.com/questions/10954293/how-to-send-files-via-http-post-with-excel-using-vba#answer-11943574 – S Meaden Mar 01 '18 at 23:03
  • Thanks for the related articles, I think the mentioned websites use a different way to upload the file. I believe I somehow have to trigger or use the href "javascript:void(0);" in order to submit the file. However I wasn't able to achieve this – Websitewichtel Mar 02 '18 at 16:09
  • Maybe this can help you: https://stackoverflow.com/questions/49188317/excel-vba-change-the-value-of-a-javascript-function-no-parameters/49189294#49189294. It controls the filedialog that pops up. – Alex de Jong Mar 16 '18 at 22:27
  • I'm posting this for future seekers. This worked for me: https://stackoverflow.com/a/45382080/4037170 – mits May 30 '20 at 08:55

1 Answers1

1

Directly assigning the file path to the value of that specific HTML element does not work. A while ago, I had the same issue (automatically passing a file to an upload file dialog). After a long googling session, I found following solution. Unfortunately, I could not find the link from where I took this answer. In case I come accross the website, I will share the link with you:

Dim FilePath As String
Dim FileName As String
FilePath = Environ$("temp") & "\"
FileName = "test_file_for_upload" & ".xlsx"

UploadFile DestURL, FilePath & FileName, "file" 'Usage

'******************* upload - begin
'Upload file using input type=file
Public Sub UploadFile(DestURL As String, FileName As String, _
  Optional ByVal FieldName As String = "File")
  Dim sFormData As String, d As String

  'Boundary of fields.
  'Be sure this string is Not In the source file
  Const Boundary As String = "---------------------------0123456789012"

  'Get source file As a string.
  sFormData = GetFile(FileName)

  'Build source form with file contents
  d = "--" + Boundary + vbCrLf
  d = d + "Content-Disposition: form-data; name=""" + FieldName + """;"
  d = d + " filename=""" + FileName + """" + vbCrLf
  d = d + "Content-Type: application/upload" + vbCrLf + vbCrLf
  d = d + sFormData
  d = d + vbCrLf + "--" + Boundary + "--" + vbCrLf

  'Post the data To the destination URL
  IEPostStringRequest DestURL, d, Boundary
End Sub

'sends URL encoded form data To the URL using IE
Sub IEPostStringRequest(URL As String, FormData As String, Boundary As String)
  'Create InternetExplorer
  Dim WebBrowser: Set WebBrowser = CreateObject("InternetExplorer.Application")

  'You can uncoment Next line To see form results
  WebBrowser.Visible = True

  'Send the form data To URL As POST request
  Dim bFormData() As Byte
  ReDim bFormData(Len(FormData) - 1)
  bFormData = StrConv(FormData, vbFromUnicode)

  WebBrowser.navigate URL, , , bFormData, _
    "Content-Type: multipart/form-data; boundary=" + Boundary + vbCrLf

  Do While WebBrowser.Busy
'    Sleep 100
    DoEvents
  Loop
  'WebBrowser.Quit
End Sub

'read binary file As a string value
Function GetFile(FileName As String) As String
  Dim FileContents() As Byte, FileNumber As Integer
  ReDim FileContents(FileLen(FileName) - 1)
  FileNumber = FreeFile
  Open FileName For Binary As FileNumber
    Get FileNumber, , FileContents
  Close FileNumber
  GetFile = StrConv(FileContents, vbUnicode)
End Function
'******************* upload - end

The third argument "file" denotes the ID of the HTML element which needs to be triggered. Hope this solution workds for you as well