简体   繁体   中英

Code works in VBA for Excel 2013, but it errors in Excel 2016

The following code works fine in Excel 2013. Our IT person installed a new computer for us with Excel 2016. Now when opening the file I get an error "Run-time error '1004': Unable to set the CenterHeaderproperty of the PageSetup class"

When I tried to record a macro to see if Excel 2016 was doing something differently the code looked the same as here, and again this code still runs fine on 2013.

Private Sub Workbook_Open()
Dim Obj As Object

AWb = Application.ActiveWorkbook.Name
If AWb = "UCSB Service Form.xlsm" Then
 Application.ScreenUpdating = False
 Sheets("Form").Activate
 DateFormat = Format(Date, "mm/dd/yy;@")
 Range("F2") = DateFormat
 Range("F36") = DateFormat

 Cells.Validation.Delete

'Sets record number
 Record = "Pending"
 Range("H6") = "'" & Record

 ActiveSheet.PageSetup.CenterHeader = "Record: " & Record
         '<<< Debug indicates this line
         'Run-time error '1004': Unable to set the CenterHeaderproperty of the PageSetup class

'Open blank form

 Range("F3:F7").ClearContents
 Range("E9:F10").ClearContents
 Range("E12:F18").ClearContents
 Range("E20:F23").ClearContents
 Range("E26:F29").ClearContents
 Range("K2:K5").ClearContents
 Range("H4:I6").ClearContents
 Range("G9:J25").ClearContents
 Range("I27:I31").ClearContents
 Range("K27:K31").ClearContents
 Range("K27:K31").ClearContents
 Range("G34:I39").ClearContents
 Range("I40").ClearContents
 Range("K40").ClearContents
 Range("K33:K36").ClearContents

'Clear Highlighting

 Range("F3:F7").Interior.Color = xlNone
 Range("E9:F9").Interior.Color = xlNone
 Range("E12:F13").Interior.Color = xlNone
 Range("E14:F17").Interior.Color = xlNone
 Range("E18:F18").Interior.Color = xlNone
 Range("E20:F23").Interior.Color = xlNone
 Range("H4:I4").Interior.Color = xlNone

Application.ScreenUpdating = True
 Range("A1").Select
 Range("F3").Select
Application.ScreenUpdating = False

Range("H6") = "'" & Record

Set Obj = ActiveSheet.CommandButton1
Obj.Caption = "Save"

Call M_Lists.S_Lists

End If
'Updated 4/25/18
End Sub

Migrated from my comment after OP confirmed it worked.

I suspect this issue is related to your printer problems. Typically you cannot do any kind of page setup actions in VBA unless printers are working properly with drivers installed. See if IT can come install Office 2016 on a computer that has working printers and Office 2013 where this code already works. Alternatively, try to set the main printer to default if it has drivers installed already.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM