Thread: [Solved] 3 Column x of y Footer
View Single Post
 
Old 01-10-2017, 10:27 AM
slaycock slaycock is offline Windows 7 64bit Office 2013
Expert
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default

Hi John

Your first block of code works fine on my PC.

Code:
Sub test()
Dim myRange As Range
Dim myTable As Table
   Set myRange = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
   Set myTable = ActiveDocument.Tables.Add(ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range, 1, 3)
   With myTable
      .Cell(1, 1).Range.Text = "QMF 24 Rev D"
      .Cell(1, 1).Range.Paragraphs(1).Alignment = wdAlignParagraphCenter
   End With
   Set myRange = myTable.Cell(1, 2).Range
   myRange.End = myRange.End - 1
   myRange.Collapse wdCollapseEnd
   myTable.Cell(1, 2).Select
   With Selection
      .Paragraphs(1).Alignment = wdAlignParagraphCenter
      .TypeText Text:="Page "
      .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="PAGE ", PreserveFormatting:=True
      .TypeText Text:=" of "
      .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="NUMPAGES ", PreserveFormatting:=True
   End With
   With myTable
      .Cell(1, 3).Range.Text = "PRM 05/Section 4.6"
      .Cell(1, 3).Range.Paragraphs(1).Alignment = wdAlignParagraphCenter
   End With
End Sub
It may be that you need to add

Code:
ActiveDocument.Fields.Update
as the last line to force your fields to update.

The collapse method makes the start and end of the range the same point. If you were to do this on a word you had selected you would see the range contract to just an insertion point. The wdCollapseStart and wdCollapseEnd parameters dictate the direction of the collapse.

So if you had a word selected then the use of wdCollapseStart would move the end of the range to the start of the range and give you an insertion point at the start of the word. Conversely, the use of wdCollapseEnd moves the start of the range to the end of the range and so puts the insertion point at the end of the range.

I'd agree with you that the word help files can be a bit hard to get your head around at times so you have my sympathies there.

I'm not sure but if you want your footer text to be left aligned in Cell(1,1), centered in Cell(1,2) and right aligned in Cell(1,3) then use

wdAlignParagraphLeft
wdAlignParagraphCentered
wdAlignParagraphRight

If you are new to using word then in the VBA IDE goto Tools.Options and make sure that all boxes under 'Code Settings' are ticked.
Reply With Quote