Renaming bookmarks when inserting file

B

bryan

I would like to be able to rename the bookmarks on a document that I am
inserting into a template new doc. The template has the abilitiy to add this
document many times and I will run sql statements to populate some of the
fields on the inserted file.

Let's say I'm inserting test1.doc which has 5 formfields, text1 - text5.
How can I rename these upon inserting the file?
That way I can maintain 1 insertable doc rather than having multiple

Thanks,
Bryan
 
F

fumei via OfficeKB.com

You can not rename bookmarks. You CAN rename formfields.

I am not quite following: "on a document that I am inserting into a template
new doc"

You are cloning a new doc from a template and THEN inserting another document
into that new document? Do I have that correct? Seems a little odd, but OK.

How do you want to rename the formfields (not the bookmarks). Note that when
you rename the formfields, the bookmarks are also created with those names.

So say you insert a file with Text1, Text2, Text3 as the formfield names.

If you select that chunk (the newly inserted file chunk), and run:

Dim oFF As FormField
Dim j As Long
j = 1
For Each oFF In Selection.FormFields
oFF.Name = "Yadda" & j
j = j + 1
Next

the previously named formfields(Text1, Text2, Text3) will be renamed Yadda1,
Yadda2, Yadda3. The bookmarks will be the same names.
 
B

bryan

I appreciate the quick response. I will be able to try this mid-week.
I created a template for users in our Commercial Lines area. When they
create the new doc based on the template I pre-populate fields based upon the
client id.
A new request came with wanting the ability to attach several id cards for
each unit of a policy. When each of these get attached I will run an sql
statement to populate the attached doc form fields for that unit. Rather than
create 10-15 different files (all the same) and changing the form field name,
I was looking for a way to have only one doc and then changing the formfield
name upon adding.

Hopefully this clears up what I am trying to accomplish.
Again thanks,
Bryan
 
B

bryan

Also,
What is meant by "when you select that chunk"?
When and how do I do this? I am fairly new to this aspect.
Here is my insert of file:

Dim myDoc As Document
Dim docrange As Range

Set myDoc = ActiveDocument
With myDoc
.Unprotect
Set docrange = .Range
docrange.Collapse wdCollapseEnd
docrange.InsertBreak wdSectionBreakNextPage
Set docrange = .Range
docrange.Collapse wdCollapseEnd
docrange.InsertFile "W:\Commercial\WC ImageRight Templates\Document
Attachments\ID Letter.doc"
.Protect wdAllowOnlyFormFields, NoReset
End With

Thanks again,
Bryan
 
D

Doug Robbins - Word MVP

chunk - the text inserted into the document by the action of inserting the
file.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

Jean-Guy Marcil

fumei via OfficeKB.com said:
You can not rename bookmarks. You CAN rename formfields.

What do you mean by that?
The following code does rename a bookmark...


Dim rngBook As Range
Dim strNewBookName As String

Const strBookName As String = "Test_1"

strNewBookName = "Book_1"

With ActiveDocument.Bookmarks
Set rngBook = .Item(strBookName).Range
.Item(strBookName).Delete
.Add strNewBookName, rngBook
End With


Maybe you meant that once you rename a bookmark, all REF fields and other
fields that refer to the renamed bookmark will now display errors...
 
B

bryan

This is not working. It renames my checkbox but not the form fields of the
file I am adding. The file I am adding has "Text1" through "Text12" which I
want to remane so I can update.
"t" is the unit number I will pass in.
This code is renaming "Check1" to "Unit741"
Here is my code in adding the file and trying the rename:
Sub chk1()
'
' chk1 Macro
' Macro created 5/28/2008 by bjsorens
'
If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then
Dim myDoc As Document
Dim docrange As Range
Dim oFF As FormField
Dim j As Long
t = 74
j = 1

Set myDoc = ActiveDocument
With myDoc
.Unprotect
Set docrange = .Range
docrange.Collapse wdCollapseEnd
docrange.InsertBreak wdPageBreak
Set docrange = .Range
docrange.Collapse wdCollapseEnd
docrange.InsertFile "U:\\MN Vehicle Insurance Identification Card.doc"
For Each oFF In Selection.FormFields
oFF.Name = "Unit" & t & j
j = j + 1
Next
.Protect wdAllowOnlyFormFields, NoReset
End With
End If
End Sub

What I want is only "Text1" through "Text12" of the added document to
change, nothing on the main form.

Thanks,
Bryan
 
B

bryan

fumei indicated 'if you select that chunk (the newly inserted file chunk) and
then run this code it would work. I must be missing the selection of this
inserted file.
How do I select that?

Thanks,
Bryan
 
H

Helmut Weber

Hi Bryan,

like that:

Sub Test400A()
Selection.Bookmarks.Add "Test"
Selection.InsertFile "C:\Test\Word1\Experts.doc"
ActiveDocument.Bookmarks("Test").End = Selection.start
ActiveDocument.Bookmarks("Test").Select
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
B

bryan

how do i use this code into my adding of the file.
Set myDoc = ActiveDocument
With myDoc
.Unprotect
Set docrange = .Range
docrange.Collapse wdCollapseEnd
docrange.InsertBreak wdPageBreak
Set docrange = .Range
docrange.Collapse wdCollapseEnd
docrange.InsertFile "U:\\MN Vehicle Insurance Identification Card.doc"
.Protect wdAllowOnlyFormFields, NoReset
End With

Thanks,
bryan
 
B

bryan

I'm learning working with VBA and Word as I'm going along.
All this information is great but, it's hard for me to figure how to use
this code with my code.
I'm inserting a file and would like to rename the formfields of the inserted
file.
Her is my insertion:
If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then
Dim myDoc As Document
Dim docrange As Range

Set myDoc = ActiveDocument
With myDoc
.Unprotect
Set docrange = .Range
docrange.Collapse wdCollapseEnd
docrange.InsertBreak wdPageBreak
Set docrange = .Range
docrange.Collapse wdCollapseEnd
docrange.InsertFile "U:\MN Vehicle Insurance Identification Card.doc"
.Protect wdAllowOnlyFormFields, NoReset
End With

How can I select this added file and rename the formfields?
Previously I had used code from fumei to rename and I had that right after
inserting the file.
What that did was rename 'Check1'.
I only want to rename the formfields of the added file.

Thanks,
Bryan
 
B

bryan

Can somebody help me with this?
I've tried using fumei's code which renames "Check1", not the formfileds
from adding a file.
I've used Helmut's code to select, but it bombs on the
selection.insert.........

When I insert a file, I want to rename the form fields of the inserted file.
How?
Help!!!
Bryan
 
D

Doug Robbins - Word MVP

The following code will rename each of the formfields in that last Section
of a document with a name of the form Sectionnm where n is the number of the
Section and m is the number of the formfield in that Section:

Dim i As Long
With ActiveDocument.Sections.Last.Range
For i = 1 To .FormFields.Count
.FormFields(i).Name = "Section" & ActiveDocument.Sections.Count & i
Next i
End With

Each formfield must already have a name for this to work and it assumes that
you would be inserting the file into a new section at the end of the
document.
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
B

bryan

Hi Doug,
Unfortunately this does not work and it may be my fault in my explanation.
I inserted this code right after I insert the file. The file I am inserting
is 2 pages.
Results: I added the file and my formfields were still Text1 through Text10.
When I added this file twice, the 2nd formfields were all blank.

Also,
Since users can add this file multiple times as they could be requesting id
cards for many vehicles, how can I get the section number?
Since I will be updating the formfields with specific information of the
vehicle for each id card, I will need to know which section I am dealing
with. I am also assuming that the 'm' number starts at 1 for each file added.

Here is my code in adding the file along with trying to rename right after:
If ActiveDocument.FormFields("IDCard").Result <> "" Then
Dim myDoc As Document
Dim docrange As Range
Dim array2()
Dim strInfo
i = 0
k = 0
j = 0
str1 = ActiveDocument.FormFields("IDCard").Result
array1 = Split(str1, ",")
For Each Item In array1
If IsNumeric(Item) Then
ReDim Preserve array2(i)
array2(i) = Item
i = i + 1
Else
pos = InStr(1, Item, "-")
startnum = Mid(Item, 1, pos - 1)
endnum = Mid(Item, pos + 1, Len(Item))
num = startnum

For j = (startnum) To (endnum)
ReDim Preserve array2(i)
array2(i) = num
num = num + 1

i = i + 1
Next

End If

Next

For Each Item In array2
strInfo = getinfo(Item)
array3 = Split(strInfo, ",")
strrkst = array3(0)
If strrkst = "22" Then
strIDCard = "U:\CL Templates\P1004707\MN Vehicle Insurance Identification
Card.doc"
Else
strIDCard = "U:\CL Templates\P1004707\Non MN Vehicle Insurance
Identification Card.doc"
End If
Set myDoc = ActiveDocument
With myDoc
.Unprotect
Set docrange = .Range
docrange.Collapse wdCollapseEnd
docrange.InsertBreak wdPageBreak
Set docrange = .Range
docrange.Collapse wdCollapseEnd
docrange.InsertFile strIDCard
'
Dim z As Long
With ActiveDocument.Sections.Last.Range
For z = 1 To .FormFields.Count
.FormFields(i).Name = "Section" & ActiveDocument.Sections.Count & z
Next z
End With
.Protect wdAllowOnlyFormFields, NoReset
End With



strvyr = array3(1)
strmake = array3(2)
strmodl = array3(3)
strvin = array3(4)
'ActiveDocument.FormFields("text9").Result = strvyr & " " & strmake & " " &
strmodl
'ActiveDocument.FormFields("text10").Result = Trim(strvin)


Dim oField As Field
Dim oStory As Range
For Each oStory In ActiveDocument.StoryRanges

Do
For Each oField In oStory.Fields
If oField.Type = wdFieldRef Then
oField.Update
End If
Next oField
Set oStory = oStory.Next
Loop Until oStory Is Nothing
Next oStory
Next
End If



End Sub

Thanks,
Bryan
 
F

fumei via OfficeKB.com

Jean-Guy…

What do you mean by that?
The following code does rename a bookmark...

Dim rngBook As Range
Dim strNewBookName As String

Const strBookName As String = "Test_1"

strNewBookName = "Book_1"

With ActiveDocument.Bookmarks
Set rngBook = .Item(strBookName).Range
.Item(strBookName).Delete
.Add strNewBookName, rngBook
End With

With due respect, I would strongly disagree. No, it does not rename a
bookmark. It deletes a bookmark, and then creates a NEW one with the given
string. The existing one is NOT renamed. It is deleted. Your code in fact
states this:

..Item(strBookName).Delete

It is, in fact, deleted…gone. A new one is created (.Add). Yes, true, in
practical terms is seems like it is renamed, but technically…no, it is not
renamed.

Bryan,

Doug’s code should work for you. I am not sure what you are doing with all
your arrays, but IF Doug’s assumptions are correct:

“Each formfield must already have a name for this to work and it assumes that
you would be inserting the file into a new section at the end of the document.
â€

Then this is the way to go. Essentially it is code that applies a renaming
of the formfields – NOT the bookmarks – in the last Section. The assumption
is that you are inserting a section break, then the inserted file. Therefore
the inserted file IS the last section.

However, you are NOT inserting a Section break, you are inserting a Page
Break.

docrange.InsertBreak wdPageBreak

Change that to Section Break, a Next Page (wdSectionBreakNextPage ) I would
think. Now, using the last Section vis-à-vis Doug’s code should work.

NOTE: there is not much point in declaring a Document object and setting it:

Set myDoc = ActiveDocument

But never really using it. You set your document object, but still keep on
using things like:

ActiveDocument.Sections.Last.Range

ActiveDocument.Sections.Count

If you are going to have the document as an object, then you may as well
actually use the object fully.

Notice I moved the declaration of the variable z away from the instructions.
While not illegal syntax, generally variables are not declared deep within a
procedure. It makes it hard to to fully know what is going on. Generally,
variables are declared at the top of a procedure.

Dim z As Long

…other stuff


Set mydoc = ActiveDocument
mydoc.Unprotect

Set docrange = mydoc.Range

With docrange
.Collapse wdCollapseEnd
.InsertBreak wdSectionBreakNextPage
.Collapse wdCollapseEnd
.InsertFile strIDCard
End With

With mydoc.Sections.Last.Range
For z = 1 To .FormFields.Count
.FormFields(i).Name = "Section" & mydoc.Sections.Count & z
Next z
End With

mydoc.Protect wdAllowOnlyFormFields, NoReset

Hi Doug,
Unfortunately this does not work and it may be my fault in my explanation.
I inserted this code right after I insert the file. The file I am inserting
is 2 pages.
Results: I added the file and my formfields were still Text1 through Text10.
When I added this file twice, the 2nd formfields were all blank.

Also,
Since users can add this file multiple times as they could be requesting id
cards for many vehicles, how can I get the section number?
Since I will be updating the formfields with specific information of the
vehicle for each id card, I will need to know which section I am dealing
with. I am also assuming that the 'm' number starts at 1 for each file added.

Here is my code in adding the file along with trying to rename right after:
If ActiveDocument.FormFields("IDCard").Result <> "" Then
Dim myDoc As Document
Dim docrange As Range
Dim array2()
Dim strInfo
i = 0
k = 0
j = 0
str1 = ActiveDocument.FormFields("IDCard").Result
array1 = Split(str1, ",")
For Each Item In array1
If IsNumeric(Item) Then
ReDim Preserve array2(i)
array2(i) = Item
i = i + 1
Else
pos = InStr(1, Item, "-")
startnum = Mid(Item, 1, pos - 1)
endnum = Mid(Item, pos + 1, Len(Item))
num = startnum

For j = (startnum) To (endnum)
ReDim Preserve array2(i)
array2(i) = num
num = num + 1

i = i + 1
Next

End If

Next

For Each Item In array2
strInfo = getinfo(Item)
array3 = Split(strInfo, ",")
strrkst = array3(0)
If strrkst = "22" Then
strIDCard = "U:\CL Templates\P1004707\MN Vehicle Insurance Identification
Card.doc"
Else
strIDCard = "U:\CL Templates\P1004707\Non MN Vehicle Insurance
Identification Card.doc"
End If
Set myDoc = ActiveDocument
With myDoc
.Unprotect
Set docrange = .Range
docrange.Collapse wdCollapseEnd
docrange.InsertBreak wdPageBreak
Set docrange = .Range
docrange.Collapse wdCollapseEnd
docrange.InsertFile strIDCard
'
Dim z As Long
With ActiveDocument.Sections.Last.Range
For z = 1 To .FormFields.Count
.FormFields(i).Name = "Section" & ActiveDocument.Sections.Count & z
Next z
End With
.Protect wdAllowOnlyFormFields, NoReset
End With



strvyr = array3(1)
strmake = array3(2)
strmodl = array3(3)
strvin = array3(4)
'ActiveDocument.FormFields("text9").Result = strvyr & " " & strmake & " " &
strmodl
'ActiveDocument.FormFields("text10").Result = Trim(strvin)


Dim oField As Field
Dim oStory As Range
For Each oStory In ActiveDocument.StoryRanges

Do
For Each oField In oStory.Fields
If oField.Type = wdFieldRef Then
oField.Update
End If
Next oField
Set oStory = oStory.Next
Loop Until oStory Is Nothing
Next oStory
Next
End If

End Sub

