Hello,
I have a report that converts to .pdf with one button in my form and is saved in a set location.
What I would like is another button that you click which finds the file (This worked alright), and then open outlook and fills in the body, subject and the signature automatically.
What I tried was creating a module with the code further down below on this post, and then adding it the to the button and it seemed to work fine; it opened a new outlook message and allowed me to send it, however there were a few problems to this:
- The code will not work if Outlook is already open, if it is open I get this error: 'Run-Time error '429', Could not create object'.
- The signature will not be added to the Outlook message, even though in my outlook I have a .htm signature as default.
- The message will not send until I open Outlook - So Outlook is never opened, just a new message, and I cannot have outlook open or it will not work.
I have tried using both objOutlook = New Outlook.Application and objOutlook = CreateObject(''Outlook.Application"), but the first only runs when Outlook is closed, and badly, and the second comes up with the same error as above when I try it with Outlook open, and this error when Outlook is closed: 'Run-Time error '438', Object doesn't support this property or method'.
Here is the code I use in the module:
Code:
Sub SendMessage(DisplayMsg As Boolean, Optional AttachmentPath)
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
Dim SigString As String
Dim Signature As String
' Create the Outlook session.
Set objOutlook = New Outlook.Application
' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
' Set the Subject, Body, and Importance of the message.
.Subject = "This is an Automation test with Microsoft Outlook"
.Body = "This is the body of the message." & vbCrLf & vbCrLf
.Importance = olImportanceHigh 'High importance
.a
' Add attachments to the message.
If Not IsMissing(AttachmentPath) Then
Set objOutlookAttach = .Attachments.Add(AttachmentPath)
End If
' Resolve each Recipient's name.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next
' Should we display the message before sending?
If DisplayMsg Then
.Display
Else
.Save
.Send
End If
I am also using MS Access 97 for the databases and reports, and Outlook 2003 to send messages.