Solved

VBScript: Adding footer to Word doc

Posted on 2014-01-22
8
2,421 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
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 video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now