【问题标题】:Upload a file to a site using VBA使用 VBA 将文件上传到站点
【发布时间】:2022-03-09 02:03:30
【问题描述】:

我正在使用 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 以外的其他内容作为输入,我是否正确?

【问题讨论】:

  • 补充一下,我使用的是 Excel 2007 和 IE11(尽管在 XP 中可能也需要使用任何解决方案,所以 IE8)
  • 你不能。它被设计为一种安全功能,理论上(我相信)可以防止流氓脚本从用户计算机上传文档
  • 如果它是一个只读属性,我不会感到惊讶,但必须有另一种方法可以做到这一点,希望同样简单:)
  • 我很确定没有。我很久以前就看过这个,最后直接将文件发布到服务器(无论如何这是一个更好的解决方案)
  • 很可能最终会走上这条路。归根结底,这是一个有效的解决方案,但希望尝试让这种方法为最终用户带来更多好处(至少如果他们看到实际站点加载等,他们知道它“在做某事”,而且这是当上传文件的主要方法(FTP)不起作用时的备份系统)。另外,让这个工作也有助于满足我自己的好奇心:P

标签: excel vba internet-explorer


【解决方案1】:

试试看

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

【讨论】:

  • @bmgh1985 可以分享链接吗?
  • 无法在您必须登录的网站上共享该链接。已经在脚本中设置了登录部分和导航部分,只需要这一点。将更改 OP 以包含更多 HTML 以使其更容易
猜你喜欢
  • 1970-01-01
  • 2012-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-19
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
相关资源
最近更新 更多