vba Office 2016 中的 MailItem.GetInspector.WordEditor 生成应用程序定义或对象定义错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45376329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:59:56  来源:igfitidea点击:

MailItem.GetInspector.WordEditor in Office 2016 generates Application-defined or object defined error

excelvbaoffice365outlook-vbaoffice-2016

提问by Falthazar

I wrote an Excel macro to send email from a spreadsheet. It works on Office 2013, but not Office 2016.

我写了一个 Excel 宏来从电子表格发送电子邮件。它适用于 Office 2013,但不适用于 Office 2016。

I looked at the VBA differences between Office 2013 and 2016, but couldn't see anything about changes to the inspector or word editor for message objects.

我查看了 Office 2013 和 2016 之间的 VBA 差异,但看不到任何有关消息对象的检查器或文字编辑器更改的信息。

Once it gets to .GetInspector.WordEditorit throws:

一旦到达.GetInspector.WordEditor它就会抛出:

Run-time error '287':
Application-defined or object defined error

运行时错误“287”:
应用程序定义或对象定义错误

Here is the relevant part of the macro:

这是宏的相关部分:

Sub SendEmail()
    Dim actSheet As Worksheet
    Set actSheet = ActiveSheet

    'directories of attachment and email template
    Dim dirEmail as String, dirAttach As String

    ' Directory of email template as word document
    dirEmail = _
        "Path_To_Word_Doc_Email_Body"

    ' Directories of attachments
    dirAttach = _
        "Path_To_Attachment"

    ' Email Subject line
    Dim subjEmail As String
    subjEmail = "Email Subject"

    Dim wordApp As Word.Application
    Dim docEmail As Word.Document

    ' Opens email template and copies it
    Set wordApp = New Word.Application
    Set docEmail = wordApp.Documents.Open(dirEmail, ReadOnly:=True)
    docEmail.Content.Copy

    Dim OutApp As Outlook.Application
    Set OutApp = New Outlook.Application
    Dim OutMail As MailItem
    Dim outEdit As Word.Document

    ' The names/emails to send to
    Dim docName As String, sendEmail As String, ccEmail As String, siteName As String
    Dim corName As String

    Dim row As Integer
    For row = 2 To 20

        sendName = actSheet.Cells(row, 1)
        sendEmail = actSheet.Cells(row, 2)
        ccEmail = actSheet.Cells(row, 3)
        siteName = actSheet.Cells(row, 4)

        Set OutMail = OutApp.CreateItem(olMailItem)
        With OutMail
            .SendUsingAccount = OutApp.Session.Accounts.Item(1)
            .To = sendEmail
            .CC = ccEmail
            .Subject = subjEmail & " (Site: " & siteName & ")"

            Set outEdit = .GetInspector.WordEditor
            outEdit.Content.Paste

            outEdit.Range(0).InsertBefore ("Dear " & sendName & "," & vbNewLine)

            .Attachments.Add dirAttach

            .Display
            '.Send

        End With
        Debug.Print row

        Set OutMail = Nothing
        Set outEdit = Nothing
    Next row

    docEmail.Close False
    wordApp.Quit
End Sub

Things I've tried based on suggestions:

我根据建议尝试过的事情:

  • Checked Outlook settings - default is HTML text
  • Moved .displayover .GetInspector.WordEditor
  • 检查 Outlook 设置 - 默认为 HTML 文本
  • .display过来.GetInspector.WordEditor

回答by David Zemens

Ensure Word is the default email editor. From the Inspector.WordEditor dox:

确保 Word 是默认的电子邮件编辑器。来自Inspector.WordEditor dox

The WordEditorproperty is only valid if the IsWordMailmethod returns True and the EditorTypeproperty is olEditorWord. The returned WordDocumentobject provides access to most of the Word object model...

WordEditor物业是唯一有效的,如果该IsWordMail方法返回true和EditorType属性olEditorWord。返回的WordDocument对象提供对大部分 Word 对象模型的访问...

Further, ensure that Outlook is configured to send Rich Text or HTML emails, not plain text.

此外,确保 Outlook 配置为发送富文本或 HTML 电子邮件,而不是纯文本。

回答by Marcus

I am not entirely sure if I had the same issue as you, but the call to GetInspectorstarted failing for me after upgrading Office 2016. So to be clear it worked with Office 2016 and then stopped working after the latest update.

我不完全确定我是否遇到了与您相同的问题,但是GetInspector在升级 Office 2016 后调用对我来说开始失败。所以要清楚它适用于 Office 2016,然后在最新更新后停止工作。

The following workaround worked for me

以下解决方法对我有用

dim item : set item = Addin.Outlook.CreateItemFromTemplate(Filename)
Outlook.Inspectors.Add(item) ' Outlook is the application object

it only appears to work if I add the item straight after creating it, setting properties on it and then adding it did not work.

如果我在创建项目后直接添加它,在其上设置属性然后添加它不起作用,它似乎才起作用。

Note: I have not tested with CreateIteminstead of CreateItemFromTemplate. The second line was added and unnecessary prior to the Office update.

注意:我没有用CreateItem而不是CreateItemFromTemplate. 第二行是在 Office 更新之前添加的并且是不必要的。

回答by Jay Pathmanathan

Try moving the editor to the first action... ...

尝试将编辑器移至第一个动作......

     With OutMail

        Set outEdit = .GetInspector.WordEditor
        outEdit.Content.Paste

        .SendUsingAccount = OutApp.Session.Accounts.Item(1)
        .To = sendEmail
        .CC = ccEmail
        .Subject = subjEmail & " (Site: " & siteName & ")"

...

...