View Single Post
  #4   Report Post  
Max Moor
 
Posts: n/a
Default

Sub AddPageRefs()
Dim oRg As Range, oIns As Range
Dim strCode As String
Dim fldPg As Field

Set oRg = ActiveDocument.Range
oRg.TextRetrievalMode.IncludeFieldCodes = True
With oRg.Find
.Forward = True
.Format = True
.Style = ActiveDocument.Styles("Emphasis")
.Text = "^d REF"
Do While .Execute
strCode = oRg.Fields(1).Code.Text
Set oIns = oRg.Duplicate
With oIns
.Collapse wdCollapseEnd
.Text = ", pg "
.Collapse wdCollapseEnd
Set fldPg = ActiveDocument.Fields.Add( _
Range:=oIns, Type:=wdFieldEmpty)
fldPg.Code.Text = " PAGE" & LTrim(strCode)
fldPg.Update
End With
Loop
End With
End Sub



Hi Jay,
I got this installed, and can see it running, but nothing in the
doc changes. (I did change the style line to my style name) I do
pretty well with Visual Basic (I'm an Access developer), so I'm trying
to understand what might be the problem.

I've looked through help on a few things, but I'm having trouble
finding all the references I need to understand the code. I was hoping
if I asked nicely, you could enlighten me on a couple things?

1. In the line 'Set oRg = ActiveDocument.Range' does this select the
whole document as the range? All the references I'm finding in help
show 'Range' used with the some range specified. (FYI, I do want the
whole thing)

2. I believe the '.Text = "^d REF"' statement sets what text to search
for. My best guess is that this is where I'm failing. Can you tell me
what the '^d' does? Does it tell the 'Find' to search in the field
codes?

3. Do I have to see anything else, like '.IncludeFieldCodes = True' or
the like?

I appreciate the help. It's always better if I can "learn to
fish."

Thanks, Max