VB.NET: Problem with Page header in Word Document

B

Balambikai N P

Hi,
I need to create page headers in a word document using VB.NET code.

The page header is different for odd and even pages. There are many odd page
section breaks in the document which splits the document into sections. For
all the sections, the odd page header should contain page number on left side
and the right side should display the name of a topic chosen within that
section with Heading style (1 to 5); this is provided by the user as command
line argument. The even page header should contain page number on right side
and document title on left side.

As the odd page has to automatically resolve the name of the topic with the
specified Heading style, I used ActiveWindow.Selection.Fields.Add() method
with Text parameter as

"STYLEREF ""Heading <n>"" "

<n> is the header level given by user through command line arguments.

Here is the code,

Select Case CType(topicTitleLevel, String)
Case 1
titleHeaderLevel = "STYLEREF ""Heading 1,h1"" "
Case 2
titleHeaderLevel = "STYLEREF ""Heading 2,h2"" "
Case 3
titleHeaderLevel = "STYLEREF ""Heading 3,h3"" "
Case 4
titleHeaderLevel = "STYLEREF ""Heading 4,h4"" "
Case 5
titleHeaderLevel = "STYLEREF ""Heading 5,h5"" "
Case Else
titleHeaderLevel = "STYLEREF ""Heading 2,h2"" "
End Select

Dim headerField As Field
'set the header text
headerField = m_wordDoc.ActiveWindow.Selection.Fields.Add( _
.Range,WdFieldType.wdFieldEmpty, titleHeaderLevel)

This code works fine. But inserts empty text in the header when there isn’t
any topic within the section with Heading <n> style chosen by the user.

Is there any way to modify the code to automatically pick the immediate
higher Heading style (e.g. it should pick Heading 3 style in case if there
isn’t any topic within that section with Heading 4 style) in only those
sections where it doesn’t have topics with required Heading levels?

Any pointers to resolve this would be appreciable.

Thanks in advance,
Bala
 

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