A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Word » Formatting Long Documents
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Changing style of heading cross-references



 
 
Thread Tools Display Modes
  #1  
Old May 1st, 2005, 08:40 AM
Max Moor
external usenet poster
 
Posts: n/a
Default Changing style of heading cross-references

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
  #2  
Old May 1st, 2005, 05:32 PM
Jay Freedman
external usenet poster
 
Posts: n/a
Default

On Sun, 01 May 2005 00:40:25 -0700, Max Moor
wrote:

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

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
  #3  
Old May 2nd, 2005, 07:55 PM
Max Moor
external usenet poster
 
Posts: n/a
Default

Jay Freedman wrote in
:

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!
  #4  
Old May 9th, 2005, 04:12 AM
Max Moor
external usenet poster
 
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







  #5  
Old May 9th, 2005, 06:22 AM
Max Moor
external usenet poster
 
Posts: n/a
Default

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
  #6  
Old May 10th, 2005, 03:22 AM
Jay Freedman
external usenet poster
 
Posts: n/a
Default

On Sun, 08 May 2005 20:12:20 -0700, Max Moor
wrote:

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


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.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
  #7  
Old May 10th, 2005, 03:33 AM
Jay Freedman
external usenet poster
 
Posts: n/a
Default

On Sun, 08 May 2005 22:22:40 -0700, Max Moor
wrote:

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/...2dcdcff8?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."

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
My custom styles are acquiring unwanted attributes Frustrated Frame User General Discussion 10 April 21st, 2005 09:38 AM
Tab character in table of contents when using heading styles Rob Nicholson Page Layout 4 December 13th, 2004 10:26 AM
Chapter Headings "continued" db Formatting Long Documents 2 November 5th, 2004 09:34 PM
Losing numbers on pre-formatted styles JulieG General Discussion 5 September 1st, 2004 04:38 PM
Inconsistent style application in cross references Lynn Formatting Long Documents 8 July 3rd, 2004 10:19 PM


All times are GMT +1. The time now is 07:28 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.