VBA IE Automation - 上传文件

时间:2014-01-29 10:13:12

标签: excel internet-explorer vba excel-vba

我正在使用VBA自动化IE以将文件上传到网站。我已经找到了“文件”类型的按钮,但是在设置路径时似乎画了一个空白。

我目前的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

它正在阅读的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>

它在单步执行时找到Type并确实设置了值,但是屏幕上没有变化(我将IE实例显示为可见以进行测试)并且未添加文件。我是否正确地假设“文件”输入类型需要.Value之外的其他输入?

1 个答案:

答案 0 :(得分:0)

试一试

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