Solved

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
This collection of functions covers all the normal rounding methods of just about any numeric value.
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 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.

708 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

12 Experts available now in Live!

Get 1:1 Help Now