Deleting bookmarks doesn't work

M

Mike

Hi,

I have a template with 2 formfields containing the following
bookmarks:

- Name
- Place

The formfields are filled using a userform:

ActiveDocument.FormFields("Name").Result = Formname.Controls(Name)
ActiveDocument.FormFields("Place").Result = Formname.Controls(Place)

Then I want to insert a 'new page section' and in the new section I
want to insert the same template again, with thus the same bookmarks.
To prevent multiple bookmarks with the same name, I delete all
bookmarks before I make the new page section using:

ActiveDocument.Bookmarks("Name").Delete
ActiveDocument.Bookmarks("Place").Delete

I then insert the template on the second page.

What I want is to use the userform again to now fill the bookmarks on
the second page. However, if I use

ActiveDocument.FormFields("Name").Result = Formname.Controls(Name)
ActiveDocument.FormFields("Place").Result = Formname.Controls(Place)

again (assuming the Name bookmark is now on the second page due to the
template and having deleted all other bookmarks), the formfields on
the 1st page are filled again in stead of the second page.

It looks like the bookmark from the first page is not entirely
deleted! If I however unprotect the page and look in the property of
the formfield of the first page, there is no bookmark!

Can someone help me please?

Thanks, Mike
 
G

Greg Maxey

Hi,

I have a template with 2 formfields containing the following
bookmarks:

- Name
- Place

The formfields are filled using a userform:

ActiveDocument.FormFields("Name").Result = Formname.Controls(Name)
ActiveDocument.FormFields("Place").Result = Formname.Controls(Place)

Then I want to insert a 'new page section' and in the new section I
want to insert the same template again, with thus the same bookmarks.
To prevent multiple bookmarks with the same name, I delete all
bookmarks before I make the new page section using:

ActiveDocument.Bookmarks("Name").Delete
ActiveDocument.Bookmarks("Place").Delete

I then insert the template on the second page.

What I want is to use the userform again to now fill the bookmarks on
the second page. However, if I use

ActiveDocument.FormFields("Name").Result = Formname.Controls(Name)
ActiveDocument.FormFields("Place").Result = Formname.Controls(Place)

again (assuming the Name bookmark is now on the second page due to the
template and having deleted all other bookmarks), the formfields on
the 1st page are filled again in stead of the second page.

It looks like the bookmark from the first page is not entirely
deleted! If I however unprotect the page and look in the property of
the formfield of the first page, there is no bookmark!

Can someone help me please?

Thanks, Mike

Mike,

I don't have a solution for you, but I can show that deleting the
bookmark associated with a formfield does "not" change the formfield
name. In a new document insert on text formfield and then run the
following code:

Sub Scratchmacro()
With ActiveDocument
.Bookmarks("Text1").Delete
MsgBox .FormFields(1).Name
.FormFields("Text1").Result = "Some text"
End With
End Sub
 
M

Mike

Mike,

I don't have a solution for you, but I can show that deleting the
bookmark associated with a formfield does "not" change the formfield
name. In a new document insert on text formfield and then run the
following code:

Sub Scratchmacro()
With ActiveDocument
.Bookmarks("Text1").Delete
MsgBox .FormFields(1).Name
.FormFields("Text1").Result = "Some text"
End With
End Sub- Tekst uit oorspronkelijk bericht niet weergeven -

- Tekst uit oorspronkelijk bericht weergeven -

Thanks, but thats not what I'm looking for. Also tried to change the
name of the field, no luck....

Anyone else?

Mike
 
J

Jean-Guy Marcil

Mike was telling us:
Mike nous racontait que :
Thanks, but thats not what I'm looking for. Also tried to change the
name of the field, no luck....

Anyone else?

Mike

Instead of renaming the formfields already in place, you should rename the
one you just inserted in the document before you fill them out with text.
The reason being that if you insert formfields that have the same name as
exiting formfields, Word will internally choke and if you try to run simple
code like:

myFormField.Name = "Test" & i

Word will return an error on those fields that had conflicting names.

There is a workaround (my code below) but that removes the formfield content
and resets it to its default value. This is fine with fresh formfields that
do not have content yet, but not with existing ones.

'_______________________________________
Sub RenameFormfields()

Dim lngSecNumber As Long

If Selection.Sections.Item(1).Index = 1 Then
MsgBox "You cannot use this procedure from the first section in the
document."
Exit Sub
End If

lngSecNumber = Selection.Sections.Item(1).Index

With ActiveDocument.Sections(lngSecNumber).Range
.FormFields(1).Select
With Dialogs(wdDialogFormFieldOptions)
.Name = "Name" & lngSecNumber
.Execute
End With
.FormFields(2).Select
With Dialogs(wdDialogFormFieldOptions)
.Name = "Place" & lngSecNumber
.Execute
End With
End With
'_______________________________________

But, if you use a "clean" system, you can use something like:

myFormField.Name = "Test" & i

For that to Work, create an Autotext with the formfields, the text and the
section break and name those formfields "Name" and "Place".

Insert them in the first section and run code like:

'_______________________________________
Dim lngSecNumber As Long

lngSecNumber = Selection.Sections.Item(1).Index

With ActiveDocument.Sections(lngSecNumber).Range
.FormFields(1).Name = "Name" & lngSecNumber
.FormFields(2).Name = "Place" & lngSecNumber
End With
'_______________________________________

Every time you insert a new section, run the code to rename the formfields.
This way you will never have conflicting names.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
M

Mike

Mike was telling us:
Mike nous racontait que :









Instead of renaming the formfields already in place, you should rename the
one you just inserted in the document before you fill them out with text.
The reason being that if you insert formfields that have the same name as
exiting formfields, Word will internally choke and if you try to run simple
code like:

myFormField.Name = "Test" & i

Word will return an error on those fields that had conflicting names.

There is a workaround (my code below) but that removes the formfield content
and resets it to its default value. This is fine with fresh formfields that
do not have content yet, but not with existing ones.

'_______________________________________
Sub RenameFormfields()

Dim lngSecNumber As Long

If Selection.Sections.Item(1).Index = 1 Then
MsgBox "You cannot use this procedure from the first section in the
document."
Exit Sub
End If

lngSecNumber = Selection.Sections.Item(1).Index

With ActiveDocument.Sections(lngSecNumber).Range
.FormFields(1).Select
With Dialogs(wdDialogFormFieldOptions)
.Name = "Name" & lngSecNumber
.Execute
End With
.FormFields(2).Select
With Dialogs(wdDialogFormFieldOptions)
.Name = "Place" & lngSecNumber
.Execute
End With
End With
'_______________________________________

But, if you use a "clean" system, you can use something like:

myFormField.Name = "Test" & i

For that to Work, create an Autotext with the formfields, the text and the
section break and name those formfields "Name" and "Place".

Insert them in the first section and run code like:

'_______________________________________
Dim lngSecNumber As Long

lngSecNumber = Selection.Sections.Item(1).Index

With ActiveDocument.Sections(lngSecNumber).Range
.FormFields(1).Name = "Name" & lngSecNumber
.FormFields(2).Name = "Place" & lngSecNumber
End With
'_______________________________________

Every time you insert a new section, run the code to rename the formfields.
This way you will never have conflicting names.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:http://www.word.mvps.org- Tekst uit oorspronkelijk bericht niet weergeven -

- Tekst uit oorspronkelijk bericht weergeven -

Thanks, you helped me a great deal!

Mike
 

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