Link to home
Start Free TrialLog in
Avatar of themroc
themroc

asked on

VB 6.0 String question

With the help of some experts I figured out how to launch an email programme with subject line and body message.

The body message can be set as a string for example

Dm bodymessage as String

Now my problem:
I would like to write a message over several lines in the body section of the Email with predefined Line changes.

for example:
Dear sirs    [linechange]
text text....   [linechange]

Regards       [linechange]

How do I invoke the Linechange or Carriage return in a string variable?
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

You just need to add something like this:

bodymessage = "Dear Sirs" & vbCR & "text text" & vbCR & "Regards" & vbCR

Or use the Linefeed character vbLF or the combination code vbCRLf

Just try it out with one or more of these and determine which gives you the best results.
Avatar of daffyduck14mil
daffyduck14mil

Hi,

First off, it's not worth the points you are willing to give.

Second, the answer to your question is the string concatenation operator (&) and the constant vbLf or vbCrLf.

Thus, it makes.
Dim BodyMessage as string

BodyMessage = "Dear sirs," & vbCrLf
BodyMessage = BodyMessage & "Text...text..." & vbCrLf
BodyMessage = BodyMessage & "Regards," & vbCrLf

The vbLf means a LineFeed, vbCr means Carriage Return, and vbCrLf means a LineFeed and a Carriage return.

Grtz.©

D.

P.s. 1 or 2 points will do nicely.. not 100 ;p
Hi friend,

I Think U want to add LineChar after Some Number of Characters.

Assume Every Line Having 50 Char, Use Following,

Dim Temp as string
For i=50 to Len(BodyMessage) step 50
   Temp=Temp+ Mid(Str, i, 2)+ vbcrlf
Next
BodyMessage=Temp


I Hope this will be Useful

Nambi

I think that with the last code offered (Nambi) that you have the potential for having words unnaturally split from one line to the next in the text body.
Try the function below with a block of text as messagein and your linelength specified as parameters.
The result is a text block with lines ending with full words

Function BuildMessage(MessageIn As String, _
                      iLineLength As Integer) As String

    Dim l As Integer
    Dim iPos As Integer
    Dim iStart As Integer
    Dim MessageOut As String
    Dim WorkingLine As String
   
    l = Len(MessageIn)
    iStart = 1
    While iStart + iLineLength < Len(MessageIn)
       
        If Mid$(MessageIn, iStart + iLineLength - 1, 1) = " " Or _
            Mid$(MessageIn, iStart + iLineLength, 1) = " " Then
                WorkingLine = Trim(Mid$(MessageIn, iStart, iLineLength))
                MessageOut = MessageOut & WorkingLine & vbCrLf
                iStart = iStart + Len(WorkingLine) + 1
        Else
            WorkingLine = Mid$(MessageIn, iStart, iLineLength)
            For i = iLineLength To 1 Step -1
                If Mid$(WorkingLine, i, 1) = " " Then
                    WorkingLine = Left(WorkingLine, i - 1)
                    MessageOut = MessageOut & WorkingLine & vbCrLf
                    iStart = iStart + Len(WorkingLine) + 1
                    Exit For
                End If
            Next i
        End If
       
        If (l - iStart) < iLineLength Then
           iLineLength = l - iStart
        End If
    Wend
    MessageOut = MessageOut & Mid$(MessageIn, iStart, iLineLength + 2)
    BuildMessage = MessageOut
 
End Function

 
Hi Dmang ,


There is a some indifinite loop in While Loop. Also thi code Works Lovely until the Empty Space Reaches. After that it split every Char as a Text. Kindly check it This.I can able to Make correct that indifinte Loop, But Not on later Part.


Regards,
Nambi
Hi Friend,

I Hope this Sholu Give Output as u Expect. I Did som Minor changes in DMang's Code(I Hope he doesn't mind it).


Function BuildMessage(MessageIn As String, _
                     iLineLength As Integer) As String

   Dim l As Integer
   Dim iPos As Integer
   Dim iStart As Integer
   Dim MessageOut As String
   Dim WorkingLine As String
   
   l = Len(MessageIn)
   iStart = 1
   While iStart + iLineLength <= Len(MessageIn)
        'MsgBox Mid$(MessageIn, iStart + iLineLength - 1, 1)
       If Mid$(MessageIn, iStart + iLineLength - 1, 1) = " " Or _
           Mid$(MessageIn, iStart + iLineLength, 1) = " " Then
               WorkingLine = Trim(Mid$(MessageIn, iStart, iLineLength))
               MessageOut = MessageOut & WorkingLine & vbCrLf
               iStart = iStart + Len(WorkingLine) + 1
       Else
           WorkingLine = Mid$(MessageIn, iStart, iLineLength)
           Dim i
           For i = iLineLength To 1 Step -1
               If Mid$(WorkingLine, i, 1) = " " Then
                   WorkingLine = Left(WorkingLine, i - 1)
                   MessageOut = MessageOut & WorkingLine & vbCrLf
                   iStart = iStart + Len(WorkingLine)
                   Exit For
               End If
           Next i
           iStart = iStart + iLineLength
           MessageOut = MessageOut & WorkingLine & vbCrLf
           'MsgBox MessageOut
       End If
       If (l - iStart) <= iLineLength Then
          iLineLength = l - iStart
          If iStart = l Then
            iStart = iStart + 1
          End If
       End If
   Wend
   MessageOut = MessageOut & Mid$(MessageIn, iStart - 1, iLineLength + 2)
   BuildMessage = MessageOut

'Consider "Nambi How Are U?" as Input String
' 2 is the Count to Make Line break
'Output is
' Na
' Mb
' i
' Ho
' W
' Ar
' e
' U?

regards,
Nambi
Avatar of themroc

ASKER

Apparently it is not so easy as expected, at least I did not get an working answer!

From TimCottee I got the code how to launch an Email programme from a VB application, Question:
https://www.experts-exchange.com/questions/20516835/VB-6-0-launching-an-email-programme-from-an-VB-application.html

It all works fine, the solution was to create the following subroutine:
__________________
Public Sub MailMessage(ByVal MailTo As String, ByVal Subject As String, ByVal Body As String)
   ShellExecute Me.hwnd, vbNullString, "mailto:" & MailTo & "?Subject=" & Subject & "&Body=" & Body, vbNullString, "C:\", SW_SHOWNORMAL
End Sub
_______________

Where body is the textstring I would like to manipulate
Even if I use the vbCR or vbCRlf command nothing happend  the text is still in one line of my email body
e.g for duffyduck14 it reads:
Dear sirs,Text...text...Regards,

What is wrong, I think the answer is worth more than 1 or two pints, therefore I increase it to 120
Avatar of themroc

ASKER

I stuck a little bit, what is the recomendation of the experts. Should I place the question again with a different header or is it worth to wait longer for an answer

????????????????????????
Um,

If your purpose is to send an e-mail message using the default mailsystem, and format the text you want to put into it...

I would not use a shellexecute method. Might work for little things, but when you get to a more complex sytem, it's not so usefull anymore. I would use CDO, or MAPI. Mapi is standard, and most (read "commercial") e-mail packages respond to it.

If my memory serves, Mailto: will launch the default MAPI enabled mail-system.

Grtz.&copy;

D.
Avatar of themroc

ASKER

gritz & copy,

does it mean it would not work in every envirnonment?
for example if it runs on an network?

So how do I launch mailto
do I have to implement the MAPI controll into my application.

The problem I have the programme I wrote might in some cases run on an network would this be effected by the shellexecute methode???

P.S. I solved the above mentoned problem by doing a not very elegant procedure.
I add always about 300 blanks to the line where I want the linebreak, then in the email it comes up with a linebreak!
ASKER CERTIFIED SOLUTION
Avatar of daffyduck14mil
daffyduck14mil

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of themroc

ASKER

thanks duffyduck14,

that soves some of my problems I had before .