如何使用 VBA 从网页中全选并复制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21557388/
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 01:43:50  来源:igfitidea点击:

How to Select All & Copy from a webpage using VBA

excelinternet-explorervbacopy

提问by user3271598

I am working with an embedded table on a webpage, and am trying to automate the process of copying it into Excel. A simple "Select All" + Copy + Paste would work perfectly, but I cannot figure out how to automate this using VBA.

我正在使用网页上的嵌入式表格,并尝试自动执行将其复制到 Excel 的过程。一个简单的“全选”+复制+粘贴就可以完美运行,但我无法弄清楚如何使用 VBA 自动执行此操作。

Any ideas?

有任何想法吗?

回答by Bwurk

A way to get data from a webpage into a VBA application is to use a WinHttpRequest object:

从网页获取数据到 VBA 应用程序的一种方法是使用 WinHttpRequest 对象:

Dim url, resp As String
Dim req As New WinHttpRequest

url = "http://www.domain.com/page"
req.Open "GET", url, False
req.Send

resp = req.ResponseText

The HTML source is stored in the respvariable from which you can retrieve the table and its data using string functions and loops to analyze table rows and their formatting and write the data to your worksheet rows.

HTML 源代码存储在resp变量中,您可以从中检索表及其数据,使用字符串函数和循环来分析表行及其格式并将数据写入工作表行。