Solved

VBScript: Adding footer to Word doc

Posted on 2014-01-22
8
2,525 Views
Last Modified: 2014-01-24
Hello experts,

I am new to VbScript, so please bare with me.  I am trying to add a footer to my existing word document.  I cant seem to get the title and page number in the footer placed properly, because it wont create a new row, My ouput currently shows both the tittle and page number overlaping one another on the same line.

Footer should look like:

Week 3 - 1/22/2014
# <-- page number

Here is the code I am using:

Option Explicit

Dim objWord
Dim objDoc
Dim objSection
Dim objSelection
Dim objPageNumbers


Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("C:\Documents and Settings\XXX.docx")
Set objSection = objDoc.Sections(1)
Set objSelection = objWord.Selection
Set objPageNumbers = objSection.Footers(1).PageNumbers
	
Const wdPageBreak = 7
Const wdPageNumberStyleUppercaseRoman = 1


HeaderFooter


Sub HeaderFooter()
On Error Resume Next


Const wdAlignPageNumberCenter = 1
Dim strNow, strDD, strMM, strYYYY, strFulldate
strYYYY = DatePart("yyyy",Now())


objPageNumbers.ShowFirstPageNumber = FALSE
objPageNumbers.RestartNumberingAtSection = TRUE
objDoc.Sections(1).Footers(1).Range.ParagraphFormat.Alignment = 1
objDoc.Sections(1).Footers(1).Range.Text = "Week "& DatePart("ww", Now())-1 &" - " & Date
objPageNumbers.StartingNumber = 1
objPageNumbers.Selection,TypeParagraph
objPageNumbers.Add(1)


End Sub

Open in new window

I have attached a test file.
TEST-DOC.docx

Thank you for your help
0
Comment
Question by:Maliki Hassani
  • 4
  • 4
8 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39800998
Hi,
I'm not a naturist, so I don't really want to 'bare' with you. Sorry.

However, I will bear with you and your relative inexperience.

Firstly, I have to ask you, do you really need to use VBScript? You should be able to use VBA, whose syntax is nearly identical, but whose debugging facilities are so much better.

With that in mind, I have been converting your script to VBA macros, which is probably how it started out anyway. Unexpectedly (to me), the line:
objPageNumbers.ShowFirstPageNumber = FALSE
also sets the section to have 'Different First Page' Headers and Footers.

Therefore it is not clear to me whether you are working on the first page only, subsequent pages only, or all pages in the section.

Can you clarify your objective in this respect, please?
0
 

Author Comment

by:Maliki Hassani
ID: 39801247
I am looking to have it coded in vbscript because it is being formated after a automation produced the file.  Which is where a script will be executed to format the document.  Lastly, the automation tool will be emailing the file.

The commands that you listed are appropriate since i dont want to have a header and footet on the 1st page, only after.

Thanks
0
 

Author Comment

by:Maliki Hassani
ID: 39801433
Thanks for the correction too.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39803724
Sorry, I thought that I had posted this query many hours ago.

Line 32: objPageNumbers.ShowFirstPageNumber = FALSE
Sets the 'Different first page' option for the section. This means that we are now dealing with the first page footer and other (primary) pages footer. Do you only need the footer in the primary footer?
0
 

Author Comment

by:Maliki Hassani
ID: 39803744
That is correct only need the footer in the primary footer.  Thanks
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 39804976
The problem is that a PageNumber object seems to be a Page field in a textbox, but I couldn't find it in order to manipulate it using code, so I've simply inserted a Page field on its own.

To do that we need to get hold of the Footer object (type HeaderFooter), so it's passed into the Sub (renamed to avoid confusion with the type).

Option Explicit

'Sub VBScript()

    Dim objWord 'As Word.Application
    Dim objDoc 'As Word.Document
    Dim objSection 'As Word.Section
    Dim objSelection 'As Word.Selection
    Dim objPageNumbers 'As Word.PageNumbers
    
    On Error Resume Next
        Set objWord = GetObject(, "Word.Application")
    On Error GoTo 0
    If objWord Is Nothing Then
        Set objWord = CreateObject("Word.Application")
    End If
    'Set objDoc = objWord.Documents.Open("C:\Documents and Settings\XXX.docx")
    objWord.Visible = True
    Set objDoc = objWord.Documents.Add
    Set objSection = objDoc.Sections(1)
    Set objSelection = objWord.Selection
    Set objPageNumbers = objSection.Footers(1).PageNumbers
        
    Const wdPageBreak = 7
    Const wdPageNumberStyleUppercaseRoman = 1
    Const wdHeaderFooterPrimary = 1
    
    SubHeaderFooter objSection.Footers(wdHeaderFooterPrimary)
'End Sub

Sub SubHeaderFooter(hdr) ' (hdr As Word.HeaderFooter)
    Const wdAlignPageNumberCenter = 1
    Const wdFieldPage = 33
    Dim strNow 'As String
    Dim strDD 'As String
    Dim strMM  'As String
    Dim strYYYY 'As String
    Dim strFulldate 'As Date
    Dim objPageNumbers 'As Word.PageNumbers
    strYYYY = DatePart("yyyy", Now())
    Set objPageNumbers = hdr.PageNumbers
    objPageNumbers.ShowFirstPageNumber = False
    objPageNumbers.RestartNumberingAtSection = True
    hdr.Range.ParagraphFormat.Alignment = 1
    hdr.Range.Text = "Week " & DatePart("ww", Now()) - 1 & " - " & Date & vbCr
    hdr.Range.Fields.Add hdr.Range.Paragraphs.Last.Range, wdFieldPage
End Sub

Open in new window

0
 

Author Comment

by:Maliki Hassani
ID: 39808021
I see you also added getobject to my code.  I appreciate yoyour help.  Thanks!
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39808037
Thanks.
Actually  the whole testing was done in VBA first.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question