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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

composing new message in outlook 1

Status
Not open for further replies.

ikim2000

MIS
May 23, 2002
22
CA
How can I automate oultlook so that I can provide the user with a new message with something like "to:abc@abc.com" and is ready for the user to just type in the body of the message?

thanks,
ik.
 
Look into the MAPI controls......or the Outlook Object Library...this should get you started. :)
 
Ikim2000 - I see that you have started 13 threads, and have only said thank you once, and have yet to indicate that any post has been helpful.

Since you're fairly new to the forum, yet may not be aware that Tek-Tips is a forum by and between professionals, it is not a tech support site, and its a good practice to share as you can with other members of the forum and to show professional courtesy when warranted.

That being said, I suggest that you do a search on this forum for MAPI - <tappety tappety tap> - Ah Yes : try thread222-485548 where I answered the question today

See also faq222-2244
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
//johnwm (Programmer)//

dude...give him a break....he might just be a newbie to the crappy vb language....all his posts are valid questions.

have a good day. :)

 
You don't need to automate Outlook to create a pre-addressed mail item. Shelling rundll32.exe to call the mailto protocol handler will do it:

strRecip = &quot;abc@abc.com&quot;
intRtn = Shell(&quot;rundll32.exe url.dll,FileProtocolHandler mailto:&quot; & strRecip)

Mailto also let's you add the subject and message body but is limited to a few hundred chars in total. It can't add attachments or send the e-mail. For anything more complicated you need to automate Outlook. Try as a good starting point for code examples.

Paul Bent
Northwind IT Systems
 
Paul,
Neat tip! I never thought of this approach. Can you let us in on the syntax for adding the subject and/or body text? I searched the net but didn't find much info on subject and body...
Thanks,
sdraper
 
If you want to do something that requires automation, are using Outlook 98, 2000 or 20002 and don't want to spend time learning the object model then check out my OLServices ActiveX library; evaluation copy available at my web site.

Re Mailto: the following is an extract from an FAQ I wrote for a Lotus Approach support site that I host. The LotusScript language is very similar to VBA so you should be able to adapt this where necessary. It shows how to add the recipient, subject and body. Multiple recipients should be separated with semi-colons. Search the web for RFC2368 for more details of the mailto protocol.

>>You can't use Chr(13) & Chr(10) to produce a carriage return in the body; mailto is an internet protocol and expects the equivalent HTML codes. I ususally declare a constant in Globals - Declarations

Public Const HTML_CR = &quot;%0D%0A&quot;

If you're merging text containing carriage returns from the database into the e-mail body then you need to replace them with the above HTML code. This function can be used to carry out the replacement:

Public Function fReplaceCR(Byval strText As String) As String

'--- Strips Lf's and Cr's from a string and replaces them with
'--- a html encoded line break - %0D%0A
'--- a public contstant HTML_CR has been declared

Dim intP1 As Integer, intP2 As Integer

intP1 = 1
Do While Instr(intP1, strText, Chr$(10)) > 0
intP2 = Instr(intP1, strText, Chr$(10))
strText = Trim$(Left$(strText, intP2 - 1)) & _
HTML_CR & Trim$(Right$(strText, Len(strText) - intP2))
intP1 = intP2 + 1
Loop

intP1 = 1
Do While Instr(intP1, strText, Chr$(13)) > 0
intP2 = Instr(intP1, strText, Chr$(13))
strText = Trim$(Left$(strText, intP2 - 1)) & _
Trim$(Right$(strText, Len(strText) - intP2))
intP1 = intP2
Loop

fReplaceCR = strText

End Function

Here's an example subroutine that creates an e-mail addressed to the current record in an Approach database. The e-mail address is displayed in a fieldbox named fbxEmail. The content is read from other fields on the view.

Sub CreateEMail

Dim intRtn As Integer 'Shell function return value
Dim strBody As String 'Message body
Dim strTmp As String 'Temp string buffer

'Build the e-mail body from the salutation, letter and sign off fields
'Replace carriage returns with HTML codes
strBody = CurrentView.Body.fbxSalutation.Text & HTML_CR & HTML_CR
strBody = strBody & fReplaceCR(CurrentView.Body.fbxLettText.Text) _
& HTML_CR & HTML_CR
strBody = strBody & fReplaceCR(CurrentView.Body.fbxSignOff.Text)

