Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keep formatting in TextBox 1

Status
Not open for further replies.

rtshort

IS-IT--Management
Feb 28, 2001
878
US
I wrote a small app to let the user input data and send it directly to an e-mail. From there it goes into a Database.

When the info from the textbox is transferred to the E-Mail it looses it's format. For example:
***************************
abc
def

ghi...
****************************
Looks like this in the e-mail:

abcdefghi...

What do I need to do to keep it's formatting? When it's put into the Database and pulled up again, it's still formatted correctly.

Rob
Just my $.02.
 
>send it directly to an e-mail

Can you show us how you are doing this? It is probably relevant.
 
Oh, and how are you applying 'formatting' in the textbox? Just through allowing it to autowrap? If so then be aware that no actual CRLFs are inserted into the text. The fomratted layout is for display purposes only.
 
With some code I found here with the help of another member.

Code:
Private Sub cmdEMail_Click()
    If txtEnter.Text = "no" Then
        MsgBox ("This Claim has not been entered!" & vbCrLf & "Please click Enter New Record to continue."), vbInformation + vbOKOnly, "ENTER RECORD FIRST"
        cmdEnter.SetFocus
        Exit Sub
    End If
    'mailInfo(0) = "anexample@somewhere.com"
    'mailInfo(1) = "cc@somehere.com"
    'mailInfo(2) = "bcc@somewhere.com"
    mailInfo(3) = "New Claim From Company Name, Inc."
    mailInfo(4) = mailInfo(4) & "Name:" & " " & txtInsFName.Text & " " & txtInsLName.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Address:" & " " & txtInsAddress.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "City:" & " " & txtInsCity.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "State:" & " " & txtInsState.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Zip:" & " " & txtInsZip.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Home Phone:" & " " & txtInsHome.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Cell Phone:" & " " & txtInsCell.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Loss Date:" & " " & dtpTMS.Value & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Vessel Year:" & " " & txtVslYear.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Vessel Make:" & " " & txtVslMake.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Vessel Model:" & " " & txtVslModel.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Adjuster:" & " " & cboAdjuster.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Client:" & " " & cboClient.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Client Claim #:" & " " & txtClaimNumber.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Our File #:" & " " & txtFileNumber.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Description:" & " " & txtDesc.Text
    ' open new message' in OE
    msgString = msgString & "mailto:" & mailInfo(0)
    msgString = msgString & "?cc=" & mailInfo(1)
    msgString = msgString & "&bcc=" & mailInfo(2)
    msgString = msgString & "&subject=" & mailInfo(3)
    msgString = msgString & "&body=" & mailInfo(4)
    'msgString = msgString + "&Attach=c:\autoexec.bat" ' Only works for some implementations
    ret = ShellExecute(Me.hwnd, vbNullString, msgString, vbNullString, "c:\", 1)
End Sub

It's the txtDesc.text.

I really don't have any special formatting on the text box, but when it goes to the DB and is pulled up again, it's still formatted like it was to start with. It's just when it's sent to e-mail.

Thanks.

Rob
Just my $.02.
 
>With some code I found here with the help of another member.
You mean from me, in thread222-1482974 :)

>but when it goes to the DB and is pulled up again, it's still formatted like it was to start with

No - as I said, the textbox will give the impression that the text is formatted by autowrapping the text (oh, and it also has some default tab stops set up so it will honour any tabbing that you use) - but that layout is imposed by the textbox not by any property of the text.

If you load the same piece of text back into the same textbox it will obviously have the same layout. Put it into a differently sized textbox, or perhaps one using a differently-sized font, or - in your case - an email, and it will have a different layout


One trick that you can play involves an API call that turns the fake linebreaks imposed by the textbox autowrapping into real linebreaks.

We then need to change those into the relevant ASCII codes that mailto will interpret correctly. And you need to be aware that imposing these changes so that the text in the email more closely resembles the text as composed in the textbox will probabluy have repercussions for the process that extracts data from the email to put into the database.

