Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Ghost" Bookmarks in Word

Status
Not open for further replies.

SilentAiche

Technical User
Dec 21, 2004
1,325
US
Bear with me, this is going to be a long one. I'm using Word 2002 on Win XP.

I have a 14 page document saved as a form, with about a dozen small independent tables. There are several hundred Form Fields that the user tabs between to complete the document, including check boxes, table cells and basic memo-type fields.

Everything was working fine until I realized a user could hit Enter while in a field and Word would insert a hard carriage return (at least that's what I call it). A paragraph marker. This occurs in table cells and memo-type fields, not check boxes.

To my surprise, when I posted this problem on Jan. 31 it drew no replies. However, a Google search led me to the following code that seems to work well:

Code:
Sub EnterKeyMacro()
   ' Check whether the document is protected for forms
   ' and whether the protection is active.
      If ActiveDocument.ProtectionType = wdAllowOnlyFormFields And _
      Selection.Sections(1).ProtectedForForms = True Then
         ' Retrieve the bookmark of the current selection.
         ' This is equivalent to the name of the form field.
         myformfield = Selection.Bookmarks(1).Name
         ' Go to the next form field if the current form field
         ' is not the last one in the document.
         If ActiveDocument.FormFields(myformfield).Name <> _
         ActiveDocument.FormFields(ActiveDocument.FormFields.Count) _
         .Name Then
            ActiveDocument.FormFields(myformfield).Next.Select
         Else
            ' If the current form field is the last one,
            ' go to the first form field in the document.
            ActiveDocument.FormFields(1).Select
         End If
      Else
      ' If the document is not protected for forms,
      ' insert a tab stop character.
         Selection.TypeText Chr(13)
      End If
   End Sub

Sub AutoNew()
    ' Do Not protect the template containing these macros.
      CustomizationContext = ActiveDocument.AttachedTemplate
      ' Bind the ENTER key to the EnterKeyMacro.
      KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyReturn), _
      KeyCategory:=wdKeyCategoryMacro, Command:="EnterKeyMacro"
      ' Reprotect the document with Forms protection.
      ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
  End Sub

Sub AutoOpen()
   ' This macro will reassign the ENTER key when you open an existing
   ' Word form fields document.
      CustomizationContext = ActiveDocument.AttachedTemplate
      ' Bind the Enter key to the EnterKeyMacro.
      KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyReturn), _
      KeyCategory:=wdKeyCategoryMacro, Command:="EnterKeyMacro"
 End Sub

Sub AutoClose()
      CustomizationContext = ActiveDocument.AttachedTemplate
      FindKey(KeyCode:=BuildKeyCode(wdKeyReturn)).Disable
      ' Disables prompt to save template changes.
      Templates(1).Save
    End Sub

With this code in place, the Enter key now behaves like the Tab key. However, for this code to work each Form Field must have a Bookmark name, or else hitting Enter produces a Runtime 5941 error ("requested member does not exist"). I am currently going through this entire document and naming each bookmark.

THE PROBLEM (at last): Under Tools\Options\View I have checked Bookmarks to that I can visually tell if a Form Field already has a bookmark. The bookmarks are flagged by square gray brackets [ ] around the field. However, in several tables the brackets appear where no Form Field exists. Picture the following three-column table, where all the zeroes are in bookmarked Form Fields.

Reported Verified
Free [0] [0]
Reduced [0] [0]
Denied [0] [0]
T[otals] [0] [0]

Bookmark brackets mysteriously appear in the word "Totals," where there is no bookmark (it's not even a Form Field), and they have the effect of causing the Enter key to generate the error mentioned above. I cannot highlight, select or DELETE the brackets (which is the heart of the problem). However, if I type between them, they move to keep the text within the brackets. In some tables the brackets occur multiple times. In one case, an actual bookmarked Form Field appears to be overlaid with the mystery brackets. It appears like one of the numeric fields above, but with an extra space before the last bracket: [0 ]. Turning off the bookmark view does not solve the problem.

Sorry for the length of this. All thoughts welcome!

THanks,
Tim
 
#1. those brackers are not form fields, they are Fields.

#2. form fields ALWAYS have a bookmark name - except on ONE circumstance. If you Copy and Paste a form field the original form retains the original name, the pasted ones do NOT have a bookmarks name. They can still be referenced by Index number.

text1
text2
text3 are formfields; they have ALSO Bookmark names ("text1", "text2", "text3")

So you can refer to them as either:

Activedocument.Bookmarks("text1")
ActiveDocument.FormFields("text1")

The properties and method differ, but these form fieldd beong in BOTH collections.

If you copy the three and paste them three times, you will have nine (9) form fields. The first three will have bokmark names - as stated (("text1", "text2", "text3"), but the ones after will have NO bookmark name. You can only refer to them by index number.

ActiveDocument.FormFields(9)

They are NOT part of the Bookmark collection.

In your case these are Fields - which are different types of objects from Bookmarks, or Form Fields. It is possible to nest things form fields within Fields etc.

Try using Alt-F9 tio see if the field ciode change. Repost back here. Thanks.

Gerry
 
Gerry,

Thanks for the quick response. Yes, when I began this document I did a great deal of copying of form fields, not realizing the future ramifications with respect to bookmarks.

When I double-click on a field it brings up a dialog box labeled "Text Form Field Options" which I guess is why I keep calling them Form Fields. In the Bookmark section of this dialog box is where I have been naming the bookmarks of the ones that did not have a name (the ones I copied). After naming them, the brackets appear, the Enter key behaves properly, etc.

The fields in question (except for the check boxes) were inserted using the Forms Toolbar and the button labeled "Text Form Field." From your reply above, I guess I am confused about the difference between what I keep calling a Form Field and what you call simply a Field.

Alt-F9 did not reveal any field code within the mystery brackets.

You also mentioned index numbers. How does one determine a field's index number? Is it a straight count of it's position in the document- left to right, top to bottom?

On somewhat of a side note, somewhere I read something of yours (a FAQ, perhaps) wherein you were emphatic that form fields should be explicitly named. Does this mean you recommend changing the default names (Text12, etc)?

Many thanks for the continued help.
Tim
 
Yes, I am strongly in support of the idea of explicitly naming objects. In particular Form Fields. ALL object should be named explicitly IMHO, but Formf Fields are a special case because Word dynamically changes the names depending on their position in the document.

Say you have Text1, then Text2, then Text3. Say you have code some where that states:
If ActiveDocument.FormFields("Text3").Result = "Bob Brown" Then
ActiveDocument.FormFields("Text6").Result = _
ActiveDocument.FormFields("Text3").Result


This states If text3 = "Bob Brown" then "Text6" = "Bob Brown"

Say you MOVED Text 6 so it is now between Text4, and Text5. If the form field is NOT explicitly named, it automatically becomes Text5, and Text5 becomes Text6. Yhe code would put the information inti the wrong form field. It would still make Text6 = "Bob Brown", but the issue is Text5 would have BECOME Text6. Word gives names in the order they appear in the document.

But if you changed text3 to "InputName", and
Text6 to "CustomerName"...no matter where you may move them, the logioc is explicit and will place the data precisely where you tell it....ergardless of location. Shrug, even though it is a pain in the ass, I always explicitly name things.

This is the esame code with explicit naming:

If ActiveDocument.FormFields("InputName").Result = _
"Bob Brown" Then
ActiveDocument.FormFields("CustomerName").Result = _
ActiveDocument.FormrFields("InputName").Result

ASlt-F9 (hopefully done when the documen is NOT protected for forms) should have changed the text form fields to show as { Form Text }. If they did not, then the document must have been protected. Form Fields show as Fields with FORMTEXT as the text.

Form Fields are Fields, and are Bookmarks as well.
Bookmarks are not Fields; they are named Ranges.

Form Fields may show as grey areas, or not. You can toggle this on or off. Set as off, text form field are indistinquisable from any text around them - assuming similar Style and/or format.

The index number (for form fields) is the order in the document.

Gerry
 
Gerry,

Very informative- thanks!

I didn't realize the Text6 type names were dynamic and could change if the field were moved. Is this also true for explicitly named fields if the name ends in a number? I ask because I named almost everything as "Bx3" or "Chk7" or something else that ended with a number. It may be a moot point since I think the placement of the fields is stable at this point, but I was curious.

Also, the form was unprotected when I pressed Alt-F9. "FORMTEXT" and "FORMCHECKBOX" showed up where expected. The only place I did not see any kind of field code was between the mystery brackets which are the heart of my problem.

Let me know if you have any ideas on how to get rid of these strange brackets.

THanks again,
Tim
 
No, if you go into properties and explictly name them, then they are named, and the name persists. So if you have a bunch of form fields with a name and a incremental number (a common practice), they are still explicitly named. So they could, and can, use reference to them with a string plus a integer variable, like:

Code:
Sub GiveMeResults()
Dim i As Integer
Dim var
Dim msg As String

On Error Resume Next
i = 1
For var = 1 To ActiveDocument.FormFields.Count
    msg = msg & ActiveDocument.FormFields("biz" & i).Result & _
            vbCrLf
    i = i + 1
Next
MsgBox msg
End Sub
would display a message box listing the results of all formfields with "biz" and a number (stated as the variable i).

As for the mystery brackets...

Run this by me again. They appear in the bottom row of a table? Could they be Formula fields? What happens if you highlight them and delete them - make a copy of you r file first. I suspect they are formula fields.

Gerry
 
Gerry,

You won't believe this, but I just stumbled upon a solution, which is a little annoying because I never understood the problem.

After reading your last post, I revisited the document so that I could be very specific about what all I had tried to do to get rid of the mystery brackets. To answer your last question, they could appear on any row in the table, but are most often in the left hand column that contains labels and has never had a Form Field inserted.

In the example from my first post, the table cell might look like T[otals]. If I highlight the cell and hit delete, it turns into []. When I hit Alt-F9 (on the unprotected doc), nothing appears between these brackets. While I could not select the brackets within the cell as characters, I could highlight whole cells. During such highlighting, the following had no effect on the brackets: cut, delete, clear, cussing.

What ended up working was copying an uninfected cell over the infected one. Simple solution to a very aggravating problem.

As usual, thanks for all your advice. I learned a lot.
Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top