'Build the full content: headers and body
'Add the recipient
strTmp = CurrentView.Body.fbxEMail.Text
'Add the subject
strTmp = strTmp & &quot;?Subject=&quot; & CurrentView.Body.fbxSubject.Text
'Add the message body contents
strTmp = strTmp & &quot;&Body=&quot; & strBody

'Create the e-mail
intRtn = Shell(&quot;rundll32.exe url.dll,FileProtocolHandler mailto:&quot; & strTmp)

End Sub

3. EML File. If the .eml file extension is registered on the system, you can overcome the mailto limitation that restricts the total content to a few hundred characters. The method is to create a text file, with a .eml extension, containing the e-mail content then Shell the file. Through its registered association and action on open, an e-mail will be created from the file using Outlook Express.

We'll adapt the above example to use a .eml file. In this case the normal Chr(13) and Chr(10) characters are used for carriage returns in the e-mail body.

Also, the LotusScript Shell function can't be used because it will only run an executable. We need to use the Windows ShellExecute function which is declared in Globals - Declarations as follows:


Declare Public Function ShellExecute Lib &quot;shell32.dll&quot; _
Alias &quot;ShellExecuteA&quot; (Byval hwnd As Long, _
Byval lpOperation As String, Byval lpFile As String, _
Byval lpParameters As String, Byval lpDirectory As String, _
Byval nShowCmd As Long) As Long

Sub CreateEMail

Dim hWnd As Long 'Handle to Approach window
Dim lngRtn As Long 'API function return value
Dim hFile As Integer 'Handle to the .eml file
Dim strFile As String 'Full path/filename of .eml file
Dim strBody As String 'Message body

'Assign the output filename
strFile = &quot;c:\windows\temp\tmp01.eml&quot;

'Get the Approach window handle
hWnd = CurrentWindow.GetHandle

'Build the e-mail body from the salutation, letter and sign off fields
strBody = CurrentView.Body.fbxSalutation.Text & _
Chr$(13) & Chr$(10) & Chr$(13) & Chr$(10)
strBody = strBody & CurrentView.Body.fbxLettText.Text _
& Chr$(13) & Chr$(10) & Chr$(13) & Chr$(10)
strBody = strBody & CurrentView.Body.fbxSignOff.Text
'Get a free file handle
hFile = FreeFile
'Open the .eml file for writing
Open strFile For Output As hFile
'Add the reply address
Print #hFile, &quot;From: <sales@northwindit.co.uk>&quot;
'Add the recipient
Print #hFile, &quot;To: &quot; & Chr$(34) & CurrentView.Body.fbxEMail.Text & Chr$(34)
'Add the subject
Print #hFile, &quot;Subject: &quot; & CurrentView.Body.fbxSubject.Text
Print #hFile, &quot;X-Unsent: 1&quot;
Print #hFile, 'Blank line
'Add the body
Print #hFile, strBody
Print #hFile, 'Blank line
Close #hFile

'Create the e-mail
lngRtn = ShellExecute(hWnd, &quot;Open&quot;, strFile, Null, Null, 1)

End Sub

Note that you can use the Windows API to get the path to the Windows temp folder. It's covered in the discussion, &quot;Win32 API functions&quot;. Unfortunately you can't delete the .eml file from the script immediately after creating the e-mail. It is deleted too quickly and you will get an error message from Outlook Express that the .eml file was not found.<<

Paul Bent
Northwind IT Systems
 
Thanks Paul! That answered my question and several others that popped into my head after the fact. I use the Outlook object model extensively in my everyday code and sometimes it seems a bit of overkill. This little ditty will be great for a quick notificaiton email and the like. Really appreciate your information
Thanks Again,
sdraper
 
I use this in one of my apps:

' Shell to default mail-client
Shell &quot;start mailto:&quot; & txtMailTo.Text

If you want you can supply a subject and even a partial body for the email.

Shell &quot;start mailto:&quot; & txtMailTo.Text & &quot;?subject=&quot; & txtSubject.Text & &quot;&body=&quot; & txtBody.Text

In the same manner as the body parameter, you can supply CC and BCC. It works kinda like passing parameters to a webpage.

Quite simple but it works
 
Great answer paulbent about alternative answer to automating Outlook..when i use your method, i don't need to worry about the system's email client type, whether the instance already exists, it's much easier if i just want to send an email. thanx,Ike.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top