简体   繁体   English

VBA:IE-如何在没有弹出文件上传表单的情况下为文件输入标签分配路径名?

[英]VBA:IE-How to assign pathname to file input tag without popup file upload form?

I am currently doing automaiton for file uploading 我目前正在自动上传文件

Below is HTML tag for input file tag: 以下是输入文件标签的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">

And below is button HTML Tag: 下面是按钮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">

My VBA coding is: 我的VBA编码是:

Dim filee As Object
Set filee = mydoc.getElementById("file")
filee.Value = filenamepath

Set attach = mydoc.getElementsByName("Attach")
attach(0).Click

When I am running this coding, input filepath box not assign path name so i am getting chose file path. 当我运行此编码时,输入文件路径框未分配路径名,因此我正在选择文件路径。

Find attach screenshot. 查找附件截图。 在此处输入图片说明

Finally i have tried below code but that send key not executing 终于我尝试了下面的代码,但发送键未执行

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

Kindly tell me the windows API program to assign my file name directory in fine name: input box on opened Choose File to Open explorer and click the open button. 请告诉我Windows API程序以精细名称分配我的文件名目录打开的输入框选择 “打开文件”浏览器,然后单击“ 打开”按钮。

I fixed this issue by running external VBScript contain file path to set it on 'Choose File to Upload' pop up window using SendKeys method after send Enter Key to close this pop up, and this run successfully because the extranl VBScript run on another process so it will not stuck on VBA code. 我通过运行外部VBScript包含文件路径来解决此问题,该路径在发送Enter键以关闭此弹出窗口后使用SendKeys方法在“选择要上传的文件”弹出窗口中进行设置,并且此运行成功,因为Extranl VBScript在另一个进程上运行,因此它不会卡在VBA代码上。

Notes: 1- I dynamically create the external VBScript from VBA code and save it on Temp folder after that I run this script using WScript.Shell.Run to excute it on another thread 1- At the begining of the external VBScript I set 1 sec delay to be sure the 'Choose File to Upload' pop up window already opened from VBA. 注意:1-我从VBA代码动态创建了外部VBScript,然后使用WScript.Shell.Run运行该脚本,然后将其保存在Temp文件夹中,然后在另一个线程上执行了该任务1-在外部VBScript开始时,我设置了1秒延迟以确保已从VBA中打开“选择要上传的文件”弹出窗口。

And here is the complete code: 这是完整的代码:

....
....

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

As setting the value of a file input element is disabled due to security reasons, the "send keys" method seems to be the only option for automating file uploads using the IE API. 由于出于安全原因禁用了设置文件输入元素的值,因此“发送键”方法似乎是使用IE API自动执行文件上传的唯一选项。

I just stumbled over the same problem that the code after the Click does not seem to be executed - that is, unless the dialog is closed. 我偶然发现了一个问题,即Click之后的代码似乎没有执行-也就是说,除非关闭对话框。 This indicates that the Click method is blocking, making it impossible to interact with the dialog from within the macro. 这表明Click方法正在阻塞,无法从宏内与对话框进行交互。

I could solve that by using a different method to open the dialog: by setting the focus to the file element with Focus , and sending the space key with SendKeys . 我可以使用另一种方法打开对话框来解决此问题:通过使用Focus将焦点设置为file元素,并使用SendKeys发送空格键。

In your case, replace 根据您的情况,更换

filee.Click

with

filee.Focus
SendKeys " "

leemes's method(Sending key to the file selection button on IE) is an easy way to automate the file selection procedure. leemes的方法(将键发送到IE上的文件选择按钮)是自动执行文件选择过程的简便方法。

In addition, if IEObject.Visible sometimes fails to give focus to the IE Window, we'd better send the IE window to the top-most position using Windows API before using 'SendKeys' like following: 此外,如果IEObject.Visible有时无法将焦点放在IE窗口上,我们最好在使用“ SendKeys”之前,使用Windows API将IE窗口发送到最顶部位置,如下所示:

#If VBA7 Then
    Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
    Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As LongPtr
#Else
    Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
#End If

Sub Test()

   'first create or get IE object
   Set IE= ...
   ...

   'second, send IE window to the foreground
   Dim TargetWnd
   TargetWnd = FindWindow("IEFrame", vbNullString)  'find IE window
   If TargetWnd = 0 Then Debug.Print "Window not found." 'Else Debug.Print TargetWnd
   SetForegroundWindow (TargetWnd)

   'sendkeys
   set filee = getElement....
   filee.Focus
   SendKeys " "     'send Space key instead of .Click method
   SendKeys "filePath"     ' "C:\path\filename"  ' Type-in the filename 
   SendKeys "{Enter}"    'closes the file dialog

   'finally submit       
   ...
   ...

end Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium VBA:填写 Windows 弹出窗口以上传文件 - Selenium VBA: Fill a Windows Popup to upload a file Selenium 驱动程序/Java - 不带上传文件<input type="file"> - Selenium Driver / Java - Upload File Without <input type="file"> VBA可以调用windows文件传输弹窗吗? - Can VBA call the windows file transfer popup? Windows上没有gem的File Upload中的Rails - Rails on Windows File Upload without gems 如何在不创建本地文件的情况下将.xlsx文件上传到FTP? - How do I upload an .xlsx file to an FTP without creating a local file? 通过IE上传文件后无法删除文件夹,但是可以删除文件 - Cannot delete folder after file upload by IE, but files can be deleted 使用ghostscript时如何使用没有扩展名的输入文件名附加输出文件名? - How to append output file name using input file name without extention while using ghostscript? 如何在不编写自己的程序的情况下将一些文件上传到 Azure blob 存储? - How do I upload some file into Azure blob storage without writing my own program? IE8不显示XML,而是显示一个弹出窗口:“您要保存此文件吗?”为什么? - IE8 not displaying XML but giving a popup saying “Do you want to save this file?” Why? 如何从批处理文件中调用VBA函数 - How to call VBA function from batch file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM