VBA + VBS SendKey脚本

问题描述 投票:0回答:1

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

我尝试使用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>
excel vba vbscript
1个回答
0
投票

当你点击“提交”按钮时,也许你需要调用一些js,或者你也可以调用?很难说没有看到页面来源。

© www.soinside.com 2019 - 2024. All rights reserved.