2

私はこれに問題があります。

ファイルをアップロードする必要があるページがあります。[ブラウザ] をクリックすると、[アップロードするファイルを選択] ウィンドウが開き、そこで 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
4

1 に答える 1

2

「 Choose File To Upload」という名前のウィンドウを見つけて、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

関連するウィンドウにロックしたら、のを見つけて、HandleそこTextboxにフルパスでファイル名を貼り付ける必要があります。それが完了したら、OpenButton のハンドルを見つけて、最後にクリックします。

ここで詳しく説明しました。リンクに投稿した例では、を使用してファイルを保存してSave Asいますが、ハンドルを見つけてファイルをアップロードするには同じアプローチを使用する必要があります。

HTH

于 2013-02-15T16:49:13.663 に答える