So here's how we'd do it:

1) Add the following declarations to your form:
Code:
[blue]Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal Msg As Long, wParam As Any, lParam As Any) As Long
Private Const EM_FMTLINES = &HC8[/blue]
2) Change
Code:
[blue]mailInfo(4) = mailInfo(4) & "Description:" & " " & txtDesc.Text[/blue]
to
Code:
[blue]SendMessage txtDesc.hWnd, EM_FMTLINES, 1, 0&
mailInfo(4) = mailInfo(4) & "Description:" & " " & Replace(txtDesc.Text, vbCr & vbCrLf, "%0D%0A")[/blue]








 
I'd give a RTB a try. It may not matter but it should only take a few minutes to test.
 
I wouldn't. For at least 2 reasons

1) The mailto protocol (which is what the OP is using*) doesn't understand rich text, just plain text. So you can't say something like

mailInfo(4) = mailInfo(4) & "Description:" & " " & rtbDesc.TextRTF

and expect it to display correctly in the mail client. In fact you will be worse off than when using the textbox.

The alternative, of using the Text property of the RTB of course means that you lose any formatting applied to the text. But that actually isn't important, since

2) The 'formatting' the OP is talking about is limited to CRLFs. And, just like the normal textbox, a RichtextBox does not add real CRLFs either in the RTF or the plain text; the layout is imposed by the size of the RTB not by any property of the text (OK, yes, a user can manually add genuine CRLFs, just as they can in a textbox, but that isn't what we are considering here).
 
Thanks StrongM. Haven't been here in a few days, but I'll let you know how it turns out.

Rob
Just my $.02.
 
I guess I'm doing something wrong. When I change the code like this:

Code:
mailInfo(4) = mailInfo(4) & "Our File #:" & " " & txtFileNumber.Text & "%0D%0A"
SendMessage txtDesc.hWnd, EM_FMTLINES, 1, 0 & mailInfo(4) = mailInfo(4) & "Description:" & " " & Replace(txtDesc.Text, vbCr & vbCrLf, "%0D%0A")

It doesn't even list the Description field in the e-mail anymore. When I do it like this:

Code:
mailInfo(4) = mailInfo(4) & "Our File #:" & " " & txtFileNumber.Text & "%0D%0A"
mailInfo(4) = mailInfo(4) & "Description:" & " " & SendMessage(txtDesc.hWnd, EM_FMTLINES, 1, 0 & Replace(txtDesc.Text, vbCr & vbCrLf, "%0D%0A"))

I get this:

Our File #: 12345
Description: 1

If I use
Code:
mailInfo(4) = mailInfo(4) & "Our File #:" & " " & txtFileNumber.Text & "%0D%0A"
mailInfo(4) = mailInfo(4) & SendMessage(txtDesc.hWnd, EM_FMTLINES, 1, 0 & mailInfo(4) = mailInfo(4) & "Description:" & " " & Replace(txtDesc.Text, vbCr & vbCrLf, "%0D%0A"))

I get this:

Our File #: 12345
1


I can't figure out what I'm messing up here. I did copy the first code to the form too.

Rob
Just my $.02.
 
You are not copying and pasting the code as I gave it

This code:
Code:
[blue]SendMessage txtDesc.hWnd, EM_FMTLINES, 1, 0&
mailInfo(4) = mailInfo(4) & "Description:" & " " & Replace(txtDesc.Text, vbCr & vbCrLf, "%0D%0A")[/blue]

is two lines:
Code:
[blue]SendMessage txtDesc.hWnd, EM_FMTLINES, 1, 0&

mailInfo(4) = mailInfo(4) & "Description:" & " " & Replace(txtDesc.Text, vbCr & vbCrLf, "%0D%0A")[/blue]

Rather than the one line you seem to have tried to make it.
 
Sorry I didn't mention that, but I did it that way first. So the code looks like:

Code:
Private Sub cmdMail_Click()
    'mailInfo(0) = "anexample@somewhere.com"
    'mailInfo(1) = "cc@somehere.com"
    'mailInfo(2) = "bcc@somewhere.com"
    mailInfo(3) = "New Claim From Company, Inc."
    mailInfo(4) = mailInfo(4) & "Name:" & " " & txtInsFName.Text & " " & txtInsLName.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Address:" & " " & txtInsAddress.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "City:" & " " & txtInsCity.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "State:" & " " & txtInsState.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Zip:" & " " & txtInsZip.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Home Phone:" & " " & txtInsHome.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Cell Phone:" & " " & txtInsCell.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Loss Date:" & " " & dtpTMS.Value & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Vessel Year:" & " " & txtVslYear.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Vessel Make:" & " " & txtVslMake.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Vessel Model:" & " " & txtVslModel.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Adjuster:" & " " & cboAdjuster.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Client:" & " " & cboClient.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Client Claim #:" & " " & txtClaimNumber.Text & "%0D%0A"
    mailInfo(4) = mailInfo(4) & "Our File #:" & " " & txtFileNumber.Text & "%0D%0A"
    'open new message' in OE
    SendMessage txtDesc.hWnd, EM_FMTLINES, 1, 0&
    mailInfo(4) = mailInfo(4) & "Description:" & " " & Replace(txtDesc.Text, vbCr & vbCrLf, "%0D%0A")
    msgString = msgString & "mailto:" & mailInfo(0)
    msgString = msgString & "?cc=" & mailInfo(1)
    msgString = msgString & "&bcc=" & mailInfo(2)
    msgString = msgString & "&subject=" & mailInfo(3)
    msgString = msgString & "&body=" & mailInfo(4)
    'msgString = msgString + "&Attach=c:\autoexec.bat" ' Only works for some implementations
    ret = ShellExecute(Me.hWnd, vbNullString, msgString, vbNullString, "c:\", 1)
End Sub

In the txtDesc text box I have:

1

2

3

4

and it comes out like this.

Our File #: 12345
Description: 1234


I tried it so many ways that I forgot about the original. I also tried putting it on the very top of the start of mail(4) and it turned out the same way as above.

I may be missing something else though.

Rob
Just my $.02.
 
Rob,

Way back near the beginning of the thread I asked you
strongm said:
how are you applying 'formatting' in the textbox? Just through allowing it to autowrap? If so then be aware that no actual CRLFs are inserted into the text
to which you replied
rtshort said:
I really don't have any special formatting on the text box
So I have been working from the assumption that you (or whoever has been inputting data to the textbox) have not been inserting manual CRLFs. I even specifically stated that this was one of the assumptions I was using in a later post:
strongm said:
OK, yes, a user can manually add genuine CRLFs, just as they can in a textbox, but that isn't what we are considering here

However, in your '1234' example shown above it is reasonably clear that manual CRLFs are in use. And again we know that the [tt]mailto[/tt] protocol doesn't handle those, they need to be converted. So ...
Code:
[blue]    SendMessage txtDesc.hwnd, EM_FMTLINES, 1, 0&
    Dim strTempDesc As String [green]' you might want to put this in a more appropriate place
    ' handle fake linebreaks imposed by multiline autowrap[/green]
    strTempDesc = Replace(txtDesc.Text, vbCr & vbCrLf, "%0D%0A")
    [green]' Now handle manually inserted CRLFs[/green]
    strTempDesc = Replace(strTempDesc, vbCrLf, "%0D%0A")
    mailinfo(4) = mailinfo(4) & "Description:" & " " & strTempDesc[/blue]
 
Strongm, you are a VB GOD.

I'm sorry I was confused about what you were asking before, but I thank you from the bottom of my heart for sticking with me and helping me figure it out. You are obviously a very nice person.

If I could give you a million stars I would because you are worth it.

Again, thanks a MILLION.


Rob
Just my $.02.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top