How to add Page Number with VBA

B

brent.reid

I have a custom footer in Word that uses information from a Document
Management System to put the Document number and Version number in
the footer.

Users want to include the page number in the footer, but if the page
number is inserted via Word menu commands, the custom footer gets
overwritten.

What I would like to do is insert the page number as part of the
Custom footer so that all the information is put into the footer at
once. However, I am having difficulty finding the proper syntax for
inserting the page number as part of the custom footer code.


I've included the code I'm using below. If anyone has a suggestion on
how to include the page number as part of the strFooterText string,
i'd appreciate it.

Thank you.


Private Sub insert_footer(strFooterText As String)

Dim footer_selection As Selection
Dim initial_active_window_view_type As Integer
initial_active_window_view_type = ActiveWindow.View.Type
Application.EnableCancelKey = wdCancelDisabled

If (ActiveWindow.View.SplitSpecial = wdPaneNone) Then
ActiveWindow.ActivePane.View.Type = wdPageView
Else
ActiveWindow.View.Type = wdPageView
End If

ActiveWindow.View.SeekView = wdSeekMainDocument
ActiveWindow.View.SeekView = wdSeekPrimaryFooter

ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Shapes.SelectAll
Set footer_selection = ActiveWindow.Selection

' Set the footer text to the sole parameter of this function.
footer_selection.Text = strFooterText
footer_selection.Font.Size = 8
footer_selection.Font.Name = "Times New Roman"
ActiveWindow.View.Type = wdPageView
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range =
footer_selection.Range
footer_selection.Paragraphs.Alignment = wdAlignParagraphLeft
ActiveWindow.View.Type = wdNormalView
ActiveWindow.View.Type = initial_active_window_view_type
Word.StatusBar = ""
Application.EnableCancelKey = wdCancelInterrupt
End Sub


Private Function get_footer_information() As String
Dim strFooterText As String
Dim objDOCSObjectsDM As Object
Dim strActiveDocumentFullName As String
Dim out_strValue As String
Dim strDocumentName As String
Dim strAuthorID As String
Dim strDocumentNumber As String
Dim strAbstract As String
Dim strForm As String
Dim strTypeID As String
Dim strCreationDate As String
Dim strLastEditDate As String
Dim strClientName As String
Dim strMatterName As String
Dim strClientID As String
Dim strMatterID As String
Dim strVersionNumber As String
Dim strPageNo As String

strActiveDocumentFullName = ActiveDocument.FullName
Set objDOCSObjectsDM = CreateObject("DOCSObjects.DM")

objDOCSObjectsDM.GetDocInfo strActiveDocumentFullName, "DOCNAME",
out_strValue
strDocumentName = out_strValue

objDOCSObjectsDM.GetDocInfo strActiveDocumentFullName,
"AUTHOR_ID", out_strValue
strAuthorID = out_strValue

objDOCSObjectsDM.GetDocInfo strActiveDocumentFullName, "DOCNUM",
out_strValue
strDocumentNumber = out_strValue

objDOCSObjectsDM.GetDocInfo strActiveDocumentFullName, "ABSTRACT",
out_strValue
strAbstract = out_strValue

objDOCSObjectsDM.GetDocInfo strActiveDocumentFullName, "TYPE_ID",
out_strValue
strTypeID = out_strValue

objDOCSObjectsDM.GetDocInfo strActiveDocumentFullName,
"CREATION_DATE", out_strValue
strCreationDate = out_strValue

objDOCSObjectsDM.GetDocInfo strActiveDocumentFullName,
"LASTEDITDATE", out_strValue
strLastEditDate = out_strValue

objDOCSObjectsDM.GetDocInfo strActiveDocumentFullName,
"CLIENT_NAME", out_strValue
strClientName = out_strValue

objDOCSObjectsDM.GetDocInfo strActiveDocumentFullName,
"CLIENT_ID", out_strValue
strClientID = out_strValue

objDOCSObjectsDM.GetDocInfo strActiveDocumentFullName, "FORM",
out_strValue
strForm = out_strValue

objDOCSObjectsDM.GetDocInfo strActiveDocumentFullName,
"MATTER_NAME", out_strValue
strMatterName = out_strValue

objDOCSObjectsDM.GetDocInfo strActiveDocumentFullName,
"MATTER_ID", out_strValue
strMatterID = out_strValue

strVersionNumber = get_version_number(strActiveDocumentFullName)


strFooterText = ""

strFooterText = strFooterText & " #" & strDocumentNumber & " v" &
strVersionNumber & " " & strPageNo


get_footer_information = strFooterText
End Sub



Public Sub subEventReplaceDMFooter(strDocumentName As String, ByRef
objSAPI As Object)
' This subroutine is called by DM when the Insert>DM Footer
' menu item is clicked in Word.
' The DM code that normally inserts the footer is not called,
' i.e. this code replaces that code.
'
' Collect the information intended for the footer.
Dim strFooterText As String
strFooterText = get_footer_information()

' Insert the information into the footer.
insert_footer strFooterText
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top