I have a module in MS Access, that sends an email using a word document as the body of the email. My issue is when the code runs the Word instance stays persistent even though I have closed it out. This is causing code and outlook to hang. My thinking is that possibly outlook is still utilizing the instance so it does not close it out, but this is just a guess.
Does anyone have any insight here on where I am going wrong and how to ensure the word instance is closed out? Thanks in advance for your help.
code....
Sub SendDocAsMsg(Subject, filepath)
Dim wd As Word.Application
Dim doc As Word.Document
Dim itm As Outlook.MailItem
Dim blnWeOpenedWord As Boolean
On Error Resume Next
If wd Is Nothing Then
Set wd = CreateObject("Word.Application")
blnWeOpenedWord = True
End If
wd.Visible = True
Set doc = wd.Documents.Open(FileName:=filepath, ReadOnly:=True)
Set itm = doc.MailEnvelope.Item
With itm
.To = "email@aaa.com"
.Subject = Subject
.SentOnBehalfOfName = "email@aaa.com"
.Send
End With
Set itm = Nothing
wd.Documents(doc).SaveAs FileName:=FilePathPrefix & "MIC_" & EmailFilePath, FileFormat:=wdFormatXMLDocument
wd.ActiveDocument.Close
Cleanup:
If blnWeOpenedWord Then
wd.Quit
End If
Set doc = Nothing
Set wd = Nothing
End Sub
Does anyone have any insight here on where I am going wrong and how to ensure the word instance is closed out? Thanks in advance for your help.
code....
Sub SendDocAsMsg(Subject, filepath)
Dim wd As Word.Application
Dim doc As Word.Document
Dim itm As Outlook.MailItem
Dim blnWeOpenedWord As Boolean
On Error Resume Next
If wd Is Nothing Then
Set wd = CreateObject("Word.Application")
blnWeOpenedWord = True
End If
wd.Visible = True
Set doc = wd.Documents.Open(FileName:=filepath, ReadOnly:=True)
Set itm = doc.MailEnvelope.Item
With itm
.To = "email@aaa.com"
.Subject = Subject
.SentOnBehalfOfName = "email@aaa.com"
.Send
End With
Set itm = Nothing
wd.Documents(doc).SaveAs FileName:=FilePathPrefix & "MIC_" & EmailFilePath, FileFormat:=wdFormatXMLDocument
wd.ActiveDocument.Close
Cleanup:
If blnWeOpenedWord Then
wd.Quit
End If
Set doc = Nothing
Set wd = Nothing
End Sub