Consulting

Results 1 to 14 of 14

Thread: Solved: Trouble looping through bookmarks

  1. #1
    VBAX Regular TButhe's Avatar
    Joined
    Sep 2004
    Location
    Sioux Falls, SD
    Posts
    64
    Location

    Solved: Trouble looping through bookmarks

    I am trying to get a KB article ready on spellchecking form fields. I want to do a spellcheck on exit of each field and that part is done. It was suggested that I have an example that runs only when the user tabs off the last field of the document. (This of course assumes that they must enter something in that field for the macro to run.) For the life of me cannot figure out how to loop through the bookmarks. I have attached my "attempt". It only looks at the last field. I am sure it is obvious but I just don't see what I am doing wrong.

    Just remember I can't hear you laughing over the internet - so you'll have to use lots of smilies when you look at my code.

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi,

    Use this sub instead for the exit macro of the last formfield: [VBA]
    Sub myCheckSpelling()
    Dim iCnt As Integer
    With Application.ActiveDocument
    If .ProtectionType <> wdNoProtection Then _
    .Unprotect Password:=""

    For iCnt = 1 To .FormFields.Count
    .FormFields(iCnt).Select
    #If VBA6 Then
    Selection.NoProofing = False
    #End If
    Selection.LanguageID = wdEnglishUK
    Selection.Range.CheckSpelling
    Next
    .Protect Type:=wdAllowOnlyFormFields, Noreset:=True, Password:=""

    MsgBox "Spell check completed", vbInformation
    End With
    End Sub
    [/VBA]

    Enjoy!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  3. #3
    VBAX Regular TButhe's Avatar
    Joined
    Sep 2004
    Location
    Sioux Falls, SD
    Posts
    64
    Location
    That is SWEET!! Thank you! You really are the Master. I will be sure to give you credit in the KB article.

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    [vba]Dim BM As Bookmark
    For Each BM In ActiveDocument.Bookmarks()
    ...do stuff to the bookmark
    Next[/vba]

    HOWEVER.....

    1. If the document is using formfields, then you have to unprotect it to spellcheck. I assume you have that taken care of elsewhere.

    2. your code [vba]ActiveDocument.Bookmarks("\Para").Select[/vba] will select the paragraph the Selection is in. It does not select the bookmark. "\para" is ONLY the paragraph the selection is in, and your code does not select the bookmark itself. The reason it is only doing the last formfield, is that you are (as stated) running the code FROM the last formfield - which has the Selection.

    3. I would also point out that it is very possible to have bookmarks in a document that are NOT formfields. While all formfields are bookmarks, not all bookmarks are formfields.

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Ah Joost, not only did you post the document protection issue, but also the formfield vs bookmark issue. You da man.

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by TButhe
    That is SWEET!! Thank you! You really are the Master. I will be sure to give you credit in the KB article.
    Your most welcome!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by fumei
    Ah Joost, not only did you post the document protection issue, but also the formfield vs bookmark issue. You da man.
    Na you silly you are!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  8. #8
    VBAX Regular TButhe's Avatar
    Joined
    Sep 2004
    Location
    Sioux Falls, SD
    Posts
    64
    Location
    Thanks again and points definately taken Gerry. I just want to say that I come out here almost every day and get some really good information just still don't have that "programmer mentality" but I hear that it is good to learn new things later in life. It will take me years to learn but this site sure makes it easier. Thanks again everyone.

  9. #9
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Programming is a school of falling down and getting up real quickly. Just make sure you keep getting up (Even if your back is killing yah)

    What your doing is correct. Read a lot, try a lot, learn a lot ...Your there!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  10. #10
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Just one thing. Except if there are spelling errors:

    This works fine if you accept any of the suggestions.
    This works fine if you edit SOME of the text in the formfield.

    This fails if you edit ALL of the text in the formfield.

  11. #11
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Do you mean in the dialog edit box?
    Meaning all including the bookmarks so that you are indeed overwriting the entire formfield?
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  12. #12
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by fumei
    Just one thing. Except if there are spelling errors:

    This works fine if you accept any of the suggestions.
    This works fine if you edit SOME of the text in the formfield.

    This fails if you edit ALL of the text in the formfield.
    Gerry,

    Could you please tell me how to reproduce the error? (Is it what I discribed above..which also is a problem)

    I want to change the code to fix that problem so the Beauty has a great KB!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  13. #13
    VBAX Regular TButhe's Avatar
    Joined
    Sep 2004
    Location
    Sioux Falls, SD
    Posts
    64
    Location
    Quote Originally Posted by MOS MASTER

    .... so the Beauty has a great KB!


    I wish!! Thanks Joost but I'm afraid not. Tbuthe is just my first initial and part of my last name. But I sure do like this site even more now that I am called "The Beauty". You made my day/week/month/year!!

    Take care and thanks again for the help - it works fine for me.
    Thanks,
    Tracy

  14. #14
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Well Tracy...You're a beauty to me!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

Posting Permissions

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