How to work with HTML documents and the Clipboard in Visual basic

Source: Internet
Author: User
Tags header key string

My program needs to put the HTML document on the Clipboard, but I don't know how to handle it so that other programs can use it. I looked at some references to HTML Clipboard Format (cf_html), but I still couldn't find the exact definition. How do I operate?

Answer:

Using the cf_html Clipboard format with the Windows Clipboard is really easy to figure out, partly because it is not a clipboard in Clipboard format; it is a registration format (registered format). So it's not a constant because its value will change depending on the system. You can use a simple API call-RegisterClipboardFormat to get a value for registering the Clipboard format. The first call to this function executes with a given string that returns a unique value between the C000-FFFF. Each subsequent call processed on the system (subsequent calls) returns the same value. The key string used in this format is "HTML format":

  
Private Declare Function registerclipboardformat Lib "user32" Alias "Registerclipboardformata"
(ByVal lpstring as String) As Long
Dim cf_html as Long
Const reghtml as String = "HTML Format"
cf_html = RegisterClipboardFormat (reghtml)

You must first build a descriptive header and put the HTML data in the data before putting it into the clipboard. This header gives other programs a description of the version information, the offset of the HTML start data, and information about the starting place of the actual selection range (actual selection). A user may select a portion of an HTML document or even just an element (such as a few rows in a table) as a selection area. Other parts of the page, such as inline style definitions (inline style definitions), may be required for full rendering (render). Not only do you need to put the original selected HTML area into the Clipboard, but also need to put a header, it looks like this:

version:1.0
starthtml:000000258
endhtml:000001491
startfragment:000001172
endfragment:000001411

The application uses the StartFragment and Endfragment properties to determine which data needs to be pasted, and the data may (or may not) format the selected part with the remaining HTML. You must put the HTML annotation into the data for future identification of the selected part. Obviously, you have to finish it before you build the final header, otherwise the offsets will change. A opening/closing annotation label for the selected data is "<!--startfragment-->" and "<!--endfragment-->" respectively.

Listing 1. This program (routine) can be useful when you build a descriptive header for an HTML fragment. If you put the entire HTML document in place, the startfragment tag will be followed by the <body> tag, and the endfragment tag will be placed before </body>. If you just put a paragraph of HTML instead of the entire document, then the program will only put the

Public Function htmldescribed (ByVal Fragment As String) As String
Dim Data As String
Dim NPOs As Long
Const Description as String = "version:1.0" & vbCrLf & "Starthtml:aaaaaaaaaa" & VbCrLf & _
"endhtml:bbbbbbb BBB "& VbCrLf &" STARTFRAGMENT:CCCCCCCCCC "& VbCrLf & _
" endfragment:dddddddddd "& VbCrLf
Cons T Fragmentstart As String = "<!--startfragment-->"
Const fragmentend As String = "<!--endfragment-->"
Const Fmt as String = "0000000000"
' Add the starting and ending tags for the
' HTML fragment by looking for < body> tag.
NPOs = InStr (1, Fragment, "<body", vbTextCompare)
Select case NPOs
Case 0
Fragment = " Case Else
NPOs = InStr (NPOs, Fragment," > ")
If NP Os > 0 and NPOs < Len (Fragment) Then
Fragment = left$ (Fragment, NPOs) & Fragmentstart & mid$ (Fragment, n Pos + 1)
End If
End Select

NPOs = InStr (1, Fragment, </body, vbTextCompare)
Select case NPOs
Case 0
Fragment = Fragment & Fra Gmentend & vbCrLf & "</body> Case Else
Fragment = left$ (Fragment, nPos-1) & Frag Mentend & mid$ (Fragment, NPOs)
End Select
"Build" the HTML given the description, the
' Fragment, and the Context. And, replace the
' offset placeholders in the description with
' values for the offsets of STARTHMTL,
' endhtm L, StartFragment, and Endfragment.
' offsets need to is zero-based when placed on
' clipboard, so subtract 1
' from each before injecting.
D ATA = Description & Fragment
data = replace (data, AAAAAAAAAA, format$ (Len (Description), FMT))
data = Replace (Data, "BBBBBBBBBB", format$ (Len (data), FMT))
NPOs = InStr (data, Fragmentstart)-1
data = Replace (data, "CCCCCCCCCC", format$ (NPOs + Len (fragmentstart), FMT))
NPOs = InStr (data, Fragmentend)-1
data = ReplacE (Data, "dddddddddd", format$ (NPOs, FMT))
' return attributed string.
htmldescribed = Data
End Function
Here I can't make a detailed description of all aspects of this header, so I can only explain some of the main points that you can refer to For example code and a deeper understanding. You must keep a few points in mind. The offset in the header is based on zero, so you have to adjust your string manipulation (String-manipulation routine). Also, if you need to read and write headers, you must understand the number of digits (for example, Internet Explorer [IE] 9,word is 10).


Finally, if you just put cf_html into the clipboard, programs such as Word and FrontPage can't be processed. You must also provide the Clipboard with a formatted HTML plain text compilation (Plain-text rendition) to achieve the desired results. Many of the tools used to perform html-to-text transformations, or macho, may need to be performed with the profiler. However, Windows programmers do not need to parse HTML manually. You can use the OS to replace this daily task:

  
Public Function Html2text (ByVal Data As String) as String
Dim obj as Object
On Error Resume Next
Set obj = CreateObject ("Htmlfile")
Obj. Open
Obj. Write Data
Html2text = obj. Body.innertext
End Function

Upgrading IE is not the quickest way to parse HTML, but it's a pretty good way to do it.

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.