出于安全的考虑,file元素被设定为不能通过属性或方法设置内容.因此只能找些变通的方法来实现,比如 SendKeys下面的例子需要在窗体上放一个webbrowser,一个timer。 Option Explicit Private WithEvents fl As HTMLInputElementPrivate Sub fl_onfocus() SendKeys ("c:{\}test.htm") End SubPrivate Sub Form_Load() WebBrowser1.Navigate2 "c:\test.htm" End SubPrivate Sub Timer1_Timer() Timer1.Enabled = False If Not fl Is Nothing Then fl.focus End If End SubPrivate Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant) Dim oDoc As HTMLDocument Set oDoc = pDisp.Document
If oDoc Is Nothing Then Exit Sub Set fl = oDoc.getElementById("fl")
Timer1.Interval = 10 Timer1.Enabled = True End Sub
Option Explicit
Private WithEvents fl As HTMLInputElementPrivate Sub fl_onfocus()
SendKeys ("c:{\}test.htm")
End SubPrivate Sub Form_Load()
WebBrowser1.Navigate2 "c:\test.htm"
End SubPrivate Sub Timer1_Timer()
Timer1.Enabled = False
If Not fl Is Nothing Then
fl.focus
End If
End SubPrivate Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Dim oDoc As HTMLDocument
Set oDoc = pDisp.Document
If oDoc Is Nothing Then Exit Sub
Set fl = oDoc.getElementById("fl")
Timer1.Interval = 10
Timer1.Enabled = True
End Sub