Thanks,
Bryan
The following code will rename each of the formfields in that last Section
of a document with a name of the form Sectionnm where n is the number of the
[quoted text clipped - 74 lines]
 
B

bryan

The multiple arrays are set so that they can insert multiple files (id card
for each vehicle). Same as printing range of word (1,2,4,6-12).
The reason for insert with page break rather than section break is that with
section break I get the header and footer which I cannot have on the id card.

Is there a way to do this with using the pagebreak rather than section break?

Thanks,
Bryan

fumei via OfficeKB.com said:
Jean-Guy…

What do you mean by that?
The following code does rename a bookmark...

Dim rngBook As Range
Dim strNewBookName As String

Const strBookName As String = "Test_1"

strNewBookName = "Book_1"

With ActiveDocument.Bookmarks
Set rngBook = .Item(strBookName).Range
.Item(strBookName).Delete
.Add strNewBookName, rngBook
End With

With due respect, I would strongly disagree. No, it does not rename a
bookmark. It deletes a bookmark, and then creates a NEW one with the given
string. The existing one is NOT renamed. It is deleted. Your code in fact
states this:

.Item(strBookName).Delete

It is, in fact, deleted…gone. A new one is created (.Add). Yes, true, in
practical terms is seems like it is renamed, but technically…no, it is not
renamed.

Bryan,

Doug’s code should work for you. I am not sure what you are doing with all
your arrays, but IF Doug’s assumptions are correct:

“Each formfield must already have a name for this to work and it assumes that
you would be inserting the file into a new section at the end of the document.
â€

Then this is the way to go. Essentially it is code that applies a renaming
of the formfields – NOT the bookmarks – in the last Section. The assumption
is that you are inserting a section break, then the inserted file. Therefore
the inserted file IS the last section.

However, you are NOT inserting a Section break, you are inserting a Page
Break.

docrange.InsertBreak wdPageBreak

Change that to Section Break, a Next Page (wdSectionBreakNextPage ) I would
think. Now, using the last Section vis-à-vis Doug’s code should work.

NOTE: there is not much point in declaring a Document object and setting it:

Set myDoc = ActiveDocument

But never really using it. You set your document object, but still keep on
using things like:

ActiveDocument.Sections.Last.Range

ActiveDocument.Sections.Count

If you are going to have the document as an object, then you may as well
actually use the object fully.

Notice I moved the declaration of the variable z away from the instructions.
While not illegal syntax, generally variables are not declared deep within a
procedure. It makes it hard to to fully know what is going on. Generally,
variables are declared at the top of a procedure.

Dim z As Long

…other stuff


Set mydoc = ActiveDocument
mydoc.Unprotect

Set docrange = mydoc.Range

With docrange
.Collapse wdCollapseEnd
.InsertBreak wdSectionBreakNextPage
.Collapse wdCollapseEnd
.InsertFile strIDCard
End With

With mydoc.Sections.Last.Range
For z = 1 To .FormFields.Count
.FormFields(i).Name = "Section" & mydoc.Sections.Count & z
Next z
End With

mydoc.Protect wdAllowOnlyFormFields, NoReset

Hi Doug,
Unfortunately this does not work and it may be my fault in my explanation.
I inserted this code right after I insert the file. The file I am inserting
is 2 pages.
Results: I added the file and my formfields were still Text1 through Text10.
When I added this file twice, the 2nd formfields were all blank.

Also,
Since users can add this file multiple times as they could be requesting id
cards for many vehicles, how can I get the section number?
Since I will be updating the formfields with specific information of the
vehicle for each id card, I will need to know which section I am dealing
with. I am also assuming that the 'm' number starts at 1 for each file added.

Here is my code in adding the file along with trying to rename right after:
If ActiveDocument.FormFields("IDCard").Result <> "" Then
Dim myDoc As Document
Dim docrange As Range
Dim array2()
Dim strInfo
i = 0
k = 0
j = 0
str1 = ActiveDocument.FormFields("IDCard").Result
array1 = Split(str1, ",")
For Each Item In array1
If IsNumeric(Item) Then
ReDim Preserve array2(i)
array2(i) = Item
i = i + 1
Else
pos = InStr(1, Item, "-")
startnum = Mid(Item, 1, pos - 1)
endnum = Mid(Item, pos + 1, Len(Item))
num = startnum

