使用VBA和模态形式操作IE [英] Manipulate IE with VBA and a modal form

查看:151
本文介绍了使用VBA和模态形式操作IE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动将excel文档上传到特定网页.我无法关联它,因为它需要一个帐户.有一个表单带有一个浏览部分(类型=文件,所以我不能只设置值)和一个使用该输入值的上载按钮.

I'm trying to automate the uploading of an excel document to a particular webpage. I can't link it as it requires an account. There is a form with a an browse section (of type=file so I can't just set the value) and an upload button that uses the value of that input.

我可以导航单击浏览"按钮,也可以单击上传"按钮,但是当我单击浏览"按钮时,会弹出一个名为选择要上传的文件"的文件浏览器窗口,这意味着我无法与其他任何人进行交互IE窗口并暂停我的VBA代码,直到关闭它为止,这意味着我无法自动将文件路径传递给它.

I can navigate to click the browse button, and I can click the upload button, but when I click the browse button a file explorer window called "Choose File to Upload" pops up and means I can't interact with any other IE window and pauses my VBA code until it is closed, which means I can't automatically pass a file path to it.

页面上的相关HTML是

The relevant HTML on the page is

<form method="post" name="uploadSkillForm" ENCTYPE="multipart/form-data"  action='uploadSkills.do?operation=upload'>        

  <tr>

</tr> 

<tr class="text1">

</tr> 


<tr width="100%">
    <td colspan="100%">
        <table width="100%">
            <tr>
                <th align="left" colspan=3 width="60%">
                    <input type="file" name="uploadedFile"  size="34" id="filePath" class="buttonStyle" style="height:20px"/>&nbsp;&nbsp;
                    <input type="button" name="upload" class="buttonStyle" value="Upload" onclick="javascript:fnUploadData()">
                </th>
            </tr>
        </table>
    </td>
</tr>

理想情况下,我只希望能够使VBA保持运行并与对话框进行交互以放入文件路径,但是将值设置为路径的任何方法都可以.

Ideally I just want to be able to keep the VBA running and interact with the dialog box to put in the file path, but any way to get the value set to the path would do.

VBA当前仅打开网页并单击按钮,因此没有任何要张贴的内容.

The VBA currently just opens the webpage and clicks the button so there is no point posting it.

任何帮助将不胜感激!

推荐答案

因此,在进行大量挖掘之后,我决定只需要让VBA运行一个外部脚本来检查窗口并输入文件名,然后按Enter.当前的问题是它集中在文件窗口上,因此,如果您不在正确的文件夹中,它将无法正常工作,但是如果您在正确的文件夹中,它将选择正确的文件,然后单击打开"!

So after much digging I decided that I had to just have VBA run an external script that checks for the window and input the file name then presses enter. Currently the issue is that it focuses on the file window so if you aren't in the right folder it doesn't work, but if you are it will select the right file and click open!

vba:

Dim retval As Variant
retval = Shell("PowerShell .'" & path & "upload_tester.ps1 file_name'", 1)

路径的存储位置,upload_tester.ps1是脚本名称,而file_name将传递给脚本以输入到文件浏览器.

where the path is where it is stored, upload_tester.ps1 is the script name and file_name is to pass to the script to input to file browser.

脚本:

param(
[Parameter(Mandatory=$true)][string]$a
)

$wshell = new-object -com wscript.shell

while(! $wshell.appactivate("Choose File to Upload")){
start-sleep -milliseconds 100
}

$wshell.appactivate("Choose File to Upload")
start-sleep -milliseconds 500;
$wshell.sendkeys($a)
$wshell.sendkeys("{ENTER}")

这将进行检查,直到打开文件浏览器窗口,键入要匹配的文件名,然后按Enter键将其选中.如果有人知道如何让它专注于文本框部分而不是文件浏览器部分,那会很棒,但否则这对我有用!

This will check until the file explorer window is open, type in the file name to match it, and then press enter to select it. If anyone knows how to get it to focus on the text box part rather than the file explorer part that would be great but otherwise this works for my purpose!

这篇关于使用VBA和模态形式操作IE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