How to add a bookmark to an existing formfield using a macro/proce

P

pwrichcreek

I'd like to run a macro to check for formfields that do not have a bookmark
defined and insert a bookmark. When I run the following code, I get an error
-2147467259,
description = "automation error unspecified". Can someone show me how to fix
the code?

TIA,

Phil

i = 1
ActiveDocument.FormFields(i).Select
Do Until i > ActiveDocument.FormFields.Count
If Selection.FormFields(1).Name = "" Then
Selection.FormFields(1).Name = ffxname <==== ERROR here
...
If i < ActiveDocument.FormFields.Count Then
ActiveDocument.FormFields(i).Next.Select
End If
i = i + 1
Loop
 
J

Jezebel

There's a bug in Word: you can't (using this method) set the name of a
formfield if it doesn't have a name to start with; although if it has a name
to start with, you can use this code to change.

You can work around the problem by executing the field properties dialog --

Dim pFormField as Word.FormField

For each pFormField in ActiveDocuments.FormFields

If Len(pFormField.Name) = 0 then

pFormField.Select
With Dialogs(wdDialogFormFieldOptions)
.Name = "....."
.Execute
End With

end if

Next
 
P

pwrichcreek

Thanks for the info Jezebel.
For each pFormField in ActiveDocuments.FormFields
I stared at this for 15 minutes, trying to figure out why is was not
working. Finally realized that ActiveDocuments does not want the 's'.
You can work around the problem by executing the field properties dialog --
I was not able to get this to work. As provided in your snippet, the code
appears to run OK, but it looks like it makes all the changes to the first
formfield it encounters. I'm pretty sure I ran it exactly as shown, but I may
have somehow broken it on my own. I tried several variations as well as
copy-and-paste yours and could not get anything to work.

I'm still interested in getting the dialog approach to work, but need to
finish the rest of the form. I ended up fixing the formfields that have no
bookmark name manually.

Phil
 
P

pwrichcreek

Jezebel,
The dialog works on whichever formfield is selected at the time.

I know. And the pFormField.Select in the code you gave should select each
formfield in the document. Before I ran all of your code I ran just the For
loop with the .Select and observed that each field was in fact selected as
the code ran (the highlighting went from field to field). After I added the
dialogs stuff it no longer advanced from field to field.

I'll start from scratch and try it again.

Thanks,

Phil
 
P

pwrichcreek

Jez,

I tried it all again from scratch and it works fine. No telling what I got
tangled up during my trial-and-error-ing.

Thanks,

Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top