Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word instance is not closing in MS Access VBA module 1

Status
Not open for further replies.

airon

Programmer
Jun 21, 2001
21
US
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
 
1) When and if you get the code right it should run without error so remove the line

On Error Resume Next

because it will ony mask any problems you may have.

2) You have Created a Word Object but you have not created an Outlook Object ... so itm is an unqualified reference.

 
How are ya airon . . .

I added the following lines ([purple]in purple[/purple]) and can't get it to error. Note: received error before adding the lines.
Code:
[blue]   wd.Documents(doc).SaveAs FileName:=FilePathPrefix & "MIC_" & EmailFilePath
   [purple][b]DoEvents[/b][/purple]
   wd.ActiveDocument.Close
   [purple][b]DoEvents[/b][/purple][/blue]
[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Hooray I think I got something figured out, with and added bonus…
A little more detail on the initial issue I was having. When I would execute this module it would execute without error. However, after execution is where the problem occurred, I would navigate to Outlook which would persistently hang. This behavior was observed until I manually killed the WINWORD.EXE application the code left open in the Windows Task Manager. Once the manual kill action was executed Outlook was once again responsive.
Steps to resolve:
1. Removed MailEnvelope.item section of code from module
a. Executed code to confirm application WINWORD.EXE would open and close as expected
b. This test was successful
c. Re-inserted MailEnvelope.item into module
2. Performed further research on MailEnvelope.item properties
a. Identified and implemented property .close olSave
b. Tested implementation code now functions as desired, WINWORD.EXE is opened and closed as desired
3. Added Bonus
a. With implementing .close olSave property, I am now able to bypass the auto executed spell checker that was being initiated before. I do not need this functionality as I am already checking spelling in a different process.
b. Removed code line setting application to display as I no longer need to guide the user through the spell check of their document.
Just want to say thanks to those who chimed in on this one, I have been saved by this forum countless of times.
New Code Implemented:
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

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"
.Close olSave
.Send
End With

Set itm = Nothing
wd.Documents(doc).SaveAs FileName:=”MIC_" & EmailFilePath, FileFormat:=wdFormatXMLDocument
wd.ActiveDocument.Close

Cleanup:
If blnWeOpenedWord Then
wd.Quit
End If

Set doc = Nothing
Set wd = Nothing

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top