views:

2199

answers:

2

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.

A: 

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.

Will Rickards
+2  A: 

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"&gt;  
    </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"

MarkJ
+1 for MarkJ's closing comment that finding another way might be the best thing to do.I have attempted this in the past and gave up after trying many increasing bizarre solutions. As the structure of my HTML was known and simple, in the end I just parsed the raw text and replaced the missing tags.
Lunatik
Perfect, thanks MarkJ. The GetHTMLClipboard function seems to work with both Firefox and IE, and it doesn't look like it is throwing those headers in there.
jcoon