VB/VBA:从剪贴板中获取 HTML 字符串(通过网络浏览器复制)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/639890/
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
VB/VBA: Fetch HTML string from clipboard (copied via web browser)
提问by Jason Coon
It seems that when you copy something from a web browser to the clipboard, at least 2 things are stored:
似乎当您将某些内容从 Web 浏览器复制到剪贴板时,至少会存储两件事:
- Plain text
- HTML source code
- 纯文本
- HTML源代码
Then it is up to the software that you are pasting into can determine which one it wants. When pasting into MS Excel 2003, you have a paste special option to paste HTML, which will paste the formatted HTML (as it is displayed by the browser).
然后取决于您要粘贴到的软件可以确定它想要的软件。粘贴到MS Excel 2003 时,您有一个粘贴特殊选项来粘贴 HTML,这将粘贴格式化的 HTML(如浏览器显示的那样)。
What I want to do is paste the actual source code as plain text. Can this be fetched from the clipboard in VBA?
我想要做的是将实际的源代码粘贴为纯文本。这可以从 VBA 的剪贴板中获取吗?
EditI'm trying to access all the source-code of the copied HTML, including the tags.
编辑我试图访问复制的 HTML 的所有源代码,包括标签。
回答by MarkJ
This time I've read the question properly and realised coonjwants to get the HTML from the clipboard including tags.
这次我已经正确阅读了问题,并意识到coonj想要从剪贴板中获取包含标签的 HTML。
I believe this is reasonably difficult. You need to read the clipboard using Windows API calls. And then, parse the resulting CF_HTML string which has some wacky headers added on top of the HTML.
我相信这是相当困难的。您需要使用 Windows API 调用读取剪贴板。然后,解析生成的 CF_HTML 字符串,该字符串在 HTML 顶部添加了一些古怪的标题。
- Microsoft Knowledge Base articlewith Windows API code to read the CF_HTML from the clipboard (function GetHTMLClipboard).
You will then probably want to ignore the wacky headers. Microsoft documents the format here. An example CF_HTML fragment is shown below. You could probably come up with some guesswork method of skipping the first few lines.
Version:0.9 StartHTML:71 EndHTML:170 StartFragment:140 EndFragment:160 StartSelection:140 EndSelection:160 <!DOCTYPE> <HTML> <HEAD> <TITLE>The HTML Clipboard</TITLE> <BASE HREF="http://sample/specs"> </HEAD> <BODY> <!--StartFragment --> <P>The Fragment</P> <!--EndFragment --> </BODY> </HTML>
- Microsoft 知识库文章,其中包含从剪贴板读取 CF_HTML 的 Windows API 代码(函数 GetHTMLClipboard)。
然后,您可能想要忽略古怪的标题。Microsoft 在此处记录格式。下面显示了一个示例 CF_HTML 片段。您可能会想出一些跳过前几行的猜测方法。
Version:0.9 StartHTML:71 EndHTML:170 StartFragment:140 EndFragment:160 StartSelection:140 EndSelection:160 <!DOCTYPE> <HTML> <HEAD> <TITLE>The HTML Clipboard</TITLE> <BASE HREF="http://sample/specs"> </HEAD> <BODY> <!--StartFragment --> <P>The Fragment</P> <!--EndFragment --> </BODY> </HTML>
It might also be worth thinking whether there's any other way of solving your problem. E,g, Will the browser always be Internet Explorer? Can you get what you need by walking the HTML tree using the COM object model?
是否还有其他方法可以解决您的问题,这也可能值得考虑。例如,浏览器会一直是 Internet Explorer 吗?通过使用 COM 对象模型遍历 HTML 树,您可以获得所需的信息吗?
EDIT: coonjhas tried this now and says "the GetHTMLClipboard function seems to work with both Firefox and IE, and it doesn't look like it is throwing those headers in there"
编辑:coonj现在已经尝试过了,并说“GetHTMLClipboard 函数似乎同时适用于 Firefox 和 IE,而且它看起来不像是将这些标题扔在那里”
回答by Will Rickards
VB6 has the Clipboard object that allows you to get the clipboard data in different formats. VBA doesn't have this object. But there are windows API calls you can use. You can see a sample implementation for VBA here.
VB6 有 Clipboard 对象,它允许您获取不同格式的剪贴板数据。VBA 没有这个对象。但是您可以使用 Windows API 调用。您可以在此处查看 VBA 的示例实现。