excel - 使用 VBA 将文件上传到站点

标签 excel vba internet-explorer

我正在使用 VBA 来自动化 IE 将文件上传到站点。

我找到了"file"类型的按钮,但是在设置路径时似乎画了一个空白。

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

它在单步执行时找到类型并设置值,但随后屏幕上没有任何变化(我将 IE 实例显示为可见以进行测试)并且未添加文件。

我假设"file"输入类型需要 .Value 以外的东西作为输入是否正确?

最佳答案

试一试

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

关于excel - 使用 VBA 将文件上传到站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21427902/

相关文章:

excel - 搜索值并返回 Excel 中的整行

excel - 循环查找并打印从一个工作簿到 VBA 中的事件工作簿的相应值

html - IE11 中的 CSS3 -ms-max-content

css - 浏览器兼容性问题。在 Google Chrome 中看起来不错,但在 IE 或 Mozilla 中不好看

excel - 如何自动将 Excel xls 文件转换为 Excel xml 格式?

excel - 当提供的值在其限制范围内时,字节数据类型出现溢出错误

vba - CorelDraw VBA 宏错误 : "Object Required"

excel - 为什么VBA excel公共(public)属性在出错后得到空值

internet-explorer - 对Windows Powershell中弹出窗口的引用

excel - 如何查找其他 Excel 实例并将它们分配给 Excel.Application 变量