#1
|
|||
|
|||
3 Column x of y Footer
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 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 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 Last edited by macropod; 01-10-2017 at 11:55 AM. Reason: Added code tags |
#2
|
|||
|
|||
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 Code:
ActiveDocument.Fields.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. |
#3
|
||||
|
||||
Quote:
Try: Code:
Sub Demo() Dim Tbl As Table With ActiveDocument Set Tbl = .Tables.Add(Range:=.Sections(1).Footers(wdHeaderFooterPrimary).Range, NumRows:=1, NumColumns:=3) With Tbl .Cell(1, 1).Range.Text = "QMF 24 Rev D" With .Cell(1, 2).Range .Text = "Page of " .Fields.Add .Characters(6), wdFieldPage .Fields.Add .Characters.Last.Previous, wdFieldNumPages End With .Cell(1, 3).Range.Text = "PRM 05/Section 4.6" End With End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
||||
|
||||
That line isn't needed as the PAGE & NUMPAGES fields update automatically is headers & footers.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Code:
.Fields.Add .Characters(6), wdFieldPage Another step up the learning curve courtesy of Macropod. Cheers. Steve |
#6
|
||||
|
||||
Note that the designated character gets wiped out, which is why
Code:
"Page of "
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Thank you slaycock for the primer on the Collapse method. I get it now.
Thank you macropod for your code. It is elegant and works perfectly. Question, though... My code produces a document made of only tables. The tables are dynamically produced and referred by their Index using an incremented variable. I noticed the code for the Footer must be put at the end of the document. Is this true for all cases? Thanks again to both for everything. -John |
#8
|
||||
|
||||
Where the footer 'goes' depends on your document structure. If you have a document with a 'different first page' and/or a 'different odd and even' page layout, you'd have to choose whether to put the footer table into the 'first page' footer, 'even pages' footer, and/or the 'primary' footer. Moreover, if your document has multiple Section breaks, you might need to do that process for more than one Section, depending on whether the footer in a given Section is linked to a previous one. For a plain document, though, the footer just goes in the page footer; there's no such thing as a 'start' or 'end' of the document for a footer - it replicates on all pages.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
Thank you for the explanation, macropod.
My problem was if I inserted the Footer code at the beginning of a document, as soon as I attempted to insert a table, it broke. Moving it to the end of the code worked. It was a coding issue I brought upon myself. -John |
#10
|
||||
|
||||
I seriously doubt that putting the footer 'at the beginning of the document' had anything to do with your problem; more likely it was how you tried to integrate that code with whatever other code you're using that caused the problem.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
Yes, Paul. It was a "John" issue!
Thanks for your help. -J |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Header and footer aligned in the footer area | ashiqghfr | Word | 2 | 07-23-2015 01:14 AM |
documents saved with double column revert to single column when re-opened | danw | Word | 6 | 04-08-2015 06:24 AM |
How can I temporarily break a 3 column format in order to type a single column paragraph | William P | Word | 1 | 01-04-2015 06:40 PM |
Footer - more then one footer (auto fill text in slides) | noodle | PowerPoint | 0 | 03-31-2013 04:50 AM |
Two column format without affecting neither the page numbers nor the header or footer | Jamal NUMAN | Word | 4 | 02-21-2012 03:20 PM |