lapsang
lapsang

Reputation: 11

Using VBA to Set Multiple Styles in the Footer in Word

I am new to VBA in Word with some experience in Excel. I am trying to produce a Word document from an Excel file. The first task is to set up the headers and footers, which I am struggling with. For context, I have added the reference to Word in Excel and will likely convert to late-binding at a later date because this is a tool I will distribute to peers. The goal of this macro is to generate a document with data that matches a report format, so the formatting is not my choice; I have to match it as the Word template is set up. At this point I am not using late binding so that I can use Intellisense while I learn this.

Requirements for the footer:

  1. Centered text, Arial, size 8: "Page " and then an automatically generated page number.
  2. Right-aligned text, Arial, size 8, bold: "Other Support Page"
  3. A top line border for the entire footer.

What I want:

screenshot

I can get most of this to function except it's either entirely bold or entirely not bold. I have looked into using "Collapse 0", however, it screws up the top border. Also, I have tried to use style objects to lower the amount of code, but it then wipes out the default tab stops. I am struggling to add the tab stops back into the footer (center 3.25" and right 6.5"). I have no problem adding tab stops in the body, but for some reason the code executes but does nothing with the tab stops when I try and put them in the footer. First try here has it set up correctly, but bolds the entire thing:

Everything bold

With rngFooter

.Font.Name = "Arial"
.Font.Size = "8"

.Fields.Add rngFooter, wdFieldPage, , False
.InsertBefore vbTab & "Page "

.Font.Bold = True
.InsertAfter vbTab & "Other Support Page"

With rngFooter.Borders(wdBorderTop)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth075pt
    .Color = Options.DefaultBorderColor
End With
End With

I have read about moving styles until after the text you want to format. So if I were to use the styles I have created, the tab stops get wiped out and the formatting isn't right anyways (the "b" in the style name means it is set to bold):

No tab stops or formatting

With rngFooter

.Fields.Add rngFooter, wdFieldPage, , False
.InsertBefore vbTab & "Page "
.Style = A8

.InsertAfter vbTab & "Other Support Page"
.Style = AB8

With rngFooter.Borders(wdBorderTop)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth075pt
    .Color = Options.DefaultBorderColor
End With
End With 

If I add the collapse in, it screws up the borders.

Collapse with screwed up borders with everything bold

With rngFooter

.Fields.Add rngFooter, wdFieldPage, , False
.InsertBefore vbTab & "Page "
.Style = A8
.Collapse 0

.InsertAfter vbTab & "Other Support Page"
.Style = AB8

With rngFooter.Borders(wdBorderTop)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth075pt
    .Color = Options.DefaultBorderColor
End With
End With 

I really want to understand how to do this properly because I will also be trying to change styles mid-way through table cells. I find the documentation on how ranges work to be confusing, but I do understand that the point of the collapse is to prevent it from overwriting the entire footer, which is what I was doing before. I just can't see how I can do the collapse and then also apply the top-line border to the whole footer. I have to put it in at the end also or it interferes with the page number.

Upvotes: 0

Views: 934

Answers (2)

lapsang
lapsang

Reputation: 11

Thank you Timothy Rylatt for the pointers to alignment tabs and character styling. I was able to avoid tables and generating a template file (which would be a lot more work as I need to distribute this Excel file to many users). My solution is as follows:

With rngFooter
.Style = A8
.InsertAlignmentTab 1, 0
.InsertAlignmentTab 2, 0
.Fields.Add rngFooter, wdFieldPage, , False
.InsertBefore vbTab & "Page "
.InsertAfter vbTab & "Other Support Page"
End With

' Make "Other Support Page" bold
With rngFooter.Find
.ClearFormatting
.Text = "Other Support Page"
.Replacement.ClearFormatting
.Replacement.Style = AB8
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceOne
End With

'Add border to entire footer
With rngFooter
     .Expand Unit:=wdParagraph
With .Borders(wdBorderTop)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth075pt
    .Color = Options.DefaultBorderColor
End With
End With

Essentially I applied the base style (now a character style and not a paragraph style), then inserted the alignment tabs as the base tab stops from the Normal template were wiped out. I then add the page number, then the "Page " text, then the "Other Support Page" text. I do a find and replace on the specific expression to format, and apply a character style to ensure it doesn't expand the formatting to the full paragraph. The border issue is fixed by using .Expand on the range prior to applying the border. Order of operations was very important to making this work.

For me, the documentation on the Word object model is more confusing than Excel is, and I appreciate the specific topics to research. I also used this StackOverflow answer for the tip on using find and replace to change the styles, which worked once they were converted to Character Styles.

Upvotes: 1

freeflow
freeflow

Reputation: 4355

Do not use tabs for alignment because they are part of the paragraph and a paragraph can only have one style without some trickery.

By far the easiest way to get what you want is to follow this procedure which can be applied to either headers or footers

  1. Insert a 1 row, 3 column table to get left, center and righgt 'fields'
  2. Turn off the table borders
  3. Insert the relevant text into each cell
  4. Set the formatting of paragraph(1) of each cell
  5. If required, turn on the bottom border of the Header Table and or the Top border of the Footer table.
  6. If stuck for vertical space you might need to set the font hieght of the compulsory row after each table to 1 or 2 points.

Be aware that each section in the document has its own Headers and Footers and that there are three headers and three footers in each section.

Upvotes: 0

Related Questions