Avatar of SWickstrom

asked on 

Error in VBA code Word to Outlook

I have an instance where I need a document which has been created in Word, via a macro, to be sent as the body of an email including formatting.  So I searched here and found some code to use, however, when the document template is open in Word, the macro works.  When the document template is closed and the macro is run, the code fails.  The code I used is included.
What fails is olkMsg.display  -- the macro completes, but no email is shown and switching to Outlook also shows on email.  If I leave the document template open and run the same code, it correctly switches to displaying the newly created email.   [Edited -- just noticed that the code also fails to kill the temporary document created; even that part of the code works if the document template is open when the code is run, but does not delete the file if the doc template is closed].

Dim wrdDoc As Word.Document
    Dim olkApp As Outlook.Application
    Dim olkMsg As Outlook.MailItem
    Dim oDoc As String
        'objFSO As FileSystemObject
        'objFile As TextStream
    Set wrdDoc = ActiveDocument
    'Save the document in the proper format.  If the document is already saved in the proper format,
    'then you could change the command from SaveAs to Save.
    wrdDoc.SaveAs , wdFormatFilteredHTML
    'Read the document in using the FileSystemObject
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(wrdDoc.Path & "\" & wrdDoc.Name, 1, False)
    'Get the open instance of Outlook
    Set olkApp = GetObject(, "Outlook.Application")
    'Create a new message
    Set olkMsg = olkApp.CreateItem(0)
    'Add recipients
    olkMsg.Recipients.Add sEmail
    'Add the subjec line
    olkMsg.Subject = sMyCaseName & " Case Closure"
    'Read the saved Word doc into the body of the message
    olkMsg.HTMLBody = objFile.ReadAll
    oDoc = (wrdDoc.Path & "\" & wrdDoc.Name)
    'Close the Word Document
    ActiveDocument.Saved = True
    'Destroy all the objects to avoid memory leaks
    Set wrdDoc = Nothing
    Set objFile = Nothing
    Set objFSO = Nothing
    'Delete the saved form
    Kill oDoc
'Display the message
  Set olkApp = Nothing
  Set olkMsg = Nothing
Unload Me

Open in new window

Microsoft WordOutlook

Avatar of undefined
Last Comment
Avatar of David Lee
David Lee
Flag of United States of America image

Hi, SWickstrom.

That looks like code I wrote.

"When the document template is closed and the macro is run, the code fails."
The code is designed to work from the active document in Word.  I guess I don't understand what it is you're trying to do.  
Avatar of SWickstrom

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial

Microsoft Outlook is a personal information manager from Microsoft, available as a part of the Microsoft Office suite. Although often used mainly as an email application, it also includes a calendar, task manager, contact manager, note-taker, journal, and web browser.

Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews


IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo