单击文件浏览器时,我尝试输入“选择要上传的文件”弹出窗口的文件名(包括路径)。
由于VBA代码与Windows弹出窗口断开连接,因此无法输入文件名。
这是我的代码:
Sub File_Test()
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLButtons As MSHTML.IHTMLElementCollection
Dim HTMLButton As MSHTML.IHTMLElement
Dim btnInput As MSHTML.IHTMLInputElement
Dim ie As Object
Dim pointer As Integer
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
ie.navigate "http://www.htmlquick.com/reference/tags/input-file.html"
Do While ie.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = ie.document
Set HTMLButtons = HTMLDoc.getElementsByTagName("input")
For Each HTMLButton In HTMLButtons
For Each btnInput In HTMLButtons
If btnInput.Type = "file" Then
HTMLButton.Click
btnInput.Value = "C:\temp\test.txt"
pointer = 1
Exit For
End If
Next btnInput
If pointer = 1 Then Exit For
Next
End Sub