I created a procedure to create email drafts in Outlook. Users can modify the email body by writing it on a range, and it's added to the email maintaining all formatting options.
My problem is that while my procedure worked at first, when I started using the word editor Microsoft Outlook starting crashing with the message "Microsoft Outlook has stopped running" and when I kill outlook I get message "The remote procedure failed" on VBA
Why might this be happenning? is there a way to open Outlook before running the code to avoid the error?
Public Sub CreateDraft(Destinatary As String, CC As String, Subject As String, Body As Range, Optional AttachmentPath As String = "")
Dim OutApp As Object
Dim OutMail As Object
Dim WordDoc As Word.Document
Dim WordRange As Word.Range
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.Display
.To = Destinatary
.CC = CC
.Subject = Subject
Set WordDoc = OutApp.ActiveInspector.WordEditor
Set WordRange = WordDoc.Goto(What:=wdGoToSection, Which:=wdGoToFirst) ' The code crashes here
Body.Copy
WordRange.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False
.HTMLBody = .HTMLBody
If (AttachmentPath <> "") Then
.Attachments.Add (AttachmentPath)
End If
.Save
.Close (False)
End With
Application.CutCopyMode = False
Set OutMail = Nothing
Set OutApp = Nothing
End Sub