For j = (startnum) To (endnum)
ReDim Preserve array2(i)
array2(i) = num
num = num + 1

i = i + 1
Next

End If

Next

For Each Item In array2
strInfo = getinfo(Item)
array3 = Split(strInfo, ",")
strrkst = array3(0)
If strrkst = "22" Then
strIDCard = "U:\CL Templates\P1004707\MN Vehicle Insurance Identification
Card.doc"
Else
strIDCard = "U:\CL Templates\P1004707\Non MN Vehicle Insurance
Identification Card.doc"
End If
Set myDoc = ActiveDocument
With myDoc
.Unprotect
Set docrange = .Range
docrange.Collapse wdCollapseEnd
docrange.InsertBreak wdPageBreak
Set docrange = .Range
docrange.Collapse wdCollapseEnd
docrange.InsertFile strIDCard
'
Dim z As Long
With ActiveDocument.Sections.Last.Range
For z = 1 To .FormFields.Count
.FormFields(i).Name = "Section" & ActiveDocument.Sections.Count & z
Next z
End With
.Protect wdAllowOnlyFormFields, NoReset
End With



strvyr = array3(1)
strmake = array3(2)
strmodl = array3(3)
strvin = array3(4)
'ActiveDocument.FormFields("text9").Result = strvyr & " " & strmake & " " &
strmodl
'ActiveDocument.FormFields("text10").Result = Trim(strvin)


Dim oField As Field
Dim oStory As Range
For Each oStory In ActiveDocument.StoryRanges

Do
For Each oField In oStory.Fields
If oField.Type = wdFieldRef Then
oField.Update
End If
Next oField
Set oStory = oStory.Next
Loop Until oStory Is Nothing
Next oStory
Next
End If

End Sub

Thanks,
Bryan
The following code will rename each of the formfields in that last Section
of a document with a name of the form Sectionnm where n is the number of the
[quoted text clipped - 74 lines]
Vista Small Business, Office XP
 
D

Doug Robbins - Word MVP

My suggestion relied on the new file being inserted into a new section at
the end of the document. Therefore, replace

docrange.InsertBreak wdPageBreak

with

docrange.InsertBreak wdSectionBreakNextPage

so that the file is inserted into a new Section.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
B

bryan

Almost there.
I changed the document I am adding to have only 2 form field and using ref
for the other info as that is general to all. The 2 form fields are specific.
The form fields are MakeMod and VehID

When I insert 1 file, it is re-naming the 1st form field but, not the other.
Result is Section22 and VehID.

When I add 2 files and loop:
First added file - MakeMod and Section22
Second added file - "" and Section32

I changed my code to insert a section break and then used fumei's code to
clean it up a bit.
Here is my code:

Set myDoc = ActiveDocument
myDoc.Unprotect

Set docrange = myDoc.Range

With docrange
.Collapse wdCollapseEnd
.InsertBreak wdSectionBreakNextPage
.Collapse wdCollapseEnd
.InsertFile strIDCard
End With

With myDoc.Sections.Last.Range
For z = 1 To .FormFields.Count
.FormFields(i).Name = "Section" & myDoc.Sections.Count & z
Next z
End With

myDoc.Protect wdAllowOnlyFormFields, NoReset

Thanks for the help,
Bryan
 
B

bryan

I got it.
Disregard my last post.
I seen where my error was in re-naming.
I was using z as the counter but, using i on the formfield.
I also changed to include section break rather than just page break.

Case closed!
Thanks to all for your suggestions and input.
I cannot tell you how much this is appreciated and how great this forum is.

Thanks again,
Bryan
 
D

Doug Robbins - Word MVP

You're welcome.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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