Mat*_*rts 5 html excel vba excel-vba
我试图通过Excel VBA自动将文件加载到网站,但我正在努力与网页的文件加载部分正确交互.我已经阅读了许多不同的网页,并在这里和其他来源上解答如何做到这一点,但到目前为止我的尝试都失败了.
不可否认,我并不是真的通过VBA和各种HTML(或其他)元素与IE进行交互,需要一些帮助.
我正在努力与之互动的网页部分是这样的:
<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>
Run Code Online (Sandbox Code Playgroud)
到目前为止,我设法编写的VBA如下(我知道有点乱,因为我仍在以各种方式使其工作)我试图使用的web元素位于"我的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
Run Code Online (Sandbox Code Playgroud)
一切都与文件加载有关.当它运行时,它最终导航到找不到网页的网页,文件没有上传,但我没有收到任何VBA错误等.
任何帮助将不胜感激.由于网络,应用程序和其他工作限制,我无法上传文件,因此我需要与此页面/表单进行交互.
编辑:整页代码如下......
<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: <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);">
<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"> </td><td class="formCopy"> </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);"> <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>
Run Code Online (Sandbox Code Playgroud)
Internet Explorer 围绕文件输入控件添加了多项安全增强功能。
因此,我完全希望 #2 和 #3 都能成为与该领域交互的任何自动化工具的拦截器...除非该工具可以完全模仿 Windows 世界中的用户,实际上单击浏览器中的按钮,然后也从文件选择器对话框中选择文件。