使用 VBA 将文件上传到站点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21427902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Upload a file to a site using VBA
提问by bmgh1985
I am using VBA to automate IE to upload a file to a site.
我正在使用 VBA 自动化 IE 将文件上传到站点。
I found the button with the "file" type, but then seem to draw a blank when setting the path.
我找到了“文件”类型的按钮,但是在设置路径时似乎画了一个空白。
My current VBA:
我目前的 VBA:
Dim btnInput As Object ' MSHTML.HTMLInputElement
Dim ElementCol As Object ' MSHTML.IHTMLElementCollection
.
.
.
Set ElementCol = appIE.Document.getElementsByTagName("input")
For Each btnInput In ElementCol
If btnInput.Type = "file" Then
btnInput.Value = "C:\temp\text.csv"
Exit For
End If
Next btnInput
The HTML it is reading:
它正在阅读的 HTML:
<div id="upload-assignments-modal" class="modal hide fade in" tabindex="-1" role="dialog" aria-hidden="false" style="display: block;">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Upload order changes</h3>
</div>
<form id="upload-form" enctype="multipart/form-data" action="" method="post" accept-charset="utf-8">
<div class="modal-body">
<div style="display:none"><input type="hidden" name="csrfmiddlewaretoken" value="abcde"></div>
<input type="hidden" name="partner" value="488" id="id_partner">
<p><label for="id_feed_file">Feed file</label><input type="file" name="feed_file" id="id_feed_file"></p>
<input type="hidden" name="feed_type" value="390" id="id_feed_type">
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal">Cancel</button>
<button name="action" value="upload" type="submit" class="btn btn-primary">Upload</button>
</div>
</form>
</div>
It finds the Type when stepping through and does go to set the value, but then there is no change on screen (I have the IE instance shown as visible for testing) and the file is not added.
它在单步执行时找到 Type 并确实去设置值,但是屏幕上没有变化(我将 IE 实例显示为可见以进行测试)并且没有添加文件。
Would I be right to assume that the "file" input type requires something other than .Value
for it's input?
我是否正确地假设“文件”输入类型需要其他东西而不是.Value
它的输入?
回答by Santosh
Give it a try
试一试
Set ElementCol = appIE.Document.getElementsByTagName("input")
For Each btnInput In ElementCol
If btnInput.Type = "file" Then
btnInput.Value = "C:\temp\text.csv"
btnInput.FireEvent ("onclick")
Exit For
End If
Next btnInput