Error populating email body from word documents

0 votes

I'm developing an excel macro that will enable me to send a set of emails, each with a different attachment, along with one of three pre-written emails that are saved as word documents. The only thing that isn't working properly is importing the email content from the word document. WordEditor appears to be the issue. The next problem message appears.

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

Here is the code I have tried:

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
Jan 15, 2023 in Others by Kithuzzz
• 38,000 points
1,534 views

1 answer to this question.

0 votes

There is no need to use late and early-binding technologies in the VBA macros:

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

Instead, choose either one or the other. The article Using early binding and late binding in automation has further information on that. I would advise declaring all objects with genuine classes (early binding), since this may help further prevent syntax errors. Additionally, substitute the New operator for the CreateObject one in the code.

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

Calling the WordEditor property may sometimes fail if the Inspector is not yet visible and initialized. Try to call the Display method prior getting the Word editor value.

answered Jan 15, 2023 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
1 answer

Runtime error 438 while importing data in excel from secured website using VBA

Replace With ieDoc.forms(0) .userType.Value = "1" ...READ MORE

answered Sep 23, 2022 in Others by narikkadan
• 63,600 points
889 views
0 votes
1 answer
0 votes
1 answer

Multiple find and replace in MS Word from a list in MS Excel

If I understand you correctly, you want ...READ MORE

answered Oct 28, 2022 in Others by narikkadan
• 63,600 points
715 views
0 votes
1 answer

Embed picture in outlook mail body excel vba

The image needs to be added and ...READ MORE

answered Dec 16, 2022 in Others by narikkadan
• 63,600 points
5,435 views
0 votes
1 answer

VBA How to extract the date and time of arrival of a answered email

Use MailItem.ReceivedTime property. I hope this helps you ...READ MORE

answered Jan 9, 2023 in Others by narikkadan
• 63,600 points
2,886 views
0 votes
1 answer

Export All appointments and meetings (including recurring meetings) Excel VBA

However, when I use the code above ...READ MORE

answered Feb 13, 2023 in Others by narikkadan
• 63,600 points
1,688 views
0 votes
1 answer

Excel VBA creating a new Outlook appointment results in a cancelled appointment

Because an inappropriate sender will be used, ...READ MORE

answered Feb 14, 2023 in Others by Kithuzzz
• 38,000 points
854 views
0 votes
1 answer

VBA: My Email .body doesn't concatenate with itself: application-defined or object-defined error

Try this: 'Somewehere declare this string variable Dim incomingHTMLBody ...READ MORE

answered Feb 16, 2023 in Others by narikkadan
• 63,600 points
878 views
0 votes
2 answers

Attempted import error: 'Switch' is not exported from 'react-router-dom'

import it from material UI there will ...READ MORE

answered Sep 1, 2022 in Others by anonymous
5,575 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP