?
Solved

VBScript: Adding footer to Word doc

Posted on 2014-01-22
8
Medium Priority
?
2,728 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
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.
Suggested Courses

765 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