Changing style of heading cross-references

M

Max Moor

Hi All,
I have a document I've written (160+ pages) that has lots of cross-
references in it to various headings in other parts of the document.
Statements like "see Boiling Rubber Chickens," where Boiling Rubber
Chickens is a heading elsewhere.

I've just about done the whole thing, and have started wishing I'd have
done cross-references like, "see Boiling Rubber Chickens, pg 63," where an
actual page number was included (so the reader doesn't have to refer to the
TOC or index everytime they want to look up the reference.

Throughout the document, I've formatted cross-references with a
particular style, so I have the ability to select all of them at once. I'm
just not sure what to do to change them to the other format once they are
selected. Is there a way?

- Max
 
J

Jay Freedman

Hi All,
I have a document I've written (160+ pages) that has lots of cross-
references in it to various headings in other parts of the document.
Statements like "see Boiling Rubber Chickens," where Boiling Rubber
Chickens is a heading elsewhere.

I've just about done the whole thing, and have started wishing I'd have
done cross-references like, "see Boiling Rubber Chickens, pg 63," where an
actual page number was included (so the reader doesn't have to refer to the
TOC or index everytime they want to look up the reference.

Throughout the document, I've formatted cross-references with a
particular style, so I have the ability to select all of them at once. I'm
just not sure what to do to change them to the other format once they are
selected. Is there a way?

- Max

Hi Max,

Unfortunately, it isn't quite so simple as changing the style, because
styles affect only formatting and not text. However, it can be done
with a macro. The idea is to find each cross-reference that has the
specific style, and add after it the text ", pg " followed by a
PAGEREF field pointing to the same heading.

Use the instructions at http://www.gmayor.com/installing_macro.htm to
put this macro into either Normal.dot or the template your document is
based on, if that's diferent. In the .Style line, change the item in
quotes to the name of the style you used on your cross-references. Run
the macro only once on the document -- if you run it a second time on
the same document, it will add a second ", pg xx" to each reference.

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
 
M

Max Moor

Unfortunately, it isn't quite so simple as changing the style, because
styles affect only formatting and not text. However, it can be done
with a macro. The idea is to find each cross-reference that has the
specific style, and add after it the text ", pg " followed by a
PAGEREF field pointing to the same heading.


Awesome, Jay. I'll give it a whirl. Thanks!
 
M

Max Moor

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
 
M

Max Moor

Hi Again Jay,

Sorry to be a problem child, but I found my answers with some more
hunting, and got the macro to work.

The style I've been setting things to is called "Cross-reference," so
that is what I set the style line in your macro to.

It finally occurred to me that I was just doing in code what the Word
Find box did in the app, so I started experimenting with that. I found out
that, indeed, "^d" says to search fields.

When I went to add my style criteria to the search, I found that there
was a style called "Cross-reference Char" in the list. Guess what? That one
works just fine. It doesn't find a single one if I use "Cross-reference."
It must be a paragraph/char style thing that I need to understand better.

I suppose I still have a lot to learn, but I'm smarter than I was. The
coolest thing is that all my references now have page number next to them,
and I hardly had to do a thing.


Thanks for the help!

Max
 
J

Jay Freedman

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

Hi Max,

1. Yes, that line initializes the variable oRg to be equal to the
range of the whole document (specifically, the mani body of the
document, not including headers, footers, text boxes,
footnotes/endnotes, or any of the other "stories" in the document).

2. The code ^d matches the field brace. If you're using the Find
dialog, click More and then Special, and select "Field"; it will
insert ^d in the Find What box. You also need to know that when the
Find matches the left field brace and the REF, oRg will automatically
include the entire field code and both braces.

3. The line oRg.TextRetrievalMode.IncludeFieldCodes = True tells VBA
to do the search as if field codes are displayed, even if they aren't.
This means the search string "^d REF" should match all REF fields.

To find out what's happening, open the macro in the VBA editor and
press F8 to start single-stepping through the code (that is, executing
one command at a time and pausing). A yellow highlight will show the
line that's about to be executed. You can hover the mouse pointer over
the name of a variable such as oRg to see what its current value is,
or look at it in the Watch window or the Locals window.

If the execution goes directly from the line 'Do While .Execute' to
the line 'End With', then the search isn't finding any fields. The
most likely reason for that is that the style name is wrong in the
line '.Style = ActiveDocument.Styles("Emphasis")' after you changed
the style name. Maybe you added or left out a space, or misspelled it.
 
J

Jay Freedman

Hi Again Jay,

Sorry to be a problem child, but I found my answers with some more
hunting, and got the macro to work.

The style I've been setting things to is called "Cross-reference," so
that is what I set the style line in your macro to.

It finally occurred to me that I was just doing in code what the Word
Find box did in the app, so I started experimenting with that. I found out
that, indeed, "^d" says to search fields.

When I went to add my style criteria to the search, I found that there
was a style called "Cross-reference Char" in the list. Guess what? That one
works just fine. It doesn't find a single one if I use "Cross-reference."
It must be a paragraph/char style thing that I need to understand better.

I suppose I still have a lot to learn, but I'm smarter than I was. The
coolest thing is that all my references now have page number next to them,
and I hardly had to do a thing.


Thanks for the help!

Max

Hi Max,

The "Char" style problem is something else -- we generally consider it
a bug. One of many explanations in the newsgroups is Shauna Kelly's:
http://groups-beta.google.com/group...ormatting.longdocs/msg/1c4881a82dcdcff8?hl=en

"In Word 2002 and 2003, "Char" styles are created when you select part
(but not all) of a paragraph and apply a (paragraph) style to that
part."
 

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