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