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.)
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.
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!))
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
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.
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."
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.
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.
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
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.
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
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.
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:
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.
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.
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"
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.
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"