VBA + VBS SendKey脚本

时间:2019-03-05 10:05:54

标签: excel vba vbscript

我正在致力于自动将多个文件上传到网站。为此,用户必须单击“浏览”按钮,然后选择文件并单击“打开”。

我尝试使用VBA和VBS复制同一件事,因为出现“选择要上传的文件”框时,VBA代码被卡住了 因此,使用VBA,我创建了一个在上载文件框出现之前运行的VBS文件,并将文件名添加到输入框,然后按Enter

Public Sub CompleteUploadThread(ByVal fName As String)
    Dim strScript As String, sFileName As String, wsh As Object
    Set wsh = VBA.CreateObject("WScript.Shell")
    '---Create VBscript String---
    strScript = "WScript.Sleep 1500" & vbCrLf & _
                "Dim wsh" & vbCrLf & _
                "Set wsh = CreateObject(""WScript.Shell"")" & vbCrLf & _
                "wsh.SendKeys """ & Trim(fName) & """" & vbCrLf & _
                "WScript.Sleep 500" & vbCrLf & _
                "wsh.SendKeys ""{ENTER}""" & vbCrLf & _
                "WScript.Sleep 500" & vbCrLf & _
                "Set wsh = Nothing"
    '---Save the VBscript String to file---
    sFileName = Application.ActiveWorkbook.path & "\Authorized\zz_automation.vbs"
    Open sFileName For Output As #1
    Print #1, strScript
    Close #1
    '---Execute the VBscript file asynchronously---
    wsh.run """" & sFileName & """"

    Set wsh = Nothing
End Sub

一切正常,直到粘贴文件名,然后单击“打开”并关闭对话框。关闭对话框后,VBA需要点击继续按钮,该按钮应触发上传过程 单击“继续”按钮后,粘贴的文本将消失,并且不会上传任何内容

如果我手动执行相同的过程,它将起作用,所以我想它必须与Sendkeys做一些奇怪的事情有关

有人可以帮我解决这个问题吗?

下面,请找到我使用上述功能的VBA部分:

CompleteUploadThread el
'el is the filename and path

'click on the Browse button
Call Doc.parentWindow.execScript("document.getElementsByName('filename')[0].click();", "JavaScript")

Dim oHTML_Element As HTMLDDElement   
For Each oHTML_Element In Doc.getElementsByTagName("input")
    If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next

完整页面的来源:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>File Upload</title>


<h3>File Upload</h3>

<div class="indent20">

<form method="post"
      action="process_upload_case.jsp"
      name="dform"
      enctype="multipart/form-data" 
      onsubmit="return checkForm();">
<input type="hidden" name="action" value="eoEata" />
<input type="hidden" name="n" value="99999999" />
<input type="hidden" name="appid" value="999" />
<table class="boundedForm alignTop">
<tr>
  <th>Case</th>
  <td>14792562</td>
</tr>
<tr>
  <th>File Description<br/></th>
  <td><textarea name="comments" rows="5" cols="40" style="width:350px;"></textarea></td>
</tr>
<tr>
  <th>Select File</th>
  <td><input type="file" name="filename" size="45" multiple /></td>
</tr>
<tr>
  <td colspan="2">
  <br/><br/>
    <input type="submit" value="Continue" />
    <input type="button" value="Cancel" onclick="reset(); window.close();" />
  </td>
</tr>
</table>
</form>

</div>

<script type="text/javascript">
<!--

function checkForm()
  {
  var f = document.dform;

  var ret = true;

  if (f.filename.value == '')
    {
    alert('Error: No file selected.');
    ret = false;
    }

  if (f.comments.value.length > 500 )
    {
    alert('Error: Description too long.' );
    ret = false;
    }

  return(ret);
  }

function reset()
{
var f = document.dform;
f.filename.value = '';
f.comments.value = '';
return true;
}
-->
</script>

1 个答案:

答案 0 :(得分:0)

当您单击“提交”按钮时,也许您还需要调用一些js或fireevent?很难看到页面来源。