Results 1 to 13 of 13
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    If Exists for items other than bookmarks! (Word 2003 onwards)

    I know about the very useful "if mydoc.bookmarks.exists("my bkmk") then ..." for working with a bookmark that might or might not exist in the document in question, but is there something similar that can be used for other items? I'm specifically looking here at automatically updating a table of contents if it's there, but I've tripped over this problem before with other objects, so any pearls of wisdom on the subject would be extremely helpful, please!

    Many thanks in advance!
    Beryl M


  2. #2
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: If Exists for items other than bookmarks! (Word 2003 onwards)

    Thanks, Hans - the on error line probably would be best in this case. By the way, is there a line to stop the 'on error resume next' command? Something that would say 'stop resuming next on error from this point onward'?

    I also found the count function useful here, as long as all you need to know is whether there is one or not - eg if mydoc.tablesofcontents.count > 0 then ...

    There doesn't seem to be anything as specific as 'if a bookmark with this name exists then ...' though, unfortunately - or is that because nothing else can be named in the same way? Wouldn't have thought so, but ...?
    Beryl M


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

    Re: If Exists for items other than bookmarks! (Word 2003 onwards)

    If you want to display the built-in error messages again, use

    On Error GoTo 0

    If you have an error handling section with label (for example) ErrHandler, you should use

    On Error GoTo ErrHandler

    instead:

    Sub Test()
    ' Set a custom error handler
    On Error GoTo ErrHandler
    ...
    ...
    ' Temporarily suppress error handling
    On Error Resume Next
    ...
    ...
    ' Set custom error handler again
    On Error GoTo ErrHandler
    ...
    ...

    ExitHandler:
    ' Cleanup here
    ...
    ...
    ' And get out
    Exit Sub

    ErrHandler:
    ' Display a custom message
    MsgBox "Something went terribly wrong!", vbExclamation
    ' Go to cleanup section
    Resume ExitHandler
    End Sub

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

    Re: If Exists for items other than bookmarks! (Word 2003 onwards)

    Exists can also be used for headers and footers. For example, if you want to find out whether a section has a first-page header, you can use

    If ActiveDocument.Sections(1).Headers(wdHeaderFooterF irstPage).Exists Then

    (Note: ActiveDocument.Sections(1).Headers(wdHeaderFooterP rimary).Exists always returns true even if you haven't entered anything in the header).

    For other kinds of items you must either loop through the collection of all such items to determine whether an item exists, or you can suspend error trapping by using On Error Resume Next.

  5. #5
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: If Exists for items other than bookmarks! (Word 2003 onwards)

    I knew there was something else I needed to be able to find and use *only* if it existed - a named customdocumentproperty! A sub I want to update currently uses the 'Manager name' builtin property to put an incrementing number in, but I'd *really* like to put it in a custom property instead, called, say, dCount - but I need to check if it already exists, create it if it doesn't and just increment it if it does.

    I can do all of that except the original check to see if it exists - any thoughts, anyone? The count trick wouldn't work here, and unfortunately nor would the on error one! <img src=/S/sad.gif border=0 alt=sad width=15 height=15>

    Unless one could say ...

    On error resume next
    - create custom property called dCount
    on error goto 0
    - increment custom property dCount
    etc

    Would that work? Notice I worked out the cancel command for the on error line (I hadn't seen Hans' reply, honest!).
    Beryl M


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

    Re: If Exists for items other than bookmarks! (Word 2003 onwards)

    That should work; you could also create the document property in the error handling section.

  7. #7
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: If Exists for items other than bookmarks! (Word 2003 onwards)

    Hans
    Unless you have made a typo, you have just confirmed a suspicion of mine.
    I have been in the habit of including the line "On Error GoTo 0"; in my error handler, and suspected it to be unnecessary if exiting the procedure.
    <UL><LI>Is my suspicion correct?
    <LI>Is the error handling reset by any other actions such as resume?[/list]T.I.A.
    Regards
    Don

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

    Re: If Exists for items other than bookmarks! (Word 2003 onwards)

    Any statement such as

    On Error GoTo 0
    On Error GoTo Label
    On Error Resume Next
    Resume
    Resume Next
    Resume Label

    will clear the error object, i.e. reset error handling. Immediately after one of the above lines, Err = 0.

    In my skeleton example, the error handling section ends with Resume ExitHandler, so the Err object is cleared. But if I hadn't had Resume ExitHandler, Err would still have been cleared when exiting the procedure.

  9. #9
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: If Exists for items other than bookmarks! (Word 2003 onwards)

    Thank you for the clear and complete explanation Hans.
    Regards
    Don

  10. #10
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: If Exists for items other than bookmarks! (Word 2003 onwards)

    If you want to avoid using an "On Error" construct, you might want to adapt my alternative approach in <post:=618,400>post 618,400</post:>.

    It's relevant to searching through any collection of objects. It returns a reference to the object if it exists, or creates it if it doesn't. I imagine you could adapt it to your own needs. It circumvents the need for error handiling, since it relies on the index of the collection item - remains at zero if the item doesn't exist.

    HTH
    Alan

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

    Re: If Exists for items other than bookmarks! (Word 2003 onwards)

    Alan,

    That is a simple and elegant solution for a common problem, your code has just been plagiarised!

    StuartR

  12. #12
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: If Exists for items other than bookmarks! (Word 2003 onwards)

    It's been up there since Dec-06. You're probably not going to be the first! <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    I was actually getting peeved off at the clunky workarounds for the errors thrown when you try to add an item to a collection that's already there, or try to delete an item that doesn't exist. The TV commercials have taught me to say, "There must be a better way!" so that's the one I dreamed up. And PLEASE don't offer me a <img src=/S/free.gif border=0 alt=free width=30 height=15> <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>... I am only too overjoyed to be privliged enough to provide it.

    Alan

  13. #13
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: If Exists for items other than bookmarks! (Word 2003 onwards)

    Thanks, Alan, very useful!
    Beryl M


Posting Permissions

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