PDA

View Full Version : Solved: Trouble looping through bookmarks



TButhe
06-28-2005, 09:57 AM
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. :rotlaugh: :giggle :rofl:

MOS MASTER
06-28-2005, 10:26 AM
Hi, :yes

Use this sub instead for the exit macro of the last formfield:
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


Enjoy! :whistle:

TButhe
06-28-2005, 10:42 AM
That is SWEET!! :cloud9: Thank you! You really are the Master. I will be sure to give you credit in the KB article.
:beerchug:

fumei
06-28-2005, 10:42 AM
Dim BM As Bookmark
For Each BM In ActiveDocument.Bookmarks()
...do stuff to the bookmark
Next

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 ActiveDocument.Bookmarks("\Para").Select 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.

fumei
06-28-2005, 10:44 AM
Ah Joost, not only did you post the document protection issue, but also the formfield vs bookmark issue. You da man.

MOS MASTER
06-28-2005, 10:44 AM
That is SWEET!! :cloud9: Thank you! You really are the Master. I will be sure to give you credit in the KB article.
:beerchug:
Your most welcome! :beerchug:

MOS MASTER
06-28-2005, 10:46 AM
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! :bow:

TButhe
06-28-2005, 10:49 AM
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.

MOS MASTER
06-28-2005, 10:51 AM
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! :cloud9:

fumei
06-28-2005, 10:55 AM
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.

MOS MASTER
06-28-2005, 11:06 AM
Do you mean in the dialog edit box?
Meaning all including the bookmarks so that you are indeed overwriting the entire formfield? :whistle:

MOS MASTER
06-28-2005, 12:48 PM
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, :yes

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! :whistle:

TButhe
06-29-2005, 02:03 PM
.... so the Beauty has a great KB! :whistle:
:rofl: :rofl: :rofl: :rofl: :rofl: :rofl: :rofl: :rofl: :rofl: :rofl: :rofl: :rofl: :rofl:

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.
:thumb

MOS MASTER
06-29-2005, 02:13 PM
Well Tracy...You're a beauty to me! :hug: