Subroutine to toggle between windows doesn't work

B

BHW

Hi,

Any idea why the following macro:

Sub NextWindow()

If Windows.Count > 1 Then ActiveDocument.ActiveWindow.Next.Activate

End Sub

returns the error: run-time error '91' Object variable or With block
variable not set

I'd like to assign a little-used key to this macro and then quickly
toggle between windows.

Thanks
 
H

Helmut Weber

Hi Bruce,

you get that error if the cursor is
in the last window of the windows collection,
as then there is no next window.

If you got only two windows, then:

Sub NextWindow()
If ActiveWindow.Index = Windows.Count Then
ActiveWindow.Previous.Activate
Else
ActiveWindow.Next.Activate
End If
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
B

BHW

Hi Bruce,

you get that error if the cursor is
in the last window of the windows collection,
as then there is no next window.

If you got only two windows, then:

Sub NextWindow()
If ActiveWindow.Index = Windows.Count Then
ActiveWindow.Previous.Activate
Else
ActiveWindow.Next.Activate
End If
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP

Wonderful, many thanks to you!
 
G

Gordon Bentley-Mix

Helmut,

Two things to consider:

1) What to do if there is only 1 window in the Windows collection.
In this instance, wouldn't ActiveWindow.Previous.Activate also throw an
error? Perhaps something like this would fix it.

If Windows.Count > 1 And ActiveWindow.Index = Windows.Count Then

2) What happens if there are 3 (or more) windows and you are in the next
to last one.
In this instance, the first run of the macro takes you to the last window,
as expected. The second run takes you back to the next to last window, as
expected. However, the 3rd run takes you to the last window again. And so on
and so on... Once you get to the next to last window you can never get back
to any of the others. Perhaps something like this would fix it.

Sub NextWindow()
If Windows.Count > 1 And ActiveWindow.Index = Windows.Count Then
ActiveWindow.Previous.Activate
Else
Windows(1).Activate
End If
End Sub

This would cycle you back to the first window again.
--
Cheers!
Gordon
The Kiwi Koder

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
G

Gordon Bentley-Mix

I just realised that the solution for 2) still is not quite right. The
problem is around activating the _previous_ window, not the next window. The
correct code should be:

Sub NextWindow()
'*** If you're in the last window, go to the first window ***
If Windows.Count > 1 And ActiveWindow.Index = Windows.Count Then
Windows(1).Activate
'*** If you're in any window except the last, go to the next window ***
Else
ActiveWindow.Next.Activate
End If
End Sub

And if I wanted to be really clever:

Sub NextWindow()
If Windows.Count > 1 Then
If ActiveWindow.Index = Windows.Count Then
Windows(1).Activate
Else
ActiveWindow.Next.Activate
End If
Else
MsgBox "There is only one window open."
End If
End Sub

That way the user won't think something's broken if there's only 1 window.

--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top