Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 01-10-2017, 06:52 AM
jmltinc jmltinc is offline Windows 7 64bit Office 2016
Novice
 
Join Date: Jan 2017
Posts: 9
jmltinc is on a distinguished road
Default 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
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

Last edited by macropod; 01-10-2017 at 11:55 AM. Reason: Added code tags
Reply With Quote
  #2  
Old 01-10-2017, 10:27 AM
slaycock slaycock is offline Windows 7 64bit Office 2013
Advanced Beginner
 
Join Date: Sep 2013
Posts: 64
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
  #3  
Old 01-10-2017, 12:11 PM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 15,395
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Quote:
Originally Posted by jmltinc View Post
The report must be created by VBA (no Macro) and insert a Footer on each page.
Macros and VBA are the same thing...

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
[MS MVP - Word]
Reply With Quote
  #4  
Old 01-10-2017, 12:14 PM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 15,395
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Quote:
Originally Posted by slaycock View Post
It may be that you need to add

Code:
ActiveDocument.Fields.Update
as the last line to force your fields to update.
That line isn't needed as the PAGE & NUMPAGES fields update automatically is headers & footers.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #5  
Old 01-10-2017, 12:50 PM
slaycock slaycock is offline Windows 7 64bit Office 2013
Advanced Beginner
 
Join Date: Sep 2013
Posts: 64
slaycock is on a distinguished road
Default

Code:
.Fields.Add .Characters(6), wdFieldPage
Good grief!! Its all so obvious when you know how. I'd never have thought to use characters as a source for a range.

Another step up the learning curve courtesy of Macropod. Cheers.

Steve
Reply With Quote
  #6  
Old 01-10-2017, 02:13 PM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 15,395
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Note that the designated character gets wiped out, which is why
Code:
"Page   of  "
has extra characters.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #7  
Old 01-11-2017, 05:14 AM
jmltinc jmltinc is offline Windows 7 64bit Office 2016
Novice
 
Join Date: Jan 2017
Posts: 9
jmltinc is on a distinguished road
Default

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
Reply With Quote
  #8  
Old 01-11-2017, 05:50 PM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 15,395
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
  #9  
Old 01-12-2017, 03:53 AM
jmltinc jmltinc is offline Windows 7 64bit Office 2016
Novice
 
Join Date: Jan 2017
Posts: 9
jmltinc is on a distinguished road
Default

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
Reply With Quote
  #10  
Old 01-12-2017, 04:56 AM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 15,395
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
  #11  
Old 01-12-2017, 06:15 AM
jmltinc jmltinc is offline Windows 7 64bit Office 2016
Novice
 
Join Date: Jan 2017
Posts: 9
jmltinc is on a distinguished road
Default

Yes, Paul. It was a "John" issue!

Thanks for your help.

-J
Reply With Quote
Reply
Please reply to this thread with any new information or opinions.

Thread Tools
Display Modes


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


All times are GMT -7. The time now is 04:40 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft