如何从 Excel VBA 保存网页中的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22470630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:23:48  来源:igfitidea点击:

How to save file from Webpage from Excel VBA?

internet-explorerexcel-vbasaveexcel-2007vba

提问by Punith GP

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.

在通过获取 Html ID 并单击导出选项在下拉列表中选择一个项目后,我试图从 IE 导出一个文件,但在保存文件时我感到震惊。
我正在根据 excel 范围中的值在下拉列表中选择选项。

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.
how to select Save

我在这里被击中的快照,在这里我想保存文件。
如何选择保存

回答by ktk

Add the following in your code:

在您的代码中添加以下内容:

Dim Report As Variant

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

回答by Tony L.

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

我发送了快捷键的击键在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.

注意:如果 IE 不是您机器上的活动窗口,代码将不会按您的预期运行,因此它在调试模式下将无法运行。

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