我正在尝试将文件上传到网页,以下是我遵循的步骤:
这是我的代码:
Sub File_Test()
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLButtons As MSHTML.IHTMLElementCollection
Dim HTMLButton As MSHTML.IHTMLElement
Dim ie As Object
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
If HTMLButton.Type = "file" Then
HTMLButton.Click
HTMLButton.Value = "C:\Documents\Test\Temp.txt"
Exit For
End If
Next
End Sub
这是截图:
有什么建议吗?
=============================================== ====
这是另一个修改过的代码,但是我无法输入文件名
Sub File_Test()
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLButtons As MSHTML.IHTMLElementCollection
Dim HTMLButton As MSHTML.IHTMLElement
Dim ie As Object
Dim WSshell
Set WSshell = CreateObject("WScript.Shell")
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
If HTMLButton.Type = "file" Then
HTMLButton.Click
With WSshell
Application.Wait (Now + TimeValue("0:00:10"))
.AppActivate "Choose File to Upload"
Application.Wait (Now + TimeValue("0:00:10"))
.SendKeys "C:\Documents\Test\Temp.txt"
Application.Wait (Now + TimeValue("0:00:10"))
.SendKeys "~" 'Enter
End With
Exit For
End If
Next
End Sub
有什么想法吗?为什么sendkyes不工作?