Code that creates Outlook email fails every other time I run it

mcomp72

Active Member
Joined
Aug 14, 2016
Messages
275
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2011
Platform
  1. Windows
  2. MacOS
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.

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?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,214,787
Messages
6,121,561
Members
449,038
Latest member
Guest1337

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top