How to add HTML code to the clipboard by using Visual Basic or VBA

Source: Internet
Author: User
Developers can use the cf_html clipboard format (HTML format) to share HTML data with other applications that understand HTML, such as Microsoft Office and Microsoft Internet Explorer.

Cf_html is entirely a text-based format that includes des a description, a context, and a fragment within that context. when you are building data to send to the clipboard, you must include a description of the data to indicate the clipboard version and the offsets for the context and fragment. you can usePuthtmlclipboardProcedure described later in this article to simplify this task.

Steps to create a sample project

  1. Start a new standard EXE project in Visual Basic. form1 is created by default.
  2. Add twoCommandbuttonControls to form1. changeCaptionPropertyCommand1ToPut html, And changeCaptionPropertyCommand2ToGET html.
  3. OnProjectMenu, chooseAdd ModuleTo add a standard bas module to the project.
  4. Add the following code to the Module window:

    Option explicit private declare function closeclipboard lib "USER32" () as long private declare function openclipboard lib "USER32" (byval hwnd as long) _ As long private declare function globalalloc lib "Kernel32" (_ byval wflags as long, byval dwbytes as long) As long private declare function setclipboarddata lib "USER32" (_ byval wformat as long, byval hmem as long) As long private declare fun Ction emptyclipboard lib "USER32" () as long private declare function registerclipboardformat lib "USER32" alias _ "registerclipboardformata" (byval lpstring as string) as long private declare function globallock lib "Kernel32" (byval hmem as long) _ As long private declare function globalunlock lib "Kernel32" (_ byval hmem as long) as long private declare sub copymemory lib "Kernel32" alias "RTL Movememory "(_ pdest as any, psource as any, byval cblength as long) Private declare function getclipboarddata lib" USER32 "(_ byval wformat as long) as long private declare function lstrlen lib "Kernel32" alias "lstrlena" (_ byval lpdata as long) As long private const m_sdescription = _ "version: 1.0" & vbcrlf & _ "starthtml: aaaaaaaaaa "& vbcrlf & _" endhtml: bbbbbbbbbb "& vbcrlf & _" startfragm Ent: cccccccccc "& vbcrlf & _" endfragment: dddddddddd "& vbcrlf private m_cfhtmlclipformat as long function registercf () as long 'register the HTML clipboard format if (m_cfhtmlclipformat = 0) then m_cfhtmlclipformat = registerclipboardformat ("HTML format") end if registercf = m_cfhtmlclipformat end function public sub puthtmlclipboard (shtmlfragment as string, _ optional scontextstart as string =" <HTML> <body> ", _ optional scontextend as string =" </body> 

  5. Add the following code in the code windowForm1:

    Private sub commandateclick () dim sfrag as string, sstart as string, send as string sstart = "<HTML> <body> <font face = Arial size = 1 color = blue>" sfrag = "<B> This is bold </B> and <i> This is italic. </I> "send =" </font> </body> 

  6. Press the F5 key to start the Visual BASIC program. ClickPut htmlTo add the HTML data to the clipboard. Then, clickGET htmlTo retrieve the Clipboard data. Note that a message box displays the HTML fragment.
  7. Start Microsoft Word (or any application that recognizes the HTML format for the clipboard). paste the clipboard contents to a new document to examine the results of the HTML code contained on the clipboard.
Additional note

Using an approach that sends HTML code to the clipboard might be especially beneficial for office automation clients. for example, if you have an automation client that needs to generate formatted data for cells in Microsoft Excel or paragraphs in Microsoft Word, you cocould build the data in HTML code, send it to the clipboard, and then paste it into the application. by using this technique, you can reduce the number of out-of-process cballs to the automation client.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.