由于我并不完全熟悉宏,因此我录制了一个宏来自动格式化Excel 2010中的报表,我每周都会向人们发送报表。宏的一部分调整页面设置属性,包括将布局更改为横向和法定大小。我可以在我的电脑上运行它,因为我的打印机有Legal尺寸的纸张作为默认设置之一。但是,有些人在使用Legal Size纸张打印之前必须手动调整打印机并且出现错误。返回的错误是:运行时错误'1004':对象'_Application'的方法'PrintCommunication'失败
当我调试它时,突出显示Application.Printcommunication = True行。当我将代码更改为Application.PrintCommunication = False时,宏不会按照它的构建方式完全格式化(例如,缩放保持在100%)。我认为解决方法可能是在运行宏之前将其默认打印机更改为Nitro Pro(类似于Adobe Pro),但错误仍然存在。非常感谢任何反馈!
Sub Auto_Open()
'
' Macro2 Macro
'
'
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$1"
.LeftMargin = Application.InchesToPoints(0.17)
.RightMargin = Application.InchesToPoints(0.17)
.TopMargin = Application.InchesToPoints(0.62)
.BottomMargin = Application.InchesToPoints(0.48)
.HeaderMargin = Application.InchesToPoints(0.17)
.FooterMargin = Application.InchesToPoints(0.17)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 1200
.CenterHorizontally = True
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperLegal
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = 60
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = True
End With
Application.PrintCommunication = True
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("N2:O2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("L1").Select
Columns("B:B").ColumnWidth = 10.86
Columns("D:D").ColumnWidth = 18.86
Columns("E:E").ColumnWidth = 13.43
Columns("F:F").ColumnWidth = 11.29
Columns("F:F").ColumnWidth = 19.29
Columns("G:G").EntireColumn.AutoFit
Range("H1").Select
ActiveWindow.SmallScroll Down:=-33
Range("I1").Select
Range("G1").Select
Columns("H:H").ColumnWidth = 13
Columns("I:I").ColumnWidth = 18.71
Columns("J:J").ColumnWidth = 19.86
Columns("K:K").ColumnWidth = 13.57
Columns("L:L").ColumnWidth = 11
Columns("L:L").ColumnWidth = 11.43
Columns("M:M").ColumnWidth = 12.71
Columns("M:M").ColumnWidth = 15.86
Columns("N:N").ColumnWidth = 41.86
Columns("O:O").ColumnWidth = 18.57
Columns("O:O").ColumnWidth = 28.86
Columns("O:O").ColumnWidth = 42
Columns("L:L").ColumnWidth = 12.71
Range("K2:L2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.NumberFormat = "$#,##0"
ActiveWindow.SmallScroll Down:=-30
Columns("A:A").Select
Selection.EntireColumn.Hidden = True
Rows("1:1000").EntireRow.AutoFit
Range("B1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
ActiveWindow.Zoom = 75
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Range("E2").Select
ActiveWindow.SmallScroll Down:=-6
ActiveWindow.LargeScroll ToRight:=-1
With ActiveWindow
.SplitColumn = 0
.SplitRow = 1
End With
ActiveWindow.FreezePanes = True
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = "&""-,Bold""&12Weekly Staffing Summary Request &D"
.RightHeader = ""
.LeftFooter = "&D"
.CenterFooter = "&P"
.RightFooter = "&F"
End With
End Sub
答案 0 :(得分:3)
从记录的宏中引入行.PrintQuality = 1200
并导致错误。删除该行并无论打印机如何工作,甚至使用Nitro PDF作为默认打印机。
答案 1 :(得分:2)
显然有些迟到的回复,但今天我遇到了同样的错误。我的问题是我的电脑无法联系默认打印机。
我将其更改为只在计算机内部工作的PDF打印机,然后一切正常!
答案 2 :(得分:1)
这里的问题是您的打印机设置(与宏无关)不允许宏与打印机通信。设置打印机设置时,需要进行通信。进入打印机设置并进行设置,我想您只需要“启用双向通信”,问题就会消失。
答案 3 :(得分:0)
在顶部添加此行&一切都会好的:
On Error Resume Next
答案 4 :(得分:0)
您可以使用“on next resume next”,如下所示:
On Error Resume Next
Application.PrintCommunication = False
Err.Clear
然后:
On Error Resume Next
Application.PrintCommunication = True
Err.Clear
如果您只想进行页面设置,则无需关闭PrintCommunication。但是可以在执行时间中看到差异:关闭打印机通信时执行速度更快。
我有Excel 2010,有时PrintCommunication工作正常,有时候没有。直到现在我才弄明白为什么!
答案 5 :(得分:0)
我尝试了不同的方法来查找问题。
我没有停止打印通信和运行宏 Application.PrintCommunication = True
出错时,显示错误消息。 就我而言,它是纸张尺寸。 我将其更正为“.PaperSize = xlPaperLetter”
在那之后,我对宏进行了多次试运行,它运行良好。
最后,当一切都很好时。我在页面设置之前停止了通信。 Application.PrintCommunication = False