?
Solved

MS Access VBA to insert Page Numbers into Excel with Read-Only display

Posted on 2013-06-11
2
Medium Priority
?
553 Views
Last Modified: 2013-06-11
Hello Experts,

I am in the process of adding Page Numbers into an Excel workbook and when doing so, an error message appears that the workbook is read-only.

I attempted to correct by closing the workbook before opening; however, that was not successful.

Please advise as the debug feature highlights, “xlBook.Save”.

Private Sub WrkBKPages_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
Dim ws As Worksheet
Set xlApp = CreateObject("Excel.Application")
DoCmd.Hourglass True
Set rs = CurrentDb.OpenRecordset("TblReports")
rs.MoveFirst

xlApp.DisplayAlerts = False
'Set reference to Workbook object
xlApp.EnableEvents = False
'Call xlApp.Workbooks("H:\MasterReport.xls").Close
Set xlBook = xlApp.Workbooks.Open("H:\MasterReport.xls")  'S:\Finance\Finance Shared\CFIDIR\ANALYSIS\Close Reports\MasterReport.xls

For Each ws In ActiveWorkbook.Worksheets

ws.PageSetup.CenterFooter = "Page # &P"

Next

    xlBook.Save
    xlBook.Close SaveChanges:=True
    xlApp.Quit
    Set xlSheet = Nothing
    Set xlBook = Nothing
    Set xlApp = Nothing
 
 DoCmd.Hourglass False
MsgBox "The Workbook has Page Numbers!"

End Sub
0
Comment
Question by:CFMI
[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
2 Comments
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 2000 total points
ID: 39238822
Which line does the debugger jump to, and can you post a screen shot of the actual error message?

I just tried running code that updates the footer in a read-only workbook, and it runs successfully.

If you have a read-only workbook, you should probably be using SaveAs rather than Save.  I would also change "For Each ws In ActiveWorkbook.Worksheets" to "For Each ws In xlBook.Worksheets".
0
 
LVL 1

Author Closing Comment

by:CFMI
ID: 39238912
The provided statement worked well and page numbers were included.
0

Featured Post

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

752 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