CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Sep 2001
    Location
    South Africa
    Posts
    186

    Formatting text before sending to clipboard

    I build up a text string in VB and then I want to send it to the Clipboard so that I can paste it in the body of an email. How do I go about to format (e.g. bold, underline and colored text) that text in the string in VB so that the formatting will be pasted in the email if possible.

    Any pointers appreciated.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Formatting text before sending to clipboard

    Use a Rich Text Box, which is the only text control that allows formatted text to be mixed. You can have a colored font in a textbox, but not two in the same textbox

    An RTB can alternate whatever you want.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Sep 2001
    Location
    South Africa
    Posts
    186

    Re: Formatting text before sending to clipboard

    Most of the information in the string I'm building comes from databases. I then copy the string to the clipboard so I can paste it in the email. Currently I then format the text in the email to show the bold and underline etc.

    I wanted to build the string already formatted before sending it to the clipboard. If I use a RTB, wouldn't I still need to format the text manually before sending it to the clipboard? Can the string be formatted programmatically in the RTB? I've never used a RTB before

    I will have a look at the RTB and see if it could work for what I want to do.

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Formatting text before sending to clipboard

    I've created a .RTF file that was just a table with about 40 fields (made in Word), and filled, using [1] ... [41] in each of the boxes that I wanted to fill in with fields. I filled in the other fields with fixed text.

    It's not that hard to format on the fly, after that.

    Code:
        .Find "[Text]"
        .SelText = "" & vbCrLf
        .SelFontSize = 14
        .SelText = Trim(Line1) & vbCrLf
        .SelFontSize = 14
        .SelText = Trim(Line2) & vbCrLf
        .Find Trim(Line1)
        .SelAlignment = rtfCenter
        .SelStart = 0
        .Find "Glass Block Panel Invoice"
        .SelBold = True
        .SelStart = 0
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Sep 2001
    Location
    South Africa
    Posts
    186

    Re: Formatting text before sending to clipboard

    I can get the text formatted in the RTB programmatically. The text in the RTB looks like this after I formatted it:

    This is the text in the richtextbox. This text I want bold. And this text I want underlined.
    When sending it to the clipboard with:
    Code:
    Clipboard.Clear
    Clipboard.SetText RichTextBox1.Text
    it gets pasted in the email without any formatting:

    This is the text in the richtextbox. This text I want bold. And this text I want underlined.
    When sending it to the clipboard with:
    Code:
    Clipboard.Clear
    Clipboard.SetText RichTextBox1.TextRTF
    This is what is pasted in the email:

    {\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}}
    \viewkind4\uc1\pard\lang7177\f0\fs17 This is the text in the richtextbox. \b This text I want bold.\b0 \ul And this text I want underlined\ulnone .
    \par }
    So it seems that the RTB is not going to work or I'm missing something. I think the format I need should be HTML but I have no idea how this works.
    Last edited by Bezzie; June 13th, 2009 at 02:54 AM.

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Formatting text before sending to clipboard

    You can paste formatted text into a RTB, but gettext is text
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Dec 2008
    Posts
    19

    Re: Formatting text before sending to clipboard

    Email can paste most rich text and preserve formatting.

    You are failing to set the clipboard get text to the Rich Text format. So, by default your are getting the ANSI text for the RTF code. To set the text to RTF try:

    Clipboard.SetText Richtextbox1.SelRTF, vbCFRTF

    See the VB6 help file for clipboard formats, etc.

  8. #8
    Join Date
    Sep 2001
    Location
    South Africa
    Posts
    186

    Re: Formatting text before sending to clipboard

    If I use Clipboard.SetText Richtextbox1.SelRTF, vbCFRTF and try to paste into the email, nothing gets pasted. Leaving out the vbCFRTF gets the string pasted with all the RTF codes as below:
    {\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}}
    \viewkind4\uc1\pard\lang7177\f0\fs17 This is the text in the richtextbox. \b This text I want bold.\b0 \ul And this text I want underlined\ulnone .
    \par }
    Could the problem be with the Thunderbird email program?

  9. #9
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Formatting text before sending to clipboard

    Should have said that from the beginning. That could be, as most email programs do HTML, not RTF. Microsoft Outlook does both
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  10. #10
    Join Date
    Dec 2008
    Posts
    19

    Re: Formatting text before sending to clipboard

    That's got to be it. Can't test Thunderbird... don't use it. It works fine both in Outlook and Outlook Express and Vista Mail.

  11. #11
    Join Date
    Sep 2001
    Location
    South Africa
    Posts
    186

    Re: Formatting text before sending to clipboard

    Is there a way to build a HTML string then?

  12. #12
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Formatting text before sending to clipboard

    Sure. It helps if you know it though... http://www.w3schools.com/
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  13. #13
    Join Date
    Sep 2001
    Location
    South Africa
    Posts
    186

    Re: Formatting text before sending to clipboard

    Thanks. I'll look into it and see if I can get it figured out.

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

Featured