PDA

View Full Version : Issue with Bookmarking form fields



ASkolnick
02-13-2013, 05:58 PM
We have a program which uses the Range.BookmarkID's to determine the Current form field. This normally works fine, but on one workstation, the Range.BookmarkID of form fields never gets changed from 0 which makes it invalid.

It is an Office '07 environment.

Any direction to which way to go would be great. Thanks.

macropod
02-13-2013, 06:37 PM
Hi ASKolnick,

Please post the code you're using (using the VBA tags to ensure gets formatted correctly by the board software).

Also, have you tried repairing the Office installation (via Word Options|Resources|Diagnose, or via Programs & Features > Microsoft Office > Change in the Windows Control Panel) on the faulty workstation?

fumei
02-13-2013, 08:38 PM
It is possible the formfields do not have bookmarks. In which case BookmarkID returns 0. If the current formfield was copied, there is no bookmark, and thus Range.BookmarkID = 0. And do remember that bookmarkID returns a number that MEANS the order that bookmark (NOT fomfield) is in the document. That may (or may not) equate to the order the formfield is in the document.

In other words the FIFTH formfield does not have to have BookmarkID = 5.

macropod is correct though, we need to see some code.

ASkolnick
02-14-2013, 07:33 AM
Thanks MacroPod and Fumei.
MacroPod, they are 3rd party clients and we have already recommended the Repair/Reinstallation of Word.

I posted the code and the code works fine other than on the "bad" workstations.

Function CheckBookmarks(Optional ByVal bMsg = True) As Integer
' Word forms only
'
' When you copy a field in word, it will have no bookmark.
'
' Check that all the form fields have bookmarks and, after confirmation,
' add bookmarks to any fields which don't.
'
' you can use this diagnostic in the immediate window (ctrl/G in VBA)
' for each ff in ActiveDocument.FormFields: print ff.name, ff.range.bookmarkid, ff.result: next ff
Const NM_PREFIX = "zAddedBkmk"
CheckBookmarks = vbOK
#If ExcelForm Then
#Else
Dim ff As FormField
Dim sMsg, bSevereWarning As Boolean
Dim sResult, sName
Dim dlg As Dialog
Dim FieldsToFix As New Collection
Dim nSuffix As Integer

On Error Resume Next

' find the problem fields and add to FieldsToFix
For Each ff In WordFormDocument.FormFields
' The check for wdFieldFormTextInput was added to fix some problem (not sure what
' it was) ... but we might need bookmarks on these other field types too
' But don't try to fix demographic fields
If ff.Type = wdFieldFormTextInput _
Or ff.Type = wdFieldFormCheckBox _
Or ff.Type = wdFieldFormDropDown Then

' Feb 22, 2011 This is to fix a weird problem in 'CH Surgical orders' from Concord Ortho
sName = ff.Name
If Err.Number <> 0 Then ' the checkbox fields were valid but their names were not
Err.Clear
ff.Delete ' the fields are not visible in the form so just delete them
Err.Clear
bSevereWarning = True ' tell user to save as a new form
Else
If ff.Name = "" _
Or ff.Range.BookmarkID = 0 Then ' v04.11 renames dem fields too
FieldsToFix.Add ff
End If
If InStr(ff.Name, NM_PREFIX) > 0 Then ' v04.11 renames dem fields too
' if CheckBookmarks was run before, nSuffix gets set to the last bkmk inserted
If CInt(Right(ff.Name, 3)) > nSuffix Then nSuffix = CInt(Right(ff.Name, 3))
End If
End If
End If
Next ff

' look for another class of problem fields and add to FieldsToFix if found
zCheckBookmarkIDs FieldsToFix

If FieldsToFix.Count > 0 Then

' v6.03 Msg is conditional
If bMsg Then
sMsg = "Some form fields are unnamed." & vbCrLf & "Click Yes to bookmark these fields."
If MsgBox(sMsg, vbYesNo) = vbNo Then
CheckBookmarks = vbCancel ' v6.03 AutoOpen will exit
Exit Function
End If
End If

' fix the errors
For Each ff In FieldsToFix
sResult = ff.Result
If Err.Number = OBJECT_DELETED Then ' not sure why this happens
' in the first form where this happened, the error could be ignored
' ... but, for a form created by an IBJI user, the field had to be deleted
Err.Clear
ff.Delete ' IBJI v04.11
Err.Clear ' IBJI
bSevereWarning = True ' IBJI
Else
ff.Select
' (for info about dialogs, search VBA help for 'built-in dialog box')
Set dlg = Dialogs(wdDialogFormFieldOptions)
nSuffix = nSuffix + 1
sName = NM_PREFIX & Format(nSuffix, "0000")
If Left(ff.Name, 3) = "dem" Then sName = "dem" & sName ' v04.11
dlg.Name = sName
dlg.Execute
WordFormDocument.FormFields(sName).Result = sResult
End If
Next ff

' v6.03 always returns vbOK if running without messages
If bMsg Then
MsgBox "AutoOpen will terminate." & vbCrLf & "Save the form, exit and re-open."
CheckBookmarks = vbCancel
End If

End If

If bSevereWarning Then
If bMsg Then
sMsg = "Some fields with invalid names or bookmarks have been deleted."
MsgBox sMsg, vbExclamation + vbOKOnly
End If
End If

#End If
End Function

macropod
02-14-2013, 10:46 PM
Hmm, it seems using the VBA tags was too much to ask for.

If the code works fine on some workstations but not others, that suggests either:
• the files being worked on a different; or
• the 'bad' workstations have some faulty/different software.
Have the files been validated? Similarly, you say:

we have already recommended the Repair/Reinstallation of Word
but has it been done??? FWIW a reinstall rarely fixes anything a repair won't.

ASkolnick
02-15-2013, 07:20 AM
Thank you every body for your help.
I did find an alternate solution which seems to have worked for this workstation.

ASkolnick
02-15-2013, 07:33 AM
I need to take something out of the code that I posted and I was wondering how I remove it.