Macro to Write Chapter Title into Primary Header 
Author Message
 Macro to Write Chapter Title into Primary Header

I'm on a project to automate header and footer updates in my company's
documents.

The documents are books, split into chapters, one file per chapter. The
Even Page Headers contain a tab, the book title, another tab and the
Page Number. The Primary Headers contain the same tabs and Page Numbers,
but in the place of the book title there is the title of the current
chapter.

So far, I have managed to concoct a macro that finds the first paragraph
in the file that conforms to the Chapter Title style (called
"TitreModule"), reads its text into a variable, then writes this text
back into the header, between the tabs. It looks like this:

****
Sub TestHeader()

Dim para As Paragraph
Dim ChapterTitle As String

Selection.HomeKey

For Each para In ActiveDocument.Paragraphs
If para.Style = "TitreModule" Then
   ChapterTitle = Left(para.Range.Text, para.Range.Characters.Count - 1)

   Exit For
   End If
Next para

      ActiveWindow.ActivePane.View.SeekView = wdSeekPrimaryHeader
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "^t*^t"
        .Replacement.Text = "^t" & ChapterTitle & "^t"
        .MatchWildcards = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

End Sub

****

This works OK, but has an important failing. The "TitreModule" style
automatically affects a chapter number to each chapter, along the lines
of:

Chapter 2. Installation and Configuration

The macro, as currently devised, only retrieves the latter part of this
text, "Installation and Configuration", and leaves out the automatic
part, "Chapter 2.". I would like it to include this.

I tried using the line

ChapterTitle = Left(para.Range.FormattedText,
para.Range.Characters.Count - 1)

in the place of

 ChapterTitle = Left(para.Range.Text, para.Range.Characters.Count - 1)

hoping that the FormattedText would retain the number, but it didn't.

Another idea I have had is to call up the dialog box where the automatic
part is configured and retrieve the text into another variable from
there. Then I could just concatenate it with the other stuff as needed.
But I do not know the name of the dialogbox that has these details. It
is the box accessed via the menu

Format >
Style >
"TitreModule" Change >
Format >
Numbering >
Customize.

(I'm not sure how it reads in English - this is on a French version of
Windows)

Does anyone know how to call up that box or have a better idea?

Many thanks
--
Elisa Francesca Roselli
Technical Writer
Quadratec Software - France



Tue, 16 Dec 2003 23:20:45 GMT  
 Macro to Write Chapter Title into Primary Header
The STYLEREF field used in the header will probably do all you want without
the need for any VBA programming.

--

<>>< ><<> ><<> <>>< ><<> <>>< <>>< ><<>

<>>< ><<> ><<> <>>< ><<> <>>< <>>< ><<>



Quote:
> I'm on a project to automate header and footer updates in my company's
> documents.

> The documents are books, split into chapters, one file per chapter. The
> Even Page Headers contain a tab, the book title, another tab and the
> Page Number. The Primary Headers contain the same tabs and Page Numbers,
> but in the place of the book title there is the title of the current
> chapter.

> So far, I have managed to concoct a macro that finds the first paragraph
> in the file that conforms to the Chapter Title style (called
> "TitreModule"), reads its text into a variable, then writes this text
> back into the header, between the tabs. It looks like this:

> ****
> Sub TestHeader()

> Dim para As Paragraph
> Dim ChapterTitle As String

> Selection.HomeKey

> For Each para In ActiveDocument.Paragraphs
> If para.Style = "TitreModule" Then
>    ChapterTitle = Left(para.Range.Text, para.Range.Characters.Count - 1)

>    Exit For
>    End If
> Next para

>       ActiveWindow.ActivePane.View.SeekView = wdSeekPrimaryHeader
>     Selection.Find.ClearFormatting
>     Selection.Find.Replacement.ClearFormatting
>     With Selection.Find
>         .Text = "^t*^t"
>         .Replacement.Text = "^t" & ChapterTitle & "^t"
>         .MatchWildcards = True
>     End With
>     Selection.Find.Execute Replace:=wdReplaceAll

> End Sub

> ****

> This works OK, but has an important failing. The "TitreModule" style
> automatically affects a chapter number to each chapter, along the lines
> of:

> Chapter 2. Installation and Configuration

> The macro, as currently devised, only retrieves the latter part of this
> text, "Installation and Configuration", and leaves out the automatic
> part, "Chapter 2.". I would like it to include this.

> I tried using the line

> ChapterTitle = Left(para.Range.FormattedText,
> para.Range.Characters.Count - 1)

> in the place of

>  ChapterTitle = Left(para.Range.Text, para.Range.Characters.Count - 1)

> hoping that the FormattedText would retain the number, but it didn't.

> Another idea I have had is to call up the dialog box where the automatic
> part is configured and retrieve the text into another variable from
> there. Then I could just concatenate it with the other stuff as needed.
> But I do not know the name of the dialogbox that has these details. It
> is the box accessed via the menu

> Format >
> Style >
> "TitreModule" Change >
> Format >
> Numbering >
> Customize.

> (I'm not sure how it reads in English - this is on a French version of
> Windows)

> Does anyone know how to call up that box or have a better idea?

> Many thanks
> --
> Elisa Francesca Roselli
> Technical Writer
> Quadratec Software - France



Wed, 17 Dec 2003 00:05:07 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. The different between FirstPage header and Primary header

2. Chapter Number/Title Associated with Search Question

3. Writing a macro to create another macro?

4. TITLE field won't update in header table

5. Title of document in the header of print

6. How to set column header title in multicolumn listbox

7. Write SQL Query for a multiple fields primary key

8. Joined Primary Key in Foxpro (Make 1 Primary Key from 2 fields)

9. Hoe to use 2 Primary Tables in a Primary/Secondary Data Form

10. 2 Primary Tables in a Primary/Secondary Data Form

11. Hoe to use 2 Primary Tables in a Primary/Secondary Data Form

12. Formatting three different footers and headers with a macro in Office XP

 

 
Powered by phpBB® Forum Software