几年前,我让一个开发人员为我编写一个宏,将当前的电子邮件打印为每页两页,而不是在两个单独的页面上。它曾在我的旧电脑Win 10/Outlook 2013上成功运行。我现在有了一台新计算机with 10/Outlook2021,它现在为Dim wdApp作为Word.Application行产生了一个编译错误“用户定义类型未定义”
我只对VBA有一个基本的掌握,所以无法解决这个问题。任何帮助都将不胜感激。
守则如下:
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发布于 2022-03-10 09:39:07
使用非类型化变量:
Dim appWD as Object
appWD = CreateObject("Word.Application")或尝试将Word对象库引用添加到项目中。
在Visual Basic Editor中,选择Tools,然后选择References并向下滚动列表,直到看到Microsoft Word 12.0 Object Library为止。选中那个框并点击Ok。
发布于 2022-03-27 17:24:27
VBA宏不是为在多台机器上分发而设计的。如果您需要在广泛的机器上部署代码,您最好考虑将您的解决方案转换为Office外接程序--它可以是COM外接程序,也可以是基于web的外接程序。有关详细信息,请参阅演练:为Outlook创建第一个VSTO外接程序。
当您将VBA代码移动到另一台计算机时,您需要确保所有COM引用都与原来机器上的COM引用相同。
为了解决问题,必须将Word对象库引用添加到项目中。
在Visual Basic Editor中,选择Tools,然后选择References并向下滚动列表,直到看到Microsoft Word XX.0 Object Library为止。选中那个框并点击Ok。
从那一刻起,当您输入Word.以确认正确设置了引用时,应该启用自动完成。
注意,您还可以使用不需要添加COM引用的后期绑定技术:
' 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因此,要启动Word自动化会话,可以使用早期绑定或后期绑定。后期绑定使用Visual GetObject函数或CreateObject函数初始化Word。有关详细信息,请参阅自动化中使用早期绑定和后期绑定。
https://stackoverflow.com/questions/71420987
复制相似问题