Results 1 to 7 of 7
  1. #1
    3 Star Lounger rcbjr2's Avatar
    Join Date
    Jan 2001
    Location
    Matthews, NC
    Posts
    279
    Thanks
    6
    Thanked 1 Time in 1 Post

    Run Time Error 91

    I just recorded a macro of Ctrl+F6, which is the Next Window command (I believe). It recorded ActiveWindow.Next.Activate. But, when I try to run it, I get error msg "Object variable or With block variable not set." What does this mean? Why would I get an error on something I recorded? Shouldn't the recorder record it so it will work??

    Thx.

    -Rich

  2. #2
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Run Time Error 91

    You get the message because you do not have a second window open when you run the macro. When you recorded the macro, you had another window open that you could switch to. Recording a macro does not build in error handling.

  3. #3
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Run Time Error 91

    Hi Rich,

    This looks like an incomplete implementation of the Word object model in VBA by MS.

    When you run the Next Window command, more is going on behind the Word scenes than just ActiveWindow.Next.Activate - for instance, what if there is only one window, or you are at the "last" window - there has to be something that checks for these conditions - if there's only one window, do nothing; if there are multiple windows but the last (highest index number in the collection) one is active, then go to the first one.

    If the macro recorder were doing what it should, it would expose a few lines of code like this, but it doesn't.

    Here's a quick attempt at a substitute - you could try using this code in place of the macro you recorded. Note that this was done in Word 2000 so I don't know if it will work in Word 97.

    Gary

    <pre>Sub NextWindowTweaked()
    'Equivalent to Ctrl+F6 (NextWindow) command
    'Gary Frieder April 2001
    Dim lngWins As Long
    lngWins = Windows.Count
    If lngWins = 1 Then
    'do nothing - no other window to switch to
    Else
    If ActiveWindow.Index = lngWins Then
    'at "last" window - go back
    Windows(1).Activate
    Else
    ActiveWindow.Next.Activate
    End If
    End If
    End Sub
    </pre>


  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Run Time Error 91

    Phil,

    This is a logical assumption, but in fact you can record this macro with only one window open, and you won't get the error message until you try to run the recorded macro.

    Just do a Ctr+F6 with only one window open, to see this in action (and see my other post :-)

    Gary

  5. #5
    3 Star Lounger rcbjr2's Avatar
    Join Date
    Jan 2001
    Location
    Matthews, NC
    Posts
    279
    Thanks
    6
    Thanked 1 Time in 1 Post

    Re: Run Time Error 91

    Thanks for the reply. However, I did have more than one window open when I ran the macro, but as Gary noted, and I figured out before I read his reply, the .Next scrolls through the open windows but then stops at the last. This is contrary to the behavior of Ctrl+F6, which continues to scroll through all open windows.

    Thx.

    -Rich Belthoff

  6. #6
    3 Star Lounger rcbjr2's Avatar
    Join Date
    Jan 2001
    Location
    Matthews, NC
    Posts
    279
    Thanks
    6
    Thanked 1 Time in 1 Post

    Re: Run Time Error 91

    Thanks for the code, Gary. As I played with it with several windows opened, I realized it was stopping at the last window, and then I saw your reply.

    By the way, how do you know to create the variable as Long in this instance?

    Thx.

    -Rich

  7. #7
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Run Time Error 91

    Hi Gary:

    If I have a second window open, I can run the macro without errors, although I only tried it with two windows. I only get the error if I run the macro with only 1 window open. Thanks for the information.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •