首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >宏以前运行在Outlook 2013上,现在不在新计算机上运行Outlook 2021

宏以前运行在Outlook 2013上,现在不在新计算机上运行Outlook 2021
EN

Stack Overflow用户
提问于 2022-03-10 08:32:13
回答 2查看 157关注 0票数 1

几年前,我让一个开发人员为我编写一个宏,将当前的电子邮件打印为每页两页,而不是在两个单独的页面上。它曾在我的旧电脑Win 10/Outlook 2013上成功运行。我现在有了一台新计算机with 10/Outlook2021,它现在为Dim wdApp作为Word.Application行产生了一个编译错误“用户定义类型未定义”

我只对VBA有一个基本的掌握,所以无法解决这个问题。任何帮助都将不胜感激。

守则如下:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2022-03-10 09:39:07

使用非类型化变量:

代码语言:javascript
复制
Dim appWD as Object
appWD = CreateObject("Word.Application")

或尝试将Word对象库引用添加到项目中。

Visual Basic Editor中,选择Tools,然后选择References并向下滚动列表,直到看到Microsoft Word 12.0 Object Library为止。选中那个框并点击Ok。

票数 1
EN

Stack Overflow用户

发布于 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引用的后期绑定技术:

代码语言:javascript
复制
' 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。有关详细信息,请参阅自动化中使用早期绑定和后期绑定

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71420987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档