Kat*_*vtv 1 windows internet-explorer automation vba excel-vba
我目前正在进行文件上传自动化
下面是输入文件标记的HTML标记:
<input name="file" title="Type the path of the file or click the Browse button to find the file." id="file" type="file" size="20">
Run Code Online (Sandbox Code Playgroud)
以下是按钮HTML标签:
<input name="Attach" title="Attach File (New Window)" class="btn" id="Attach" onclick="javascript:setLastMousePosition(event); window.openPopup('/widg/uploadwaiting.jsp', 'uploadWaiting', 400, 130, 'width=400,height=130,resizable=no,toolbar=no,status=no,scrollbars=no,menubar=no,directories=no,location=no,dependant=no', true);" type="submit" value="Attach File">
Run Code Online (Sandbox Code Playgroud)
我的VBA编码是:
Dim filee As Object
Set filee = mydoc.getElementById("file")
filee.Value = filenamepath
Set attach = mydoc.getElementsByName("Attach")
attach(0).Click
Run Code Online (Sandbox Code Playgroud)
当我运行这个编码时,输入文件路径框不分配路径名,所以我得到选择文件路径.
最后我尝试了下面的代码,但发送密钥没有执行
Dim filee As Object
Set filee = mydoc.getElementById("file")
filee.Click
obj.SetText filename
obj.PutInClipboard
SendKeys "^v"
SendKeys "{ENTER}"
Set attach = mydoc.getElementsByName("Attach")
attach(0).Click
Set finall = mydoc.getElementsByName("cancel")
finall(0).Click
Run Code Online (Sandbox Code Playgroud)
请告诉我Windows API程序以精细名称分配我的文件名目录:打开时选择文件以打开资源管理器并单击打开按钮.
小智 6
我通过运行外部VBScript包含文件路径来修复此问题,在发送Enter键后使用SendKeys方法在"选择要上载的文件"弹出窗口中将其设置为关闭此弹出窗口,这样运行成功,因为extranl VBScript在另一个进程上运行所以它不会停留在VBA代码上.
注意:1-我从VBA代码动态创建外部VBScript并将其保存在Temp文件夹之后我使用WScript.Shell.Run运行此脚本以在另一个线程上执行它1-在外部VBScript开始时我设置1秒延迟以确保已从VBA打开的"选择要上载的文件"弹出窗口.
这是完整的代码:
....
....
Set filee = mydoc.getElementById("file")
CompleteUploadThread MyFilePath
filee.Foucs
filee.Click
....
....
Private 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 1000" & vbCrLf & _
"Dim wsh" & vbCrLf & _
"Set wsh = CreateObject(""WScript.Shell"")" & vbCrLf & _
"wsh.SendKeys """ & fName & """" & vbCrLf & _
"wsh.SendKeys ""{ENTER}""" & vbCrLf & _
"Set wsh = Nothing"
'---Save the VBscript String to file---
sFileName = wsh.ExpandEnvironmentStrings("%Temp%") & "\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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4223 次 |
| 最近记录: |