Hi Folks,
I am having a time with this and have not been able to obtain a solution. I am fluent in VBA (Access), but Word is another animal...
From LabVIEW, I am opening an instance of Word, inserting a VBA module, and calling that module. From Word, the module then formats the document, populates it with test data passed by LabVIEW, prints it as a report for the customer, and closes the Word instance. The report must be created by VBA (no Macro) and insert a Footer on each page. The Footer requires three columns with the center column showing 'Page x of y' to look like:
QMF 24 Rev D ---------- Page 1 of 2---------- PRM 05/ Section 4.6
I tried inserting a 3 column table inside the Footer using code I found:
Code:
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
But, this code does not show the current page number (Page 1 of y shows y of y). I am presuming PAGE and NUMPAGES are reserved words and return the page numbers, but are meant for Macros???? In any event, I am lost as to how the numbering takes place.
I tried this code I found (without table):
Code:
With .Footers(wdHeaderFooterPrimary)
Set rng = .Range.Duplicate
rng.Text = "QMF 24 Rev D"
rng.Collapse wdCollapseEnd
rng.InsertBefore vbTab & "Page of "
rng.Collapse wdCollapseStart
rng.Move wdCharacter, 6
ThisDocument.Fields.Add rng, wdFieldPage
Set rng = .Range.Duplicate
rng.Collapse wdCollapseEnd
ThisDocument.Fields.Add rng, wdFieldNumPages
rng.Collapse wdCollapseEnd
End With
It works very well, but I have no idea how to insert the third column text and right-justify so all text are evenly spaced. Further, this code is even more a mystery to me. I do not understand the concept of the Collapse method. My research tells me it has no effect on the document unless it is applied to an object using the SET keyword. Then there is the Duplicate Method...
1) Can anyone help me make either code snippet work as I need it?
2) Microsoft's online Help is so very vague explaining their Objects, Properties, and Methods. Can I be enlightened about Word's Object model regarding this, please?
Thanks,
-John