I am currently doing automaiton for file uploading
Below is HTML tag for input file tag:
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.
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.
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
leemes's method(Sending key to the file selection button on IE) is an easy way to automate the file selection procedure.
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:
#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
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.
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. This indicates that the Click
method is blocking, making it impossible to interact with the dialog from within the macro.
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
.
In your case, replace
filee.Click
with
filee.Focus
SendKeys " "