[RESOLVED] How to get the size of the RTF content from the clipboard ?-VBForums
Results 1 to 6 of 6

Thread: [RESOLVED] How to get the size of the RTF content from the clipboard ?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2012
    Posts
    193

    Resolved [RESOLVED] How to get the size of the RTF content from the clipboard ?

    How to get the size of the RTF content from the clipboard ?
    See below:
    Attached Images Attached Images  

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    15,558

    Re: How to get the size of the RTF content from the clipboard ?

    APIs. GetClipboardData will get you a handle to the clipboard item. Then you can use GlobalSize API to determine is size (estimated). With text, there can be garbage appended and that's where lstrlenW/lstrlen APIs can help
    Insomnia is just a byproduct of, "It can't be done"

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {GDI+ Classes/Samples} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VB and DPI Tutorial} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2012
    Posts
    193

    Re: How to get the size of the RTF content from the clipboard ?

    Hi Lavolpe, I found the following code from your AlphaImage, but did not get the size of the RTF, I don't know which parameter is wrong.

    Code:
    Option Explicit
    
    ' User32 APIs
    Private Declare Function OpenClipboard Lib "user32.dll" (ByVal hWnd As Long) As Long
    Private Declare Function CloseClipboard Lib "user32.dll" () As Long
    Private Declare Function GetClipboardData Lib "user32.dll" (ByVal wFormat As Long) As Long
    Private Declare Function GlobalAlloc Lib "kernel32.dll" (ByVal uFlags As Long, ByVal dwBytes As Long) As Long
    Private Declare Function GlobalFree Lib "kernel32.dll" (ByVal hMem As Long) As Long
    Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Function GlobalSize Lib "kernel32.dll" (ByVal hMem As Long) As Long
    Private Declare Function GlobalUnlock Lib "kernel32.dll" (ByVal hMem As Long) As Long
    
    Private Sub Command1_Click()
        Dim hHandle As Long, lSize As Long
        
        If Clipboard.GetFormat(vbCFRTF) Then
            If OpenClipboard(0&) Then
                hHandle = GetClipboardData(vbCFEMetafile And &HFFFF&)
                If hHandle Then
                    lSize = GlobalSize(hHandle)
                    'If lSize > 0& Then
                        'ReDim inArray(0 To lSize - 1&)
                        'lSize = GlobalLock(hHandle)
                        'CopyMemory inArray(0), ByVal lSize, UBound(inArray) + 1&
                    'End If
                    'GlobalUnlock hHandle
                End If
                CloseClipboard
            End If
        End If
        
    End Sub

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    15,558

    Re: How to get the size of the RTF content from the clipboard ?

    Well, for starters this is wrong: vbCFEMetafile
    Also, you gotta release the handle if it returned non-zero. However, vbCFRTF And &HFFFF& is returning a zero handle, looking at it now.
    Insomnia is just a byproduct of, "It can't be done"

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {GDI+ Classes/Samples} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VB and DPI Tutorial} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    15,558

    Re: How to get the size of the RTF content from the clipboard ?

    Ok, vbCFRTF is not a valid clipboard format on my Win10 machine. When I copied RTF from WordPad to the clipboard, I enumerated all the formats placed on the clipboard and guess which isn't there? vbCFRTF. That may be an internal constant for vb and when it is used, VB retrieves the correct format handle. These types of clipboard formats are not guaranteed to be the same value day in and day out.
    Code:
    ' API
    Private Declare Function RegisterClipboardFormat Lib "user32.dll" Alias "RegisterClipboardFormatA" (ByVal lpString As String) As Long
    
    Dim realCFRTF As Integer, lValue As Long
    lValue = RegisterClipboardFormat("Rich Text Format")
    ' convert long to integer
    If lvalue > &H7FFF Then 
        realCFRTF = (lValue And &H7FFF) Or &H8000
    Else
        realCFRTF = lValue
    End If
    now you can use realCFRTF with VB's Clipboard.GetFormat and convert to Long for APIs via: realCFRTF And &HFFFF&

    That should do the trick. RTF is just one such clipboard format that is retrieved like shown above. When WordPad placed stuff on the clipboard, it placed 10 such formats along with 6 standard formats (none was vb's CFRTF)
    Insomnia is just a byproduct of, "It can't be done"

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {GDI+ Classes/Samples} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VB and DPI Tutorial} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2012
    Posts
    193

    Re: How to get the size of the RTF content from the clipboard ?

    According to your advice, now the problem has been solved. Thank you very much, LaVolpe.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.