0

I am running a simple macro to paste contents from Excel range into a Word doc. It worked perfectly before but now I am getting

Runtime Error 91
Object variable or with block variable not set

The code is below.

Any ideas how to get this to run?

Sub CopyALLtoWord()
    Dim objWord, objDoc As Object
    ActiveWindow.View = xlNormalView

    Dim i As Integer

    For i = 1 To ActiveWorkbook.Worksheets("worksheet1").Cells(3, 3)
        Worksheets("worksheet1").Range("n5").Value = i

        Range("N5").Select
        Calculate

        Application.Wait (Now + TimeValue("0:00:05"))

        ThisWorkbook.Worksheets("worksheet1").Range("L12:AK85").Select
        Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture

        'Select Word & paste
        Set objWord = GetObject(, "Word.Application")
        objWord.Selection.Paste
    
        objWord.Selection.TypeParagraph

        Application.CutCopyMode = False
    Next i
End Sub

I tried to debug and it highlighted the line objWord.Selection.Paste.

6
  • 1
    There's no active selection in Word? Is there a document open? Commented Nov 20, 2023 at 21:05
  • The code is supposed to paste range from "Worksheet1" into an empty Word Doc. I have Word open in another window. It has worked before and now has suddenly stopped working. Commented Nov 20, 2023 at 21:12
  • Word is open, but is there a document open in Word? Commented Nov 20, 2023 at 21:24
  • Yes there is a blank document open in word Commented Nov 20, 2023 at 21:31
  • 2
    Check Task Explorer to make sure you only have one instance of Word running. You may have other (possibly hidden instances) open, and GetObject may have attached to one of those. Maybe add objWord.Visible = True right after the GetObject line, to make sure you're not using a hidden instance. Commented Nov 20, 2023 at 21:59

1 Answer 1

-1

Try your code with a newly opened blank document. In this case it will be the active document. I tested it with this snippet, and works.

Sub objword()
Dim obj As Word.Application

Set obj = GetObject(, "word.application")
Range("A1:A10").Copy
obj.Selection.Paste
Debug.Print "345"
End Sub

SIDENOTE: At the end the Debug.Print is for testing purposes, because when i step through the code with F8, after executing the Paste it finishes the program like hit F5. (I don't know why)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.