[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2862
  • Last Modified:

VBScript: Adding footer to Word doc

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
Maliki Hassani
Asked:
Maliki Hassani
  • 4
  • 4
1 Solution
 
GrahamSkanCommented:
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
 
Maliki HassaniAuthor Commented:
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
 
Maliki HassaniAuthor Commented:
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.

 
GrahamSkanCommented:
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
 
Maliki HassaniAuthor Commented:
That is correct only need the footer in the primary footer.  Thanks
0
 
GrahamSkanCommented:
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
 
Maliki HassaniAuthor Commented:
I see you also added getobject to my code.  I appreciate yoyour help.  Thanks!
0
 
GrahamSkanCommented:
Thanks.
Actually  the whole testing was done in VBA first.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now