Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word - multiple fonts & fields in Header

Status
Not open for further replies.

peejaykay

Programmer
Jan 10, 2007
12
US
I'm attempting to build a header with multiple font sizes and several fields. I'm using Word.Range, to set the specific header parameters. Using the range object makes formatting changes to the entire Header (the entire range), and inserted fields appear at the beginning of the header instead of where I want the insertion points. Is there a way to accomplish this with the range object, should I be using another Word object, or am I forced to open the Header/Footer view and use Selection?

Thank you
pjk
 
Have you tried to play with the Characters collection ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
No you do not have to use the Selection; indeed it is better not to. Using Ranges is the way to do it but you must specify what the Range is - alternatively you may be able to preformat and use an AutoText. You don't say much about how you are creating the text and fields so it's difficult to advise on the way forward. Can you post your code - or enough of it to show how you are building the header.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
This is the beginning code to establish the header:

Code:
    Dim i As Integer, wpNum$, oHf As HeaderFooter, r As Word.Range
    '-------------------------
    'Clear out header / footer
    '-------------------------
    i = W.ActiveDocument.Sections.count
    For Each oHf In W.ActiveDocument.Sections(i).Headers
        oHf.LinkToPrevious = False
        oHf.Range.Delete
    Next
    For Each oHf In W.ActiveDocument.Sections(i).Footers
        oHf.LinkToPrevious = False
        oHf.Range.Delete
    Next
    W.ActiveDocument.Sections(i). _
        PageSetup.DifferentFirstPageHeaderFooter = True
        
    '--------------------------
    'Establish Header tab stops
    '--------------------------
    Set r = W.ActiveDocument.Sections(i).Headers(wdHeaderFooterFirstPage).Range
    r.ParagraphFormat.TabStops.ClearAll
    r.ParagraphFormat.TabStops.Add position:=InchesToPoints(3.375), _
        Alignment:=wdAlignTabCenter, Leader:=wdTabLeaderSpaces
    r.ParagraphFormat.TabStops.Add position:=InchesToPoints(6.75), _
        Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces

This works using selection, but haven't been able to duplicate it without using the View / Selection method: (later headers also include formulas)

Code:
    i = W.Selection.Information(wdActiveEndSectionNumber)
    W.Selection.Goto What:=wdGoToSection, Which:=wdGoToFirst, count:=i, name:=""
    W.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    
    '--------------------------------------------
    'Select large font for document name
    '--------------------------------------------
    W.Selection.Font.name = "Arial"
    W.Selection.Font.Size = 18
    Fmt.TextBoldOn
    '-------------------------------------
    'Establish WP number from passed index
    '-------------------------------------
    wpNum$ = frmMain.txtWPNum(wpIdx)
    '--------------------------------------------
    'Write the program info to first tab position
    'Include the Work Package string, no date
    '--------------------------------------------
    If Left$(frmMain.txtTPS.Text, 3) = "TPI" Then
        W.Selection.TypeText Text:=frmMain.txtTPS.Text
    Else
        W.Selection.TypeText Text:="TPI " & frmMain.txtTPS.Text
    End If
    W.Selection.Font.name = "Arial"
    W.Selection.Font.Size = 14
    W.Selection.TypeText Text:=vbTab & vbTab & _
        "WP " & wpNum$ & " 0" & Trim$(Str$(subWp)) & vbCrLf$
    W.Selection.TypeText Text:=vbTab & vbTab & "Page "
    W.Selection.Fields.Add Range:=W.Selection.Range, Type:=wdFieldPage
    W.Selection.TypeText Text:=" of "
    '--------------------------------------------
    'Add the total number of section pages
    '--------------------------------------------
    If wpIdx = 7 Then
        W.Selection.TypeText Text:="TPLPAGES"
        W.Selection.TypeText Text:=vbCrLf$ & vbCrLf$
    Else
        W.Selection.Fields.Add Range:=W.Selection.Range, Type:=wdFieldEmpty, _
            Text:="SECTIONPAGES ", PreserveFormatting:=True
        W.Selection.TypeText Text:=vbCrLf$
    End If
    W.Selection.Font.Size = 16          'to get the 1.5" margin
    W.Selection.TypeText Text:=vbCrLf$
    With W.Selection.ParagraphFormat
        .SpaceBefore = 0
        .SpaceBeforeAuto = False
        .SpaceAfter = 12
        .SpaceAfterAuto = False
    End With
    Fmt.BorderBottom    'put border after 2nd line of header
    With W.Selection.HeaderFooter.PageNumbers
        .NumberStyle = wdPageNumberStyleArabic
        .HeadingLevelForChapter = 0
        .IncludeChapterNumber = False
        .ChapterPageSeparator = wdSeparatorHyphen
        .RestartNumberingAtSection = True
        .StartingNumber = 1
    End With
    Fmt.TextBoldOff    ' turn off bold
 
