简体   繁体   English

代码在VBA for Excel 2013中有效,但在Excel 2016中出错

[英]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" 以下代码在Excel 2013中工作正常。我们的IT人员使用Excel 2016为我们安装了新计算机。现在打开文件时,出现错误“运行时错误'1004':无法设置PageSetup类的CenterHeaderproperty”

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. 当我尝试记录宏以查看Excel 2016是否做不同的事情时,该代码看起来与此处相同,并且此代码在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. 在OP确认它可行后,将其从我的评论中迁移出来。

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. 通常,除非打印机与安装的驱动程序一起正常工作,否则您无法在VBA中执行任何类型的页面设置操作。 See if IT can come install Office 2016 on a computer that has working printers and Office 2013 where this code already works. 查看IT人员是否可以将Office 2016安装在具有可正常使用此代码的打印机和Office 2013的计算机上。 Alternatively, try to set the main printer to default if it has drivers installed already. 或者,如果已经安装了驱动程序,请尝试将主打印机设置为默认打印机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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