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 = "%0D%0A"
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 & "?Subject=" & CurrentView.Body.fbxSubject.Text
'Add the message body contents
strTmp = strTmp & "&Body=" & strBody
'Create the e-mail
intRtn = Shell("rundll32.exe url.dll,FileProtocolHandler mailto:" & 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 "shell32.dll" _
Alias "ShellExecuteA" (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 = "c:\windows\temp\tmp01.eml"
'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, "From: <sales@northwindit.co.uk>"
'Add the recipient
Print #hFile, "To: " & Chr$(34) & CurrentView.Body.fbxEMail.Text & Chr$(34)
'Add the subject
Print #hFile, "Subject: " & CurrentView.Body.fbxSubject.Text
Print #hFile, "X-Unsent: 1"
Print #hFile, 'Blank line
'Add the body
Print #hFile, strBody
Print #hFile, 'Blank line
Close #hFile
'Create the e-mail
lngRtn = ShellExecute(hWnd, "Open", 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, "Win32 API functions". 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