使用VBA将文件上传到html浏览器

时间:2018-02-26 20:20:44

标签: vba excel-vba excel

我正在尝试将文件上传到网页,以下是我遵循的步骤:

  1. 打开网页http://www.htmlquick.com/reference/tags/input-file.html
  2. 等待页面加载
  3. 在此网页中,我将文件上传到第一个“上传文件”浏览器。
  4. 按标签名称获取输入元素为“input”
  5. 点击“浏览”按钮,因为粘贴药水被禁用。
  6. 在“选择要上载的文件”窗口中输入文件路径
  7. 输入 第5步后,我无法在“选择要上传的文件”窗口中输入文件路径,看起来宏不支持此功能。
  8. 这是我的代码:

    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
    

    这是截图:

    htmlquick

    有什么建议吗?

    =============================================== ====

    这是另一个修改过的代码,但是我无法输入文件名

    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不工作?

0 个答案:

没有答案