Forum Discussion
How to fix "object/block variable not set" error in VBA?
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.