Solved

VBScript: Adding footer to Word doc

Posted on 2014-01-22
8
2,573 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

856 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