How to save file from Webpage from Excel VBA?

微笑、不失礼 提交于 2019-12-12 12:17:43

问题


I am trying to export a file from IE, after selecting an item in dropdown by taking the Html ID and clicking the export option, but i am strucked while saving the file.
I am Selecting the option in the dropdown based on the value in an excel range.

Please help.
Below is the code I am trying.

Dim htm As Object
Dim IE As Object
Sub Website()

    Dim Doc As Object
    Set IE = CreateObject("internetexplorer.application")
    IE.Visible = True

    IE.navigate "http://**Link is confidential, sorry for not providing link**"

    Do While IE.readystate <> 4: DoEvents: Loop

    Set Doc = CreateObject("htmlfile")
    Set Doc = IE.document

        Set ref = Doc.getelementbyid("ReportViewerControl_ctl01_ctl05_ctl00")
        For x = 0 To ref.Options.Length - 1
            If ref.Options(x).Text = "Excel" Then
                ref.selectedIndex = x
                Set refclick = Doc.getelementbyid("ReportViewerControl_ctl01_ctl05_ctl01")
                refclick.Click
                Set refclick = Nothing
            End If
        Next
    Set IE = Nothing
End Sub    

And the snap shot I am strucked here, and here i want to save the file.


回答1:


Add the following in your code:

Dim Report As Variant

Report = Application.GetSaveAsFilename("Attrition Report.xls", "Excel Files (*.xls), *.xls")



回答2:


I sent the keystrokes of the shortcut keys to click the save button in IE11.

Note: the code will not run as you expect if IE is not the active window on your machine so it won't work while in debug mode.

  • Shortcut key:Alt+S
  • VBA: Application.SendKeys "%{S}"


来源:https://stackoverflow.com/questions/22470630/how-to-save-file-from-webpage-from-excel-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!