I have a sub that copies the contents of a Word document into an Outlook email message. The first time I run it, it works fine. The second time, I get a run-time error. The third time, it works fine. The fourth time -- you guessed it, a run-time error. I can't understand why it works fine half the time but fails the other half.
The variable Doc is a Word document that was created in the sub that calls this sub.
When I get the error, it is on this line:
And the exact error is:
Run-time error '5097': Word has encountered a problem.
Before the code runs, I have another sub that checks to see if any instance of Word is open, and if it is, it prompts the user to close it. No idea if that might be related to the problem, but here's that code.
This code works just as it should, and when I click YES to close an instance of Word, that's usually when everything works fine.
IsWordRunning is another Function. It checks if there is any instance of Word currently running.
Any idea why it would work half the time and not the other half?
VBA Code:
Sub NewMemberEmailFromWordDoc(Doc As Object, EmailAddr As String, SchPDFName As String)
Dim OutApp As Object
Dim OutMsg As Object
Dim editor As Object
Doc.Content.Copy
Set OutApp = CreateObject("Outlook.Application")
Set OutMsg = OutApp.CreateItem(0)
With OutMsg
Set editor = .GetInspector.WordEditor
editor.Content.Paste
.To = EmailAddr
.Subject = "Welcome to the Group"
If SchPDFName <> "" Then .Attachments.Add SchPDFName
.Display
End With
Set editor = Nothing
Set OutMsg = Nothing
Set OutApp = Nothing
End Sub
The variable Doc is a Word document that was created in the sub that calls this sub.
When I get the error, it is on this line:
VBA Code:
editor.Content.Paste
And the exact error is:
Run-time error '5097': Word has encountered a problem.
Before the code runs, I have another sub that checks to see if any instance of Word is open, and if it is, it prompts the user to close it. No idea if that might be related to the problem, but here's that code.
VBA Code:
Public Function ConfirmEmailShouldBeCreated() As Boolean
Dim Answer As VbMsgBoxResult
If IsWordRunning = True Then
Answer = MsgBox("Microsoft Word is currently running. It is best if it is closed completely before continuing. Please save any Word files you have open (if you want them to be saved), as Word is about to be closed. When you are ready to continue, press Yes. If you want to cancel the operation, press No.", vbYesNo)
If Answer = vbYes Then
GetObject(, "Word.Application").Quit False
ConfirmEmailShouldBeCreated = True
End If
Else
ConfirmEmailShouldBeCreated = True
End If
End Function
This code works just as it should, and when I click YES to close an instance of Word, that's usually when everything works fine.
IsWordRunning is another Function. It checks if there is any instance of Word currently running.
VBA Code:
Public Function IsWordRunning() As Boolean
IsWordRunning = GetObject("winmgmts:\\.\root\cimv2").ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'WINWORD.EXE'").Count > 0
End Function
Any idea why it would work half the time and not the other half?