Solved

Include footer page numbers into Excel using MS Access VBA

Posted on 2013-05-23
10
705 Views
Last Modified: 2013-05-29
Hello Experts,

Your assistance is needed regarding how to include footer page numbers into Excel using MS Access VBA.  

The goal is to update an Excel file with MS Access VBA statements.  The code below is my attempt to update 53 worksheets in a single workbook.  I have been able to manually update the spreadsheet by adding the footer; however, the process should ultimately be automated.

Private Sub MasterUpdate_Click()
Dim ExcelApp As New Excel.Application
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim varCentreFooter As String
Dim WS As Worksheet
Dim rs As Recordset
Dim SSTab As String
Dim shtName As String
Set xlApp = CreateObject("Excel.Application")

Set rs = CurrentDb.OpenRecordset("TblReportsOrder")
rs.MoveFirst

xlApp.DisplayAlerts = False
'Set reference to Workbook object
Set xlBook = xlApp.Workbooks.Open("H:\PDF\MasterReporttest.xls")

Do While Not (rs.EOF)
    shtName = rs!SSTab
    Set xlSheet = xlBook.Sheet(shtName)

  varCentreFooter = " Page # "
 
xlSheet.PageSetup.CenterFooter = varCentreFooter

  rs.MoveNext
Loop

MsgBox "The Workbook is ready!"

End Sub
0
Comment
Question by:CFMI
  • 4
  • 4
  • 2
10 Comments
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 39192487
I may be wrong here, so please wait for an Excel expert.

It may be difficult to determine the Page numbering from Access.
The Page numbering is done from within  the Excel environment, based on the Page settings: margins, orientation, Papersize, header size, footer size, ...etc

So AFAIK, Excel will do this automatically.

If you "hardcode" the page numbering, it may be wrong if any of the settings, I mentioned above, are changed


JeffCoachman
0
 
LVL 80

Expert Comment

by:byundt
ID: 39192994
The following statement should add the page number to your center footer. &P is the code for the page number.
 varCentreFooter  = "Page # &P"
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 39193091
Hey Brad...
;-)

guess I misunderstood the question
0
 
LVL 1

Author Comment

by:CFMI
ID: 39201584
Hello,

When I updated the statement, the footer wasn't updated so I was thinking could it be a problem using a xls instead of the xlsx file?  Any other help with the below code is really appreciated as I am looping through an Access table with the Worksheet names and opening the workbook.  As you see, I tried opening the workbook at the end since I have to reboot to open the file.

Private Sub MasterUpdate_Click()
Dim ExcelApp As New Excel.Application
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim varCentreFooter As String
Dim WS As Worksheet
Dim rs As Recordset
Dim SSTab As String
Dim shtName As String
Set xlApp = CreateObject("Excel.Application")

Set rs = CurrentDb.OpenRecordset("TblReportsOrder")
rs.MoveFirst

xlApp.DisplayAlerts = False
'Set reference to Workbook object
Set xlBook = xlApp.Workbooks.Open("H:\PDF\Master.xls")

Do While Not (rs.EOF)
    shtName = rs!SSTab
    'Set xlSheet = xlBook.Sheet(shtName)
  xlBook.Activate
  varCentreFooter = "Page # &P"
 
'xlSheet.PageSetup.CenterFooter = varCentreFooter

  rs.MoveNext
Loop

    xlBook.Save
    xlBook.Close
    ExcelApp.Quit
    xlApp.Quit
   
MsgBox "The Workbook is ready!"
ExcelApp.Visible = True

End Sub
0
 
LVL 80

Expert Comment

by:byundt
ID: 39201860
The PageSetup.CenterFooter part works perfectly in Excel. In Word, it worked on .xls and .xlsx files, but not on a .xlsm file. There were no errors, but the .xlsm file just didn't save.

I changed your code to use late-binding so you don't need to set references.

Private Sub MasterUpdate_Click()
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Dim varCentreFooter As String
Dim rs As Recordset
Dim SSTab As String
Dim shtName As String
Set xlApp = CreateObject("Excel.Application")

Set rs = CurrentDb.OpenRecordset("TblReportsOrder")
rs.MoveFirst

xlApp.DisplayAlerts = False
'Set reference to Workbook object
xlApp.EnableEvents = False
Set xlBook = xlApp.Workbooks.Open("H:\PDF\Master.xls")


Do While Not (rs.EOF)
    shtName = rs!SSTab
    Set xlSheet = xlBook.Worksheets(shtName)
    varCentreFooter = "Page # &P"
 
    xlSheet.PageSetup.CenterFooter = varCentreFooter

    rs.MoveNext
Loop

    xlBook.Save
    xlBook.Close
    xlApp.Quit
    Set xlSheet = Nothing
    Set xlBook = Nothing
    Set xlApp = Nothing
   
MsgBox "The Workbook is ready!"

End Sub

Open in new window

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 1

Author Comment

by:CFMI
ID: 39202612
Experts,

When I used the above code, there were no footers.  The code ran but afterwards, the spreadsheet did not have any footers so I tried stepping through the code and it also appeared to work well.  Do you have any other ideas or perhaps a different direction?
0
 
LVL 80

Expert Comment

by:byundt
ID: 39202639
When I stepped through the code, I could verify (using the Immediate pane) that the footer was updated on a .xlsm file--but the save never completed as evidenced by the file modification date. I tried early-binding, late-binding, workbook activation and sheet activation. None of it seemed to make a difference.

I did enjoy success using Word 2013 (I don't use Access) to run the macro without the Access-specific stuff, but only on .xls and .xlsx files. My failure with .xlsm files could be due to corruption in my Excel 2013 install or it could be an actual bug.

When you tested the code in Access, which version did you use? Did the file modification date change?

If both of us are having problems, it makes it more likely that there is a bug. If so, I'll report it to Microsoft.

Brad
0
 
LVL 1

Author Comment

by:CFMI
ID: 39204290
Good Morning Brad,

I am using Access 2010 v 14.0.6112.5000 (32-bit) and the modification date did not change so I will inform Microsoft; should I select "Accept as Solution"?
0
 
LVL 80

Accepted Solution

by:
byundt earned 500 total points
ID: 39204454
What type of files (.xls, .xlsx, .xlsb, .xlsm) did you test?
What operating system are you using?
What is your default printer? I found an old KnowledgeBase article saying that PageSetup would fail if there was no default printer.

The following code works if run from Excel. It puts the page number in the center footer of every worksheet except for two as listed in a Select Case block:
Sub CenterFooter()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
    Select Case ws.Name
    Case "Summary", "Confidential"  'Do nothing to footers on these worksheets
    Case Else
        ws.PageSetup.CenterFooter = "Page # &P"
    End Select
Next
End Sub

Open in new window

Brad
0
 
LVL 1

Author Closing Comment

by:CFMI
ID: 39205413
Brad,

Forget the questions; your code is PERFECT.  Joe himself could not have thrown a better Touchdown pass - THANK you very much!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

757 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