Html Excel VBA自动IE文件加载

Html Excel VBA自动IE文件加载,html,vba,excel,Html,Vba,Excel,我试图通过ExcelVBA自动将文件加载到网站,但我很难与网页的文件加载部分正确交互。我已经阅读了许多不同的网页和答案都在这里和其他来源如何做到这一点,但到目前为止,我的尝试是失败的 诚然,我并不太了解如何通过VBA和各种HTML或其他元素与IE交互,需要一些帮助 我努力与之互动的网页部分如下: <LABEL ACCESSKEY=U CLASS=filelabel>Upload a file to current older: <INPUT TYPE="file" NAME="

我试图通过ExcelVBA自动将文件加载到网站,但我很难与网页的文件加载部分正确交互。我已经阅读了许多不同的网页和答案都在这里和其他来源如何做到这一点,但到目前为止,我的尝试是失败的

诚然,我并不太了解如何通过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>
到目前为止,我设法编写的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
除了文件加载之外,一切都正常工作。当它运行时,它只是导航到一个网页未找到屏幕,文件没有上传,但我没有得到任何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:&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>

Internet Explorer围绕文件输入控件添加了一些安全增强功能

发送到服务器的值可能包含一个伪路径,以便不向文件来源的服务器公开 用户无法输入键盘记录者在页面上窥探到的值,因此输入被禁用 用户需要单击字段或按钮,而不是使用JavaScript触发单击事件
因此,我完全希望2和3都是任何与现场交互的自动化工具的拦截器。。。除非该工具能够完全模仿Windows世界中的用户,实际单击浏览器中的按钮,然后从“文件选择器”对话框中选择文件。

我可以想象,该网页可能有一些前端来准备上传文件。可能是某些Javascript事件由于您对DOM元素的操作而未触发。如果不深入研究,我猜。。。例如,这通常是由鼠标点击事件触发的吗?只是一些需要调查的东西,直到有时间处理这个问题的人来帮忙,我很害怕在网页中,当你点击元素时,它会自动打开一个文件浏览器窗口,当你选择你的文件时,它会填充一个框,但是你不能点击后面的并手动键入任何内容。然后,您必须按下submit按钮,加载文件并刷新pafe。我将添加完整的页面代码以提供帮助。我不是这方面的专家,但此链接似乎指出,对于多部分/表单数据表单,不能在表单数据中包含边界vbCrlf。我看到你在变量d中包含了vbCrlf,也许这就是问题所在?你在不能的时候加入了边界?@RyanWildry-谢谢你的链接。删除了线路馈线,但无效。虽然我不认为自己是愚蠢的,但我必须承认其余的ANWER在这个链接上让我感到困惑,我真的不知道从何处去。@ MattRoBrts确保内容类型:应用程序/上传是正确的,也许它应该是类似于应用程序/八位字节流的泛型或?无论如何,最好使用“网络”选项卡上的浏览器开发人员工具检查实际请求数据。由于unicode字符由2个字节组成,因此ReDim bFormDataLenFormData-1应该是ReDim bFormDataLenFormData-1。同样不确定的是,首先进行连接,然后使用StrConv、vbFromUnicode,您可以尝试改用ADODB.Stream。