Results 1 to 22 of 22

Thread: [RESOLVED] Clipboard Italics

  1. #1

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Resolved [RESOLVED] Clipboard Italics

    Using a 'plain' textbox (not a RichTextBox), how can I send ITALIC text to the Clipboard, and then PASTE that into another application (the application I WANT to use is GMAIL)? (Reason for not using an RTB---I have several, several textboxes in an application and would take a lot to convert them.)

    Example, I have a textbox with non-italicized text. On a command button click, I change that text to Italics, copy it to the clipboard, and then reset the textbox to normal font. When I paste into a draft email (or even in a Word Document) what is in the Clipboard, it displays as normal, not italicized).

    As apposed to, for example, if I have a Word Document with italics, and copy some of that text to the clipboard (using the mouse, not VB code), and paste it to another document, or even the email draft, it does come out in italics. I want to be able to replicate that using VB6 in code.

    My (failed) attempt:

    Code:
    Private Sub Command1_Click()
        Text1.Text = "Now is the time for all good men to come to the aid of their party."
        Text1.Font.Italic = True
        Clipboard.Clear
        Clipboard.SetText (Text1.Text)
        Text1.Font.Italic = False
    End Sub
    (When I now use the mouse to Paste into a draft email, the text is normal, not italics.)

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Clipboard Italics

    Even though the FONT is italics, Text1.Text is just text, it doesn't have any font information. That's not to say you can't do this... it just requires a different approach... try this:
    Code:
    Private Sub Command1_Click()
        Text1.Text = "Now is the time for all good men to come to the aid of their party."
        Text1.Font.Italic = True
        Clipboard.Clear
        Text1.SelStart = 0
        Test1.SelLength=Len(Text1.Text)
        Clipboard.SetText (Text1.SelText)
        Text1.Font.Italic = False
    End Sub
    I think that works. It'll select everything in the text box, then send that to the clipboard... and I think that should sent the font info as well, preserving the italics font. I think.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,997

    Re: Clipboard Italics

    I suggest this solution:

    Add a RichTextBox to the form named rbtAux and set Visible to False.
    Then add this code to the form:

    Code:
    Private Const WM_COPY = &H301&
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Private Sub CopyClipboardFormatted(nText As String, Optional nFontName As String = "Arial", Optional nFontSize As Single = 10, Optional nBold As Boolean, Optional nItalic As Boolean)
        rtbAux.Text = ""
        rtbAux.SelFontName = nFontName
        rtbAux.SelFontSize = nFontSize
        rtbAux.SelBold = nBold
        rtbAux.SelItalic = nItalic
        rtbAux.SelText = nText
        rtbAux.SelStart = 0
        rtbAux.SelLength = Len(rtbAux.Text)
        SendMessage rtbAux.hWnd, WM_COPY, 0&, 0&
    End Sub
    And to copy the content of a TextBox named Text1 to the Clipboard in italics:

    Code:
    CopyClipboardFormatted Text1.Text, , , , True

  4. #4

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Clipboard Italics

    no sir (good thought though)....same thing...normal text (both in Word and the email). (changed your common misspelling of test1. to text1 (I do that all the time!))

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Clipboard Italics

    RTF is easy enough:

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        With Clipboard
            .Clear
            .SetText "{\rtf1\i " & Text1.Text & "}", vbCFRTF
        End With
    End Sub
    
    Private Sub Form_Load()
        RichTextBox1.LoadFile "Document.rtf"
    End Sub
    
    Private Sub Text1_Change()
        Command1.Enabled = Len(Text1.Text) > 0
    End Sub
    Name:  sshot.png
Views: 495
Size:  5.3 KB


    That will probably work fine for Word, however GMail is another matter.

    It completely depends on the email client, and if using the GMail browser client then it may depend on the browser in use and how Google implements its widgets on the page.

    It will probably always take plain text, might take RTF text, or might take HTML text though I'm not sure how standard HTML snippets are and in VB6 that would require API calls instead of the Clipboard object.

  6. #6

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Clipboard Italics

    Ed....Excellent...it works! It DOES copy the formatted text to the clipboard, and I can then paste it into Word (Word has Paste Option, the default being 'retaining format'. UNFORTUNATELY, the gmail draft window only gives one a 'paste' option, and it does NOT retain the format. BUT, If I first paste it into MS WORD, and then, using the mouse, copy-paste it into gmail...it shows the now italicized format!!!!!!!!!!!!! Now THAT'S strange.

    IOW...directly from Clipboard to gmail...normal font. Paste to Word, copy with mouse from Word, paste to gmail...italics. Things that make you wanna go "Hmmmm."

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Clipboard Italics

    Hi Sam,

    I suspect you already know some of this, but you just have to have some understanding of clipboard formats to understand what's happening here. And, in fact, you can simultaneously have multiple formats in the clipboard (and, in fact, often do).

    Regarding text, here are a few different formats: Plain text, RTF text, XML text, etc.

    The problem you are having, all that will ever come out of a TextBox is Plain text. Sure, you can tell the TextBox to show you this Plain text as italic, or bold, or red, etc. However, it's still always Plain text.

    The only way to "Paste" some other kind of text from the clipboard, is to put it in the clipboard that way. That's why everyone is encouraging you to use the RTB. The RTB rather trivially creates RTF text that can be used for the clipboard, and it tells the clipboard that it's RTF text so that it can be Pasted that way into other applications.

    Other than learning how to build your own RTF string, and then manually putting it into the clipboard and also informing the clipboard that it's RTF text, I don't see another way.

    All The Best,
    Elroy

    EDIT1: Ahhh, I studied Ed's post after I saw your post in between me writing this. Yes, Ed is essentially doing what my last sentence suggests.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  8. #8

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Clipboard Italics

    dile...now THAT worked! Was able to paste directly to gmail, and italics shown.

    Why the difference between what ed and you posted?

  9. #9

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Clipboard Italics

    to elroy....got it. Thanks for the explanation. Putting an RTB (invisible) is no issue in my program (well, not NO issue, but not much of one...I can work that part out no problem.

    But, still wonder why Ed's didn't work, while dilettante's did.

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Clipboard Italics

    I'm not sure, but telling the RTB to "copy" whatever is selected that way probably puts several formats of the selected text onto the Clipboard.

    So if the GMail widget logic sees "plain text" as the first format maybe it just takes that one?

  11. #11

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Clipboard Italics

    possibly...but confusing...oh well....

    Gotta mark this as Resolved as dile's suggestion seems to work fine. Now, to put that RTB into that app of mine.

    Thanks to all for your suggestions.

    Sam

  12. #12

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] Clipboard Italics

    Oh, one more thing...I DID use part of ed's code...copy the textbox to the rtb...my final code (in my SAMPLE program):

    Code:
    Option Explicit
    Private Const WM_COPY = &H301&
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Sub Command1_Click()
        CopyFormattedToRTB Text1.Text, , , , True
        With Clipboard
            .Clear
            .SetText "{\rtf1\i " & Text1.Text & "}", vbCFRTF
        End With
    End Sub
    Private Sub Form_Load()
        Text1.Text = "Thanks Ed, dile, et al."
    End Sub
    Private Sub Text1_Change()
        Command1.Enabled = Len(Text1.Text) > 0
    End Sub
    Private Sub CopyFormattedToRTB(nText As String, Optional nFontName As String = "Arial", Optional nFontSize As Single = 10, Optional nBold As Boolean, Optional nItalic As Boolean)
        RTBAux.Text = ""
        RTBAux.SelFontName = nFontName
        RTBAux.SelFontSize = nFontSize
        RTBAux.SelBold = nBold
        RTBAux.SelItalic = nItalic
        RTBAux.SelText = nText
        RTBAux.SelStart = 0
        RTBAux.SelLength = Len(RTBAux.Text)
        SendMessage RTBAux.hWnd, WM_COPY, 0&, 0&
    End Sub

  13. #13
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,997

    Re: [RESOLVED] Clipboard Italics

    That's not neccesary Sam, you are copying the text to the clipboard twice in that way, and only the last one will prevail.
    Choose one method or the other. If dilettante's method worked fine for what you need, then you can delete all the code about the RichTextBox (and the RichTextBox itself) because they are not needed.

  14. #14

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] Clipboard Italics

    oh yeah...I see...oops

  15. #15

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] Clipboard Italics

    Come to find out....what I want to put into the clipboard, is not only what is in a textbox, but is appended by texts of three comboboxes. I put all that into a variable (whatToCopy), then using dile's code
    Code:
    Clipboard.SetText "{\rtf1\i " & whatToCopy & "}", vbCFRTF
    , I get 'almost' what I want.

    That will put ALL of that as italics in the Clipboard (what I asked for, but not what I see I wanted!!!). What I desire is to have just the textbox text in Italics, the other in plain. I can easily, after pasting in gmail, highlight that portion of the text I want to change back to normal (always at the end...textbox first, a space, an opening quote, the contents of the three comboboxes, and a closing quote) ---this is all a Bible verse (textbox) with Book, Chapter and Verse following)

    Example DESIRED: "Jesus Wept" (John 11:35) (Just verse is in Italics) GOT: "Jesus Wept" (John 11:35) (Everything in Italics)

    SO, I guess I'll be happy with at least what I have now....(sure wish clipboard had multiple 'levels' (like last 10 or so!!!!))...that would come in handy in lots of apps where I might want to save 10 different things at different times...but I guess that is asking to much of Gates's boys and gals.

  16. #16
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: [RESOLVED] Clipboard Italics

    You still can get that... you just need to add the RTF as you build the text... I copied the text, pasted it into WordPad, formatted it bold and italics, then saved it as RTF, then opend the RTF file in a test editor... it looks like this:
    Code:
    {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
    {\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sa200\sl276\slmult1\lang9\b\i\f0\fs22 "Jesus Wept"\i0  (John 11:35)\b0\par
    }
    It's a bit more than desired since it's also got font info... but whittling that down... what you're probably looking for is something like this:
    Code:
    "{\rtf1\i " & Quote & "\i0 " & ChapterVerse & "}"
    Wasn't sure if you wanted it all bold too, if that's the case:
    Code:
    "{\rtf1\b\i " & Quote & "\i0 " & ChapterVerse & "\b0}"
    Probably wouldn't hurt to throw a \par on the end to end the paragraph too.

    -tg

    (note, I just checked, using the stripped down RTF with the simple italics and bold in an RTF file, rendered perfect when opened.)
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  17. #17
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: [RESOLVED] Clipboard Italics

    Escaping backslash in Quote/ChapterVerse is mandatory or the rich-text format will break badly. Also, RTF is very unicode unfriendly, even upper ANSI codepage has to be escaped.

    cheers,
    </wqw>

  18. #18

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] Clipboard Italics

    Thanks, tech....so THAT was what that \i was for!!! Got it. Thanks all...

  19. #19
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [RESOLVED] Clipboard Italics

    Quote Originally Posted by wqweto View Post
    Escaping backslash in Quote/ChapterVerse is mandatory or the rich-text format will break badly. Also, RTF is very unicode unfriendly, even upper ANSI codepage has to be escaped.
    These are good points. Similar to avoiding SQL Injection headaches when building dynamic SQL.

  20. #20
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Clipboard Italics

    Quote Originally Posted by dilettante View Post
    I'm not sure, but telling the RTB to "copy" whatever is selected that way probably puts several formats of the selected text onto the Clipboard.

    So if the GMail widget logic sees "plain text" as the first format maybe it just takes that one?
    Microsoft recommends enumerating the clipboard and handle the first format that makes sense. The formats placed on the clipboard first come from the 'soucre code' and other formats follow that are automatically generated by Windows.
    Quote Originally Posted by msdn
    If you are pasting information from the clipboard, retrieve the first clipboard format that you can handle. That will be the most descriptive clipboard format that you can handle.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    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} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  21. #21
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [RESOLVED] Clipboard Italics

    By that logic when told to "copy" the selected text, the RTB should copy RTF first and then Text, right?

    So does that explain the behavior seen above?


    Here's an attempt to escape text as "RTF Safe" text for building clipboard RTF strings. Probably not perfect, I'm sure it isn't exhaustively complete. There might even be some API call to do this that I'm not aware of.

    Just a stab at the problem really.
    Attached Files Attached Files

  22. #22
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [RESOLVED] Clipboard Italics

    Quote Originally Posted by dilettante View Post
    By that logic when told to "copy" the selected text, the RTB should copy RTF first and then Text, right?
    I'm not curious enough to enum the clipboard formats preceded by a RTF copy, but that would answer the question I'd think. And yes, if I wrote an RTB control, I think I'd place the RTF first in the clipboard, followed by non-markup unicode, followed by other formats as appropriate.

    That first in, first desired concept is what I eventually used to read clipboard formats for my image control which could accept a file name (text) placed on the clipboard. Since it seems a metafile version of the text was also placed there, it came after the text. This way I didn't try to load a metafile containing text vs pasting a text string.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    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} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

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



Click Here to Expand Forum to Full Width