Macro used to run on Outlook 2013, now doesn't run on Outlook 2021 on a new computer

377 Views Asked by At

A few years ago I had a developer write a macro for me to print the current email as two pages per page rather than on two separate pages. It used to run successfully on my old computer Win 10/Outlook 2013. I now have a new computer Win10/Outlook 2021 It now comes up with a compile error "User defined type not defined" for the line Dim wdApp As Word.Application

I only have a rudimentary grasp of VBA so am unable to solve this one. Any help would be greatly appreciated.

Code is as follows:

Option Explicit
Public Sub print_mail()
    Dim objOL                      As Outlook.Application
    Dim objMsg                     As Outlook.MailItem
    Dim objAttachments             As Outlook.Attachments
    Dim objSelection               As Outlook.Selection
    Dim i                          As Long
    Dim lngCount                   As Long
    Dim Response                   As Integer
    Dim msg                        As String
    Dim strSubject                 As String
    Dim currentItem As Object
 
    Set objOL = CreateObject("Outlook.Application")
    Set objSelection = objOL.ActiveExplorer.Selection

   For Each currentItem In objSelection
      If currentItem.Class = olMail Then
                Set objMsg = currentItem
                PrintFirstPage objMsg
   End If
   Next
 
    Set objAttachments = Nothing
    Set objMsg = Nothing
    Set objSelection = Nothing
    Set objOL = Nothing
End Sub

Public Sub PrintFirstPage(Mail As Outlook.MailItem)
  Dim wdApp As Word.Application
  Dim wdDoc As Word.Document
  Dim olDoc As Word.Document

  Set wdApp = CreateObject("Word.Application")
  Set wdDoc = wdApp.Documents.Add(Visible:=True)
  Set olDoc = Mail.GetInspector.WordEditor
  olDoc.Range.Copy
  wdDoc.Range.Paste

'  With wdDoc
'    .PageSetup.Orientation = wdOrientLandscape
'  End With
  'wdDoc.PrintOut
      wdApp.PrintOut FileName:="", Range:=wdPrintRangeOfPages, Item:= _
        wdPrintDocumentWithMarkup, Copies:=1, Pages:="1-2", PageType:= _
        wdPrintAllPages, Collate:=True, Background:=True, PrintToFile:=False, _
        PrintZoomColumn:=2, PrintZoomRow:=1, PrintZoomPaperWidth:=0, _
        PrintZoomPaperHeight:=0
    
  wdDoc.Close False
  wdApp.Quit
End Sub
2

There are 2 best solutions below

0
On

Use an untyped variable:

Dim appWD as Object
appWD = CreateObject("Word.Application")

Or try to add the Word object library reference to the project.

Inside the Visual Basic Editor , select Tools then References and scroll down the list until you see Microsoft Word 12.0 Object Library. Check that box and hit Ok.

0
On

VBA macros are not designed for distributing on multiple machines. If you need to deploy your code on a wide range of machines you would better consider transforming your solution to Office add-ins - it can be a COM add-in or a web-based one. See Walkthrough: Create your first VSTO Add-in for Outlook for more information.

When you move your VBA code to another machine you need to make sure that all COM references are added as it was on your original machine.

In order to solve your problem, you have to add the Word object library reference to your project.

Inside the Visual Basic Editor, select Tools then References and scroll down the list until you see Microsoft Word XX.0 Object Library. Check that box and hit Ok.

From that moment, you should have the auto complete enabled when you type Word. to confirm the reference was properly set.

Note, you could also use the late binding technology which doesn't require adding COM references:

' No reference to a type library is needed to use late binding.
' As long as the object supports IDispatch, the method can 
' be dynamically located and invoked at run-time.

' Declare the object as a late-bound object
  Dim oWord As Object

  Set oWord = CreateObject("Word.Application")

' The Visible property is called via IDispatch
  oWord.Visible = True

So, to start an Word Automation session, you can use either early or late binding. Late binding uses either the Visual Basic GetObject function or the CreateObject function to initialize Word. See Using early binding and late binding in Automation for more information.