我遇到了这个问题。
我有一个页面,我需要上传文件。当我点击“浏览器”时,会打开“选择要上传的文件”窗口,我需要选择(双击)TXT文件。
我尝试使用Set fd = Application.FileDialog(msoFileDialogFilePicker),但无法识别此对象。
任何帮助将不胜感激!
Sub Main
' Get the browser object belonging to the active/topmost IE tab
Dim objIe
Dim activeDocument
Dim Button
Set activeDocument = Nothing
' Try and get the window object of the active IE tab
Set objIe = GetActiveBrowserObj 'this is a function previously defined that allows me to work on the active browser window
If objIe Is Nothing Then
MsgBox "Could not get active IE document object”
Exit Sub
End If
' Assign the document object to the activeDocument variable
Set activeDocument = objIe.Document
If activeDocument Is Nothing Then
MsgBox "Could not get active IE document object"
Exit Sub
End If
'Click the browse button to upload the first TXT file
Set Button = objIe.Document.getElementById("txtfile1")
Button.Click
chooseFile()
End Sub
Private Sub chooseFile()
Dim fd
Dim strTxt As String
Start:
'Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.Title = "Choose File to Upload" 'Change the title to suit
.Filters.Add "txt files", "*.txt", 1 'Change the filters to suit, or omit for none
.AllowMultiSelect = False
If .Show = -1 Then
'The user made a selection. The variable strWorkBook will contain the path\name of the file
strTxt = .SelectedItems(1)
Else
response = MsgBox("You have not selected a txt file")
If response = vbRetry Then
GoTo Start
Else
Exit Sub
End If
End If
End With
Set fd = Nothing
End Sub
答案 0 :(得分:2)
您必须使用API与该窗口进行互动,方法是找到名称为“选择要上传的文件”的窗口
这是一个例子
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Sub Sample()
Dim Ret
Ret = FindWindow(vbNullString, "Choose File To Upload")
If Ret <> 0 Then
MsgBox "File upload window Found"
Else
MsgBox "File upload window Not Found"
End If
End Sub
一旦锁定了RELavnt窗口,就必须找到Handle
的{{1}},然后在那里粘贴您的文件名。完成后,您会找到Textbox
按钮的句柄,最后单击它。
我已广泛报道here。虽然在我在链接中发布的示例中,我使用Open
保存文件,但您必须使用相同的方法来查找句柄并上传文件。
HTH