Solved

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

Posted on 2013-06-11
2
549 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 92

Accepted Solution

by:
Patrick Matthews earned 500 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

696 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