+ Reply to Thread
Results 1 to 14 of 14

Bookmarks based on Format: Loop Once through whole Doc

  1. #1
    Registered User
    Join Date
    01-10-2023
    Location
    BRD/UK
    MS-Off Ver
    2003 v.DE/EN
    Posts
    52

    Bookmarks based on Format: Loop Once through whole Doc

    Hi,
    I'm trying to auto create Bookmarks based on a specific Font Format (with that line once selected to become the Bookmark name) found throughout the document. Single instance find and BM creation works, but I'm having trouble to make it loop ONCE through the Doc to catch all lines that have that specific format. Any help is much appreciated.

    The single instance find creates named BMs and is cumaltive but tedious:
    Please Login or Register  to view this content.
    Thus the whole doc loop. Problem: either only first line caught, or infinity loop even though the End-of-Doc declare, regardless of how I rewrite it:
    v1
    Please Login or Register  to view this content.
    v2
    Please Login or Register  to view this content.
    Last edited by politono; 06-30-2025 at 11:49 AM.

  2. #2
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,864

    Re: Bookmarks based on Format: Loop Once through whole Doc

    For example:
    Please Login or Register  to view this content.
    Do note that the inserted bookmarks are hidden and numbered. That's because:
    • bookmarks cannot begin with numbers; and
    • bookmark names must be unique.
    Making the bookmarks hidden prefixes them with an underscore, which avoids the first problem, whilst numbering avoids the second problem. Changing the prefix character to something else will make the bookmarks visible.

    As coded, the macro will replace any of the existing bookmarks it has previously inserted on each run.
    Last edited by macropod; 07-05-2025 at 07:08 PM.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Registered User
    Join Date
    01-10-2023
    Location
    BRD/UK
    MS-Off Ver
    2003 v.DE/EN
    Posts
    52

    Re: Bookmarks based on Format: Loop Once through whole Doc

    Hi! Im getting a RUN-TIME ERROR 5827: bad bookmark name, on ".Bookmarks.Add "_" & Left(StrBkMk, 36) & Format(i, "000")".

    What is the purpose of having the the Do loop repeat itself inside itself?

  4. #4
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,864

    Re: Bookmarks based on Format: Loop Once through whole Doc

    Code corrected.

    The nested loop is so that each line within in a multi-line range will get its own bookmark. It achieves what you're doing with
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    but without the need to select anything. I suppose I could have used an IF test instead of the outer loop, but the result would be the same.
    Last edited by macropod; 07-02-2025 at 06:16 PM.

  5. #5
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 V 2507
    Posts
    13,809

    Re: Bookmarks based on Format: Loop Once through whole Doc

    @ politono.

    Please update your profile to specify your location. Something general like USA or UK etc is sufficient. It aids us volunteers in having an idea what your regional settings are.

    I am sure you understand and thank you in advance.
    Dave

  6. #6
    Registered User
    Join Date
    01-10-2023
    Location
    BRD/UK
    MS-Off Ver
    2003 v.DE/EN
    Posts
    52

    Re: Bookmarks based on Format: Loop Once through whole Doc

    Hm still getting the same error but only on the second i.e. outer loop, and bookmarks are being populated but utterly senseless:
    test_BM.png
    It counted 24 bookmarks created instead of the supposed 3 in the test case, with alpha-numeric names rather than the bolded text's name

  7. #7
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,864

    Re: Bookmarks based on Format: Loop Once through whole Doc

    I've replaced the Const StrNoChr As String line to eliminate paragraph breaks, etc.

    Regardless, I cannot see how you're getting all those bookmarks. Assuming your 'Title #' strings are all that's bolded, the code as posted only returns one bookmark for the whole of each string.

  8. #8
    Registered User
    Join Date
    01-10-2023
    Location
    BRD/UK
    MS-Off Ver
    2003 v.DE/EN
    Posts
    52

    Re: Bookmarks based on Format: Loop Once through whole Doc

    I just noticed why 24 bookmarks when checking the location option:
    it appears it bookmarks every bold character including spaces (between bold characters that are within the bold format range) individually rather than the whole string (line as one).
    And bookmark names are: "_" "bold letter/space" than "the number"

    test_BM_L.png

  9. #9
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,864

    Re: Bookmarks based on Format: Loop Once through whole Doc

    That's not how the macro works. It works at the whole-of-string level - including bolded spaces. The only reason it would bookmark individual characters is if you have non-bold spaces between the letters/numbers.

  10. #10
    Registered User
    Join Date
    01-10-2023
    Location
    BRD/UK
    MS-Off Ver
    2003 v.DE/EN
    Posts
    52

    Re: Bookmarks based on Format: Loop Once through whole Doc

    Agreed, though weirdly it does and I can't figure out why. I have only written ordinarily than bolded the titles as a whole, again ordinarily.
    Alas, thank you for your patient help.

  11. #11
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 V 2507
    Posts
    13,809

    Re: Bookmarks based on Format: Loop Once through whole Doc

    politono Please update your profile as requested back in post #5.

  12. #12
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,864

    Re: Bookmarks based on Format: Loop Once through whole Doc

    Unless you're working with a corrupt document, the code as posted does not bookmark each character. The only other way you could get that result is because you've changed the code (e.g. using an ordinary search for ^? or a wildcard search for *).

    See attached.
    Attached Images Attached Images

  13. #13
    Registered User
    Join Date
    01-10-2023
    Location
    BRD/UK
    MS-Off Ver
    2003 v.DE/EN
    Posts
    52

    Re: Bookmarks based on Format: Loop Once through whole Doc

    Ayee, that it was: the "^?". Silly me. Thank you for pointing out the overobvious yet not tunnel-view visible problem.

  14. #14
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,864

    Re: Bookmarks based on Format: Loop Once through whole Doc

    Now all you need to do is update your profile...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Loop through Worksheets and format data as Table based on keyword
    By Brawnystaff in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-26-2020, 12:14 AM
  2. how to convert list-based format to matrix-based format
    By Delangstevandestraat in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-17-2019, 02:46 AM
  3. Calling A Function With Variables based A Loop Within A Loop
    By Mark123456789 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-24-2016, 03:03 PM
  4. Replies: 0
    Last Post: 11-06-2013, 09:00 AM
  5. Replies: 3
    Last Post: 10-30-2013, 07:29 AM
  6. Substitute bookmarks/subject from Outlook message from Excel rows/cells then loop
    By nylevolm in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-23-2013, 11:02 AM
  7. Advancing outer Loop Based on criteria of inner loop
    By ExcelMonkey in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-15-2005, 01:05 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1