Automatically download IE VBA files

I am trying to automate uploading a file to a website through Excel VBA, but I am trying to interact correctly with the file upload part of the web page. I read many different web pages and answered both here and other sources on how to do this, but so far my attempts have failed.

Admittedly, I really don't understand how to interact with IE via VBA and various HTML (or other) elements and need some help.

The part of the webpage I'm trying to interact with is as follows:

<LABEL ACCESSKEY=U CLASS=filelabel>Upload a file to current older: <INPUT TYPE="file" NAME="eftupload" CLASS=filefield/></LABEL> <INPUT TYPE="submit" value="Upload" CLASS=submitbutton> 

The VBA that I have so far dealt with is as follows (a bit messy, I know, since I still work differently to make it work) the web element I'm trying to use is at "my URL 2" ,

 Sub File2Web() 'dimension (declare or set aside memory for) our variables Dim objIE As InternetExplorer 'special object variable representing the IE browser Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element 'initiating a new instance of Internet Explorer and asigning it to objIE Set objIE = New InternetExplorer 'make IE browser visible (False would allow IE to run in the background) objIE.Visible = True 'navigate IE to this web page (a pretty neat search engine really) objIE.Navigate "my URL" 'wait here a few seconds while the browser is busy Do While objIE.Busy = True Or objIE.ReadyState <> 4: DoEvents: Loop 'in the userid box put value objIE.Document.getElementById("username").Value = "userID" 'in the password box put value objIE.Document.getElementById("password").Value = "PassWD" 'click the 'go' button objIE.Document.getElementById("loginSubmit").Click 'wait again for the browser Do While objIE.Busy = True Or objIE.ReadyState <> 4: DoEvents: Loop 'navigate IE to this web page 'Upload file using input type=file Dim sFormData As String, d As String Dim FileName As String, FieldName As String FieldName = "File" FileName = "C:\FTP Temp\Test.txt" 'Boundary of fields. Be sure this string is Not In the source file Const Boundary As String = "" 'Get source file As a string. sFormData = GetFile(FileName) FieldName = "frmUpload" 'Build source form with file contents 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 Dim URL As String, FormData As String FormData = d Dim bFormData() As Byte ReDim bFormData(Len(FormData) - 1) bFormData = StrConv(FormData, vbFromUnicode) URL = "my URL 2" objIE.Navigate URL, , , bFormData, "Content-Type: multipart/form-data; boundary=" + vbCrLf 'wait here a few seconds while the browser is busy Do While objIE.Busy = True Or objIE.ReadyState <> 4: DoEvents: Loop 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 

Everything works separately from file upload. When it starts, it just finishes the navigation to the web page not found, and the file does not load, but I do not get any VBA errors, etc.

Any help would be greatly appreciated. I have no other way to upload a file due to network, application, and other working limitations, so I need to interact with this page / form.

Change The full page code is as follows:

 <html> <head><title>sftp.mysite.com</title> <link href="/EFTClient/efthtmladvanced.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="/EFTClient/efthtmladvanced.js"> </script> <script type="text/javascript" src="/EFTClient/Shared/PTCs.js"> </script> </head> <body onload="javascript:init();"><div class=header><H1 CLASS=cwd>sftp.mysite.com - /Input/Applications/UKC Apps/</H1></div> <h3><i>NOTE: Web Transfer Client Access has been disabled for this user or container. Reverting to basic client.</i></h3> <hr /> <FORM NAME="frmUpload" METHOD="POST" ACTION="/Input/Applications/UKC Apps/" ENCTYPE="multipart/form-data"><table border=0 width='90%'> <tr> <td width='70%'> <LABEL ACCESSKEY=U CLASS=filelabel>Upload a file to current folder:&nbsp;<INPUT TYPE="file" NAME="eftupload" CLASS=filefield/></LABEL> <INPUT TYPE="submit" value="Upload" CLASS=submitbutton> </td> <td width='30%' align='right'> <INPUT TYPE='button' value='Change Password' onclick="setChangePassDivPosition();showDiv('ChangePass', true);">&nbsp; <INPUT style="visibility:visible;valign='top';" TYPE='button' value='Log Out' onclick='doLogout(); window.location.reload(true); return false;'> </td> </tr> </table> </FORM><hr /> <pre> <a href="/Input/Applications/;html">[To Parent Directory]</a><br><br> <input type="button" value="Delete" name="btnDelete" id="btnDelete" onClick="javascript:deleteFiles();"><BR> <input type='checkbox' name='folderitem' value="/Input/Applications/UKC Apps/my file.txt" id='1'> <a href='javascript:void(0);' onclick="rename('/Input/Applications/UKC Apps/my file.txt');">Rename</a> 18 Dec 2017 14:02 56955145 <a href="/Input/Applications/UKC Apps/myfile.txt;html">my file.txt</a><BR> </pre><hr> <form name="changePassForm" id="changePassForm" method="post"><div id="ChangePass" class="floatingDiv" style="{ visibility:hidden; }"><table cellpadding="5" cellspacing="0" width="100%" style="border:1px solid black; border-bottom:0px"><tr class="divHeader"><td><table cellpadding=0 cellspacing=0 width="100%%"><tr><td class="formCopy"><b>Change Password</b></td><td class="formCopy" align="right"><a title="Click here to close this dialog" href="javascript:void(0)" onclick="showDiv('ChangePass', false);" style="font-family:verdana; font-size:10px; color:black; text-decoration:none">X</a></td></tr></table></td></tr></table><table cellpadding="5" cellspacing="0" width="100%%" style="border:1px solid black;"><tr><td class="formCopy">Current Password:</td><td class="formCopy"><input type="password" value="" name="oldpass" id="oldpass"></td></tr><tr><td class="formCopy">New Password:</td><td class="formCopy"><input type="password" value="" name="newpass" id="newpass"></td></tr><tr><td class="formCopy">Confirm Password:</td><td class="formCopy"><input type="password" value="" name="confirmpass" id="confirmpass"></td></tr><tr><td class="formCopy">&nbsp;</td><td class="formCopy">&nbsp;</td></tr></table><table cellpadding="5" cellspacing="0" style="border:1px solid black; border-top:2px solid #aaaaaa;" class="divHeader" width="100%%"><tr><td colspan="2" align="right"><input type="button" value="Change Password" onclick="changePassword(this.form);">&nbsp;<input type="button" value="Cancel" onclick="showDiv('ChangePass', false);"></td></tr></table></div></form><br /><br /> <center><font face="arial" size="1" color=#3366CC><em>Powered by the <b>Globalscape EFT Server</b></em></font></center> <br /> </body> </html> 
+5
source share
1 answer

Several security enhancements have been added to Internet Explorer around the file entry control.

  • The value sent to the server will most likely contain a "fake path" so as not to expose the server where this file came from
  • The input is disconnected from the fact that users can enter a value (this was tracked by keyloggers on the pages).
  • The field (or button) must be clicked by the user in relation to the launch of the click event on it using JavaScript

Thus, I would fully expect that both # 2 and # 3 will be blockers from any automated tool that interacts with the field ... if this tool cannot fully imitate a user from the Windows world, by actually clicking a button in the browser, then also select the file from the file selection dialog box.

+1
source

Source: https://habr.com/ru/post/1274190/


All Articles