.Display does not work when Outlook has not been started yet

  • Thread starter Thread starter BigBirdNL
  • Start date Start date
B

BigBirdNL

Hi,
To send an attachment from excel VBA, I create an MailItem, attach a file
and display the mail item for the user to be able to add a receipient.
This works fine when Outlook has already been started. Though, when Oulook
is not started, it askes for the profile I want to use, mentions that Word is
used as email editor and crashes with "Run-time error '-1630519291
(9ed04005)': The operation failed." The debugger stops on the .Display method.
How can I make this work even when Oulook is not started yet?

My code:

Sub SendByMail(cBevNr, cKorteOmschrijving, cBijlageFile)
' creates and sends a new e-mail message with Outlook
' requires a reference to the Microsoft Outlook 11.0 Object Library
Dim OLF As Outlook.MAPIFolder, olMailItem As Outlook.MailItem
Dim ToContact As Outlook.Recipient
Set OLF = GetObject("", _

"Outlook.Application").GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
Set olMailItem = OLF.Items.Add ' creates a new e-mail message
With olMailItem

'Subject
.Subject = cBevNr & ": " & cKorteOmschrijving ' message subject

'Mailbody
' the message text with a line break
.Body = "Bijgesloten de bijlagen behorend bij bevinding " & cBevNr &
Chr(13)

'Attachements
.Attachments.Add cBijlageFile, olByValue, , _
"Attachment" ' insert attachment

'Confirmations
.OriginatorDeliveryReportRequested = False ' delivery confirmation
.ReadReceiptRequested = False ' read confirmation

'Display
.Display

End With
Set ToContact = Nothing
Set olMailItem = Nothing
Set OLF = Nothing
End Sub
 
Problem solved.
The problem occurred because I did not initialize Outlook. Below is the
corrected code.

Sub SendByMail(cBevNr, cKorteOmschrijving, cBijlageFile)
' creates and sends a new e-mail message with Outlook
' requires a reference to the Microsoft Outlook 11.0 Object Library
Dim ol As Outlook.Application
Dim OLF As Outlook.MAPIFolder
Dim olItem As Outlook.MailItem
Dim ToContact As Outlook.Recipient

Set ol = CreateObject("Outlook.Application")
Set OLF = GetObject("", _

"Outlook.Application").GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
Set olItem = ol.CreateItem(olMailItem)
With olItem

'Subject
.Subject = cBevNr & ": " & cKorteOmschrijving ' message subject

'Mailbody
' the message text with a line break
.Body = "Bijgesloten de bijlagen behorend bij bevinding " & cBevNr &
Chr(13)

'Attachements
.Attachments.Add cBijlageFile, olByValue, , _
"Attachment" ' insert attachment

'Confirmations
.OriginatorDeliveryReportRequested = False ' delivery confirmation
.ReadReceiptRequested = False ' read confirmation

'Display
.Display

End With
Set ToContact = Nothing
Set olItem = Nothing
Set OLF = Nothing
End Sub
 
Back
Top