Home » excel » excel – VB/VBA: Fetch HTML string from clipboard (copied via web browser)

excel – VB/VBA: Fetch HTML string from clipboard (copied via web browser)

Posted by: admin May 14, 2020 Leave a comment

Questions:

It seems that when you copy something from a web browser to the clipboard, at least 2 things are stored:

  1. Plain text
  2. HTML source code

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).

What I want to do is paste the actual source code as plain text. Can this be fetched from the clipboard in VBA?

Edit I’m trying to access all the source-code of the copied HTML, including the tags.

How to&Answers:

This time I’ve read the question properly and realised coonj wants to get the HTML from the clipboard including tags.

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.

  1. Microsoft Knowledge Base article with Windows API code to read the CF_HTML from the clipboard (function GetHTMLClipboard).
  2. 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>  
    

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?

EDIT: coonj has 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”

Answer:

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.