?
Solved

VB 6.0    String question

Posted on 2003-02-19
12
Medium Priority
?
347 Views
Last Modified: 2010-08-05
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?
0
Comment
Question by:themroc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 7980557
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.
0
 
LVL 3

Expert Comment

by:daffyduck14mil
ID: 7980562
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
0
 
LVL 1

Expert Comment

by:sazhagianambi
ID: 7980855
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

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:dmang
ID: 7982699
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

 
0
 
LVL 1

Expert Comment

by:sazhagianambi
ID: 7991837
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
0
 
LVL 1

Expert Comment

by:sazhagianambi
ID: 7992174
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
0
 

Author Comment

by:themroc
ID: 7992514
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:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20516835.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
0
 

Author Comment

by:themroc
ID: 8007818
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

????????????????????????
0
 
LVL 3

Expert Comment

by:daffyduck14mil
ID: 8070136
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.
0
 

Author Comment

by:themroc
ID: 8070761
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!
0
 
LVL 3

Accepted Solution

by:
daffyduck14mil earned 480 total points
ID: 8071145
>does it mean it would not work in every envirnonment?
Correct. There must be a working e-mail system in place for MAPI to work. This is a requirement for both client and server.

Thus, if your program can be started on a server (or network, I don't exactly understand what you mean by that word), it must have a MAPI enabled mail-system. But, this is a requirement you can check and enforce. Even with the ShellExecute solution, you still have the same requirement. And yes, starting the program on the network might yield problems with ShellExecute.

Using MAPI is fairly simple, it has a component in VB, after you add it to the toolbox, add a Mapi Session, and a Mapi Message. Then, a command box, and use:

Private Sub cmdMail_click()
  MAPISession1.SignOn
  MAPIMessages1.SessionID = MAPISession1.SessionID
  MAPIMessages1.Compose
  MAPIMessages1.RecipType = RecipTypeConstants.mapToList
  MAPIMessages1.RecipDisplayName = "someone@somewhere.com"
  MAPIMessages1.MsgSubject = "Test message"
  MAPIMessages1.MsgNoteText = "This is a test message using" & vbCrLf & "MAPI."
  MAPIMessages1.Send
  MAPISession1.SignOff
End Sub

The above code-snippet is the basis of sending an E-mail message through MAPI and VB.

Hope this helps you somewhat.

Grtz.&copy;

D.

P.s. The Grtz.&copy; ain't my name... ;-)
0
 

Author Comment

by:themroc
ID: 8071663
thanks duffyduck14,

that soves some of my problems I had before .
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question