This would be so much easier if you used paragraph styles for the paragraphs, and character styles for the characters you want differently.

Or, as Tony suggested, using Autotext.

Using the range object makes formatting changes to the entire Header (the entire range)
This is not correct. Formatting is actioned on the Range, and you do NOT have to maintain the range as the entire header.

You can use the Range object as the entire header to action the entire header, but then resize the range object to be whatever part of the header you want. You can use InsertBefore to put things before the Range, InsertAfter to put things after. The range object can be whatever you want it to be.

Gerry
My paintings and sculpture
 
Are you creating a complete document via code - or just adding the header? I'm presuming the latter, but just want to confirm. And will anything be added to the header later, or is what you are doing complete? And, final question, why are you doing this? That sounds a bit rude but it may make a difference if you are, say, making a (one-off) global change to lots of documents or perhaps processing documents held in some sort of document library package.

Scanning the code it looks like you want three elements in your header: TPIxxxxxxx at the left, WPxxxxxx in the middle, and PAGE of SECTIONPAGES at the right. All in Arial Bold, 18pt on the left and 14pt in the middle and on the right, and all with a bottom border. I think I'd create a table rather than use tabs.





Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
I have created a VB application that reads source code used to program Automatic Test Equipment, plus a database describing the interfacing. The application then creates another database which details the test and path information. Eventually, the application creates a Word document containing this and other information. The Word file is converted to PDF outside of the application.

The average Word document created is between 1500 and 3000 pages with around 30 to 40 sections. The formatting requirements are fairly rigid and much of the data is in a tabular format (but not in tables). There is extensive cross-linking between sections, plus shading and text highlighting. I use a custom template to pre-define the styles used in the document.

As you can tell from my code samples, all of the Word VBA code was developed from recorded macros. All of the code worked except for when I switched from headers with a different 1st page to without. Formatting and information in the previous headers got screwed up. This was subject of a post last week that fumei answered for me. His response demonstrated that I should probably rewrite all of my VBA code. I don't know how much time I will be allowed to use for that purpose, but it would be great if the process ran quicker and used less resources.

I guess I don't know the difference between paragraph and character styles, and how the range element is manipulated to isolate the different parts of the header to apply specific formatting to each.

I have to produce headers (for the source code listings) that include the section page numbers plus the individual source code pages and total number of pages in each source code file. I use a formula to accomplish, but there's probably a better way. (not the sample code provided)

The sample code I provided creates a header with:
TPIxxxxxx at left in 18 point bold Arial
WP xxx xx xx at right margin in 14 point bold Arial
Page x of sectionpages at right below WP (same font)
and a 16 point blank line with a bottom border
This positions the bottom border at 1.5" from top of page, there's probably a better way to do that also.

There usually isn't any editing required once the document is created.

Would you avoid using selection within the main body of the document also? Thank you in advance... pjk
 
If all your headers are the same format - which they seem to be - why do you not do all the formatting in the template on which you base the document? All you would then need to do in code would be to fill in the TPI and WP numbers/codes which would be much simpler than doing the whole thing.

And, yes, avoiding the Selection everywhere is generally best - and has better performance.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top