Forum Discussion
How to fix "object/block variable not set" error in VBA?
I'm using VBA in Word 2013. My OS is Win10.
I'm having a problem with macros that I created that will no longer work. When I click to run them, I get: "Run-time error '91': Object variable or With block variable not set."
I have no idea what the problem could be. These macros ran fine before. Then, this started happening.
For example, here's a very simply-written one to zoom out in order to be able to see 5 pages at once in Word:
Sub Five_Page_View()
'
' Zoom 45% to see 5 pages
'
'
ActiveWindow.ActivePane.View.Zoom.Percentage = 45
End Sub
Used to work. Now, doesn't. I get the error above. Why?
Does anyone have any idea what the problem might be?
Thanks!
1 Reply
- LucarahellerBrass Contributor
Here’s what’s happening and how I fix it on my side.
Why the error appears
Run-time error '91' means one of the objects in the chain is Nothing. In Word, ActiveWindow.ActivePane can be Nothing depending on the current view (e.g., Read Mode, Protected View, or certain split/outline states). Your code assumes an active pane, so it breaks when there isn’t one.Quick fixes
- Make sure a document is open and you’re in Print Layout (not Read Mode).
- Avoid ActivePane altogether; target the window’s view instead.
- Add a small guard so the macro doesn’t run with no documents.
Robust version (5 pages at ~45%):
Sub Five_Page_View()
' Ensure a document is open
If Documents.Count = 0 Then Exit Sub
With ActiveWindow.View
' Ensure Print Layout (needed for multi-page)
If .Type <> wdPrintView Then .Type = wdPrintView
' Set Many Pages: 1 row x 5 columns (adjust as you prefer)
.Zoom.PageRows = 1
.Zoom.PageColumns = 5
' Optional: lock in a specific zoom percentage
.Zoom.Percentage = 45
End With
End Sub
If you just want a fixed zoom (no multi-page grid):
Sub Zoom45()
If Documents.Count = 0 Then Exit Sub
With ActiveWindow.View
If .Type <> wdPrintView Then .Type = wdPrintView
.Zoom.Percentage = 45
End With
End Sub
If you still want to use a pane safely:
Sub Five_Page_View_UsingPane()
If Documents.Count = 0 Then Exit Sub
If ActiveWindow Is Nothing Then Exit Sub
If ActiveWindow.Panes.Count = 0 Then Exit Sub
With ActiveWindow.ActivePane.View
If .Type <> wdPrintView Then .Type = wdPrintView
.Zoom.PageRows = 1
.Zoom.PageColumns = 5
.Zoom.Percentage = 45
End With
End Sub
This removes the Nothing reference and makes the macro resilient to view changes.