Share via

VBA Code not working - Word Editor

Anonymous
2014-04-29T09:01:28+00:00

Goo morning, All.

I have routine below which I wrote with the help of some of you guys :).

It was working perfectly yesterday and today it has decided not to work.

The routine stops at:

--> wdDoc.Range.InsertBefore vbNewLine with the error message: Run-Time Error '91': Object variable or With block variable not set.

VBA Code

Sub sendcomplexemailattachment()

     Dim olApp As Object 'Outlook.Application

     Dim olEmail As Object 'Outlook.MailItem

     Dim olInsp As Object 'Outlook.Inspector

     Dim olAttachments As Object 'Outlook.Attachments

     Dim wdDoc As Object 'Word.Document

     Dim wdRng As Object 'Word.Range

     On Error Resume Next

     Set olApp = GetObject(, "outlook.application")

     If Err <> 0 Then Set olApp = CreateObject("outlook.application")

     On Error GoTo 0

     Set olEmail = olApp.CreateItem(0)

     Set olAttachments = olEmail.Attachments

     With olEmail

         .BodyFormat = 3

         Set olInsp = .GetInspector

         Set wdDoc = olInsp.WordEditor

         .To = "WE CFR REPORT"

         .Subject = "WE CFR DAILY REPORT" & " - " & Format(Date, " dd.mm.yy")

         wdDoc.Range.InsertBefore vbNewLine

         Sheets("SAP BO Cuts").Range("A1").CurrentRegion.Copy

         Set wdRng = wdDoc.Range

         wdRng.Collapse Direction:=1 'wdCollapseStart

         wdRng.MoveStart Unit:=1, Count:=1 'Unit:=wdCharacter

         wdRng.Paste

         wdDoc.Range.InsertBefore vbNewLine & vbNewLine & "Past days cuts:"

         olAttachments.Add "L:\MMO\Scorecard\1. CFR Results\Total Hair WE CFR Report.xls", _

            1, 1, "Total Hair WE CFR Report"

         wdDoc.Range.InsertBefore vbNewLine & vbNewLine & "CFR MDO/Plants:" & vbNewLine

         Sheets("Summary").Range("A1:F7").Copy

         Set wdRng = wdDoc.Range

         wdRng.Collapse Direction:=1 'wdCollapseStart

         wdRng.PasteSpecial DataType:=4 'wdPasteBitmap

         wdDoc.Range.InsertBefore "CFR MTD:" & vbNewLine & vbNewLine

         wdDoc.Range.InsertBefore "Good Morning, All." & vbNewLine & vbNewLine & "Please see below latest CFR results and cuts." & vbNewLine & vbNewLine

         .Display

     End With

 End Sub

Any ideas what could be causing this issue, please?

I tried to change the reference list priority but no lick.

Thank you very much for your help in advance.

Best regards,

Vanessa Simmonds

<Mod. Moved to Office Programming>

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Anonymous
    2014-04-29T11:49:30+00:00

    I trust this is an Excel macro and not a Word macro as indicated in the header?

    You must display the message before you can work with the document body in this manner e.g.

        With olEmail

            .BodyFormat = 3

            .to = "WE CFR REPORT"

            .Subject = "WE CFR DAILY REPORT" & " - " & Format(Date, " dd.mm.yy")

          .display

    Set olInsp = .GetInspector

    Set wdDoc = olInsp.WordEditor

            wdDoc.Range.InsertBefore vbCr

    Was this answer helpful?

    6 people found this answer helpful.
    0 comments No comments