Solved

SetClipboardData with TEXT and HTML

Posted on 2002-07-05
13
2,369 Views
Last Modified: 2012-06-27
I have found some code to do what I am after but I am having some problems with it still. Very frustrating. Any help would be appreciated.

The information is going into the clipboard but it is not able to be pasted into anything (notepad, word, bla bla bla) Any ideas would be appreciated...


Private Const GMEM_MOVEABLE = &H2
Private Const GMEM_SHARE = &H2000
Private Const GMEM_DDESHARE = &H2000

Private Enum EPredefinedClipboardFormatConstants
    [_First] = 1
    CF_TEXT = 1
    CF_BITMAP = 2
    CF_METAFILEPICT = 3
    CF_SYLK = 4
    CF_DIF = 5
    CF_TIFF = 6
    CF_OEMTEXT = 7
    CF_DIB = 8
    CF_PALETTE = 9
    CF_PENDATA = 10
    CF_RIFF = 11
    CF_WAVE = 12
    CF_UNICODETEXT = 13
    CF_ENHMETAFILE = 14
    CF_HDROP = 15
    CF_LOCALE = 16
    CF_MAX = 17
    [_Last] = 17
End Enum


' Clipboard functions:
Private Declare Function OpenClipboard Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function RegisterClipboardFormat Lib "user32" Alias "RegisterClipboardFormatA" (ByVal lpString As String) As Long
Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function CountClipboardFormats Lib "user32" () As Long
Private Declare Function EnumClipboardFormats Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function GetClipboardFormatName Lib "user32" Alias "GetClipboardFormatNameA" (ByVal wFormat As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
' Memory functions:
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 Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)

Public Sub SetClipboardDataFromString(ByVal szText As String, ByVal lFormatId As Long, Optional ByVal hWndOwner As Long = 0)
    Dim wLen As Integer
    Dim hMemory As Long
    Dim lpMemory As Long
    Dim retval As Variant
    Dim wFreeMemory As Boolean

    ' Get the length, including one extra for a CHR$(0) at the end.
    wLen = Len(szText) + 1
    szText = szText & Chr$(0)
    hMemory = GlobalAlloc(0, wLen + 1)
    If hMemory Then
        wFreeMemory = True
        lpMemory = GlobalLock(hMemory)
        If lpMemory Then
            CopyMemory ByVal lpMemory, ByVal szText, wLen
            ' Don't send clipboard locked memory.
            retval = GlobalUnlock(hMemory)
            If OpenClipboard(hWndOwner) Then
                If EmptyClipboard() Then
                    If SetClipboardData(lFormatId, hMemory) Then
                        wFreeMemory = False
                    End If
                End If
                CloseClipboard
            End If
        End If
    End If
    wFreeMemory = False

    If wFreeMemory Then
        GlobalFree hMemory
    End If
End Sub

Public Sub SetClipboardHTML(HTMLText As String, Optional ByVal hWndOwner As Long = 0)
    '
    ' Place the passed text in the clipboard identified as HTML
    '
    Dim nClipboardText As String
   
    nClipboardText = "Version:0.9" & vbCrLf _
                   & "StartHTML:-1" & vbCrLf _
                   & "EndHTML:-1" & vbCrLf _
                   & "StartFragment:000081" & vbCrLf _
                   & "EndFragment:°°°°°°" & vbCrLf _
                   & HTMLText & vbCrLf
   
    nClipboardText = Replace(nClipboardText, "°°°°°°", Format$(Len(nClipboardText), "000000"))
    SetClipboardDataFromString nClipboardText, GetClipboardIDForCustomFormat("HTML Format"), hWndOwner
End Sub


0
Comment
Question by:schworak
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 3

Author Comment

by:schworak
ID: 7132973
Oops. stopped copying one routine too soon.

Here is the GetCliboardIDForCustomFormat routine

Public Function GetClipboardIDForCustomFormat(ByVal sName As String) As Long
Dim wFormat As Long
    wFormat = RegisterClipboardFormat(sName & Chr$(0))
    If (wFormat > &HC000&) Then
        GetClipboardIDForCustomFormat = wFormat
    End If
End Function
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7132982
could you try with this:?

Const LR_LOADFROMFILE = &H10
Const IMAGE_BITMAP = 0
Const IMAGE_ICON = 1
Const IMAGE_CURSOR = 2
Const IMAGE_ENHMETAFILE = 3
Const CF_BITMAP = 2
Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal dwImageType As Long, ByVal dwDesiredWidth As Long, ByVal dwDesiredHeight As Long, ByVal dwFlags As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Long) As Long
Private Sub Form_Load()
    'KPD-Team 1999
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    Dim hDC As Long, hBitmap As Long
    'Load the bitmap into the memory
    hBitmap = LoadImage(App.hInstance, "c:\windows\logow.sys", IMAGE_BITMAP, 320, 200, LR_LOADFROMFILE)
    If hBitmap = 0 Then
        MsgBox "There was an error while loading the bitmap"
        Exit Sub
    End If
    'open the clipboard
    OpenClipboard Me.hwnd
    'Clear the clipboard
    EmptyClipboard
    'Put our bitmap onto the clipboard
    SetClipboardData CF_BITMAP, hBitmap
    'Check if there's a bitmap on the clipboard
    If IsClipboardFormatAvailable(CF_BITMAP) = 0 Then
        MsgBox "There was an error while pasting the bitmap to the clipboard!"
    End If
    'Close the clipboard
    CloseClipboard
    'Get the picture from the clipboard
    Me.Picture = Clipboard.GetData(vbCFBitmap)
End Sub
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7132997
Besides, why don't you use Clipboard object?
0
 
LVL 3

Author Comment

by:schworak
ID: 7133015
I have figured out why it isn't working but no solution yet.



The problem is I have some HTML that I need to put in the clipboard. HTML can not be put in the clipboard using the clipboard object. Bummer! It is treated as plane text not HTML.

Anyway, I found that I need to put not only the HTML version but I also need a Rich Text version of the HTML also in the clipboard in order for me to use it in Word. And I need a text version to use it in NotePad.

ARG!!!!




Only a VB answer will do, I can't use some 3rd party control to do this. If it comes stock with VB6 then maybe but no 3rd party controls please.



The Question now is how do I do that? I know how to put each type into the clipboard, but how do I get the HTML to be RTF or TEXT?
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7133279
How do you get html rich text contents in first place, manually?
You could do it with Automation getting full rich format and if i am not wrong, images too.
0
 
LVL 3

Author Comment

by:schworak
ID: 7133588
The HTML that is stored in a database comes from a lagacy system that can not easily handle creating RTF. The output is intended mainly for output to the web but now we need to also insert some of these items into Word or plane text files when doing some reporting.



I need to turn this standard HTML into RTF and TEXT then transport it through the clipboard for the user to paste into Word or Notepad or what not.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 44

Expert Comment

by:bruintje
ID: 7133606
Hi schworak, couldn't you use the richt text control from vb? i could be wrong here but thought that putting html on the rich text control could give a way to get plain rtf or text from it?

:O)Bruintje
0
 
LVL 44

Expert Comment

by:bruintje
ID: 7133612
found a small sample

' Written exclusively for VB Center by Marco Cordero

Private Sub Command1_Click()
    Dim txt As String
    Dim b() As Byte

    On Error GoTo ErrorHandler

    Command1.Enabled = False
    ' This opens the file specified in the URL text box
    b() = Inet1.OpenURL(URL.Text, 1)
    txt = ""
    For t = 0 To UBound(b) - 1
        txt = txt + Chr(b(t))
    Next
    ' This loads the opened file into the RichTextBox control
    RichTextBox1.Text = txt
    Command1.Enabled = True
    Exit Sub

ErrorHandler:
    MsgBox "The document you requested could not be found.", vbCritical
    Exit Sub
End Sub

maybe you can use it
0
 
LVL 44

Expert Comment

by:bruintje
ID: 7133615
that's only the rtf part of course, from there you should be able to copy the contents to the clipboard for pasting in other applications or am i on the wrong track here?
0
 
LVL 3

Author Comment

by:schworak
ID: 7133630
Hmmmm.. That is an interesting concept, but the contents of the RTF text box is not the translated HTML. I mean, none of the formatting has been converted. It is just the raw code.

I am trying to take the HTML and convert it to formatted RTF and also plane text. But the idea of using the internet control may be an idea... Hmmmm... How can I get the variable MyHTML loaded into the internet control then get that up into the clipboard?
0
 
LVL 3

Author Comment

by:schworak
ID: 7133685
Found my own answer! Woo Hoo!


This solves several problems. You will need to create a blank form with the WebBrowser control, a TextBox and a command button. Inject this code into the form and give it a try. It will create some HTML in the WebBrowser control WITHOUT reading from a file or web page. It will then select the contents of the web page and push it over to the clipboard. This will fill the clipboard with HTML Format, RTF, Text and other formats all in one swoop.

Note: The text box isn't needed it is only for demo so I can see the text version on screen.

------------------ The Code ------------------


Option Explicit

Dim Body As HTMLBody

Private Sub Command1_Click()
    Dim txt As String
    Dim b() As Byte
    Dim t As Long
   
   
    Command1.Enabled = False
    ' This opens the file specified in the URL text box
    WebBrowser1.Document.Body.innerHTML = "<h1>Testing</h1><table border=1 cellspacing=0><tr><td>Test1</td><td>Test2</td></tr></table>"
    ' This loads the opened file into the RichTextBox control
   
    Set Body = WebBrowser1.Document.Body
    Text1 = Body.innerText
   
    WebBrowser1.ExecWB OLECMDID_SELECTALL, OLECMDEXECOPT_DODEFAULT
    WebBrowser1.ExecWB OLECMDID_COPY, OLECMDEXECOPT_DODEFAULT

    Command1.Enabled = True
End Sub

Private Sub Form_Load()
    WebBrowser1.Navigate2 "about:blank"
    Do While WebBrowser1.Document Is Nothing
        DoEvents
    Loop
End Sub
0
 
LVL 44

Expert Comment

by:bruintje
ID: 7133699
that's great!

-now you can ask for a deletion of your second question
-and ask to make this one a PAQ because it's a real good one

:O)Bruintje
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 7133732
Points reduced and placed in PAQ

Computer101
E-E Moderator
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now