Results 1 to 14 of 14
  1. #1
    5 Star Lounger
    Join Date
    Feb 2001
    Location
    Australia
    Posts
    676
    Thanks
    28
    Thanked 2 Times in 2 Posts

    Macro error (Word 2003)

    I recorded a pair of macros to select a whole document, set Zoom = 100%, and set the language (UK English in one, US English in the other), and they ran OK in Normal.dot after I created them, but when I run them in other documents, an error results: "Object variable or With block variable not set". How can I fix this?

    Sub UK()
    Selection.WholeStory
    Selection.LanguageID = wdEnglishUK
    Selection.NoProofing = False
    Application.CheckLanguage = False
    ActiveWindow.ActivePane.View.Zoom.Percentage = 100
    End Sub

    Sub US()
    Selection.WholeStory
    Selection.LanguageID = wdEnglishUS
    Selection.NoProofing = False
    Application.CheckLanguage = False
    ActiveWindow.ActivePane.View.Zoom.Percentage = 100
    End Sub

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro error (Word 2003)

    The macros should run OK as long as you have a document open. The error message will occur if you run the macro while no document is open. You can get around this by inserting an error handler:

    Sub UK()
    On Error GoTo ExitHere
    Selection.WholeStory
    Selection.LanguageID = wdEnglishUK
    Selection.NoProofing = False
    Application.CheckLanguage = False
    ActiveWindow.ActivePane.View.Zoom.Percentage = 100
    ExitHere:
    End Sub

  3. #3
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Macro error (Word 2003)

    I can't see any reason why that code would give you errors. If you single step the code then which line results in the error?

    You could make this more efficient by replacing
    <code>
    Selection.WholeStory
    Selection.LanguageID = wdEnglishUK
    Selection.NoProofing = False
    </code>
    with
    <code>
    With ActiveDocument.Content
    .LanguageID = wdEnglishUK
    .NoProofing = False
    End With
    </code>
    This will apply the settings to the text without selecting it.

    StuartR

  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Macro error (Word 2003)

    Hans,

    I I run that code with no document open then it doesn't give me the error Object variable or With block variable not set. I get the message This command is not available because no document is open.

    StuartR

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro error (Word 2003)

    I do get the error "Object variable or With block variable not set" in Word 2002 if no document is open. The error message below is in Dutch, but you'll recognize the keywords, I hope.
    Attached Images Attached Images
    • File Type: png x.png (7.7 KB, 0 views)

  6. #6
    5 Star Lounger
    Join Date
    Feb 2001
    Location
    Australia
    Posts
    676
    Thanks
    28
    Thanked 2 Times in 2 Posts

    Re: Macro error (Word 2003)

    The error occurs even when a document is open. I tried removing the line "ActiveWindow.ActivePane.View.Zoom.Percentage = 100", and the error stopped. Is there another way to set the Zoom = 100% in the macro?

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro error (Word 2003)

    Not as far as I know. I can't reproduce the error with an open document, so I have no idea why you get it. Does it happen in a specific situation?

  8. #8
    5 Star Lounger
    Join Date
    Feb 2001
    Location
    Australia
    Posts
    676
    Thanks
    28
    Thanked 2 Times in 2 Posts

    Re: Macro error (Word 2003)

    No, it happens in any document, every time.

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro error (Word 2003)

    That is definitely not as it should be. Perhaps an add-in is interfering. Take a look at Phil Rabichow's <!post=Systematic Approach to Behavioral Problems in Word,197827>Systematic Approach to Behavioral Problems in Word<!/post>.

  10. #10
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Macro error (Word 2003)

    I cannot reproduce the error either.

    Is the Macro in your Normal.dot?
    How are you starting the Macro?

    StuartR

  11. #11
    5 Star Lounger
    Join Date
    Feb 2001
    Location
    Australia
    Posts
    676
    Thanks
    28
    Thanked 2 Times in 2 Posts

    Re: Macro error (Word 2003)

    Yes, the macro is in Normal.dot. I normally run macros from keyboard shortcuts, but this error occurs regardless of how I run the macro - even from within the macro editor. However, I have discovered that the error does not occur when the code is in Modules > NewMacros, but it does occur when the code is in MS Word Objects > ThisDocument.
    Attached Files Attached Files

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro error (Word 2003)

    The ThisDocument module is intended for event procedures that run automatically when a document is opened or closed etc.
    Ordinary macros should *not* be stored in ThisDocument but in a standard module under the Modules section (such as NewMacros).

  13. #13
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Macro error (Word 2003)

    That makes sense. ThisDocument is not intended for generic Macros that manipulate other documents.

    ActiveWindow is a property of a document, and I suspect that Word was telling you that Normal.Dot didn't have any active windows as it was only open as a template.

    StuartR

  14. #14
    5 Star Lounger
    Join Date
    Feb 2001
    Location
    Australia
    Posts
    676
    Thanks
    28
    Thanked 2 Times in 2 Posts

    Re: Macro error (Word 2003)

    OK thanks.

Posting Permissions

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