excel 从Word文档填充电子邮件正文时出错

pkln4tw6  于 2023-01-18  发布在  其他
关注(0)|答案(1)|浏览(372)

我正在使用excel宏发送一系列电子邮件,每封邮件都有一个独特的附件,还有三个模板电子邮件中的一个,保存为word文档。除了从word文档中提取电子邮件的正文外,一切都运行良好。问题似乎出在WordEditor上。我收到以下错误

Err.Description:The operation failed.
Err.Number:-2147467259
Err.Source:Microsoft Outlook

下面是我尝试过的代码:

Sub SendDCLEmails()

    Dim OutlookApp As Object
    Dim OutlookMail As Object
    Dim WordApp As Object
    Dim WordDoc As Object
    Dim DCLFile As String 'Attachment that differs for each email
    Dim DCLCount As Integer 'Number of emails that will be sent
    Dim toList As String
    Dim ccList As String
    Dim CoverLetter As String 'Word document template email
    Dim fileCheckDCL As String
    Dim fileCheckCover As String
    Dim editor As Object
    
    
'Set references to Outlook
    On Error Resume Next
    Set OutlookApp = GetObject(, "Outlook.Application")
    If Err <> 0 Then Set OutlookApp = New Outlook.Application
    On Error GoTo 0
        
'Set references to Word
    On Error Resume Next
    Set WordApp = GetObject(, "Word.Application")
    If Err <> 0 Then Set WordApp = New Word.Application
    On Error GoTo 0
            
    Sheets("Contacts").Select
    
'Create email for each record on "Contacts" tab
    DCLCount = ActiveSheet.UsedRange.Rows.Count - 1

    For i = 1 To DCLCount
    

        DCLFile = Range("AD1").Offset(i, 0).Value & "\" & Range("AE1").Offset(i, 0).Value
        CoverLetter = Range("AF1").Offset(i, 0).Value
        fileCheckDCL = Dir(DCLFile)
        fileCheckCover = Dir(CoverLetter)
        
            
            'Run some validations and generate the toList and ccList variables.
                                 
            Set WordDoc = WordApp.Documents.Open(CoverLetter)
            WordDoc.Content.Copy
                        
        'Create Emails
            Set OutlookMail = OutlookApp.CreateItem(0)
                    
            With OutlookMail
                .Display
                .To = toList
                .CC = ccList
                .Subject = Range("AG1").Offset(i, 0).Value
                Set editor = .GetInspector.WordEditor 'This is where the error occurs.
                editor.Content.Paste
                .Attachments.Add DCLFile
                .Send
            End With
                               
            WordDoc.Close savechanges:=False
        End If
           
        toList = vbNullString
        ccList = vbNullString
        CoverLetter = vbNullString
        DCLFile = vbNullString
        fileCheckDCL = vbNullString
        fileCheckCover = vbNullString
        Set editor = Nothing
        
    Next i
    
    OutlookApp.Quit
    WordApp.Quit

    End Sub
lb3vh1jj

lb3vh1jj1#

在VBA宏中无需使用后期绑定和早期绑定技术:

Set OutlookApp = GetObject(, "Outlook.Application")
    If Err <> 0 Then Set OutlookApp = New Outlook.Application

相反,你需要使用一个或另一个。阅读更多关于Using early binding and late binding in Automation的文章。我建议用真实的的类(早期绑定)声明所有的对象,这样可以进一步避免语法错误。并且在代码中使用New操作符而不是CreateObject操作符。

Set editor = .GetInspector.WordEditor 'This is where the error occurs.

如果Inspector不可见且尚未初始化,则调用WordEditor属性有时可能会失败。请尝试在获取Word编辑器值之前调用Display方法。
此外,您可以在Outlook中创建模板,并使用Application.CreateItemFromTemplate方法从Outlook模板(.oft)创建一个新的Microsoft Outlook项目,并返回新项目,而不是依赖Word文档作为模板。在我为技术博客撰写的文章中了解更多信息,请参阅How To: Create a new Outlook message based on a template

相关问题