要用vba获取或设置工作表的打印区域,需要用到PageSetup对象。
PageSetup对象可以通过工作表对象Worksheet的PageSetup属性获得。
要设置或获取工作表的打印区域,可以使用PageSetup对象的PrintArea属性。
如果设置PageSetup对象的PrintArea属性为False或者””,则表示打印整个工作表。
以下vba代码演示了如何获取、设置工作表的打印区域:
Sub QQ1722187970()
Dim oWB As Workbook
Set oWB = Excel.ThisWorkbook
Dim oWK As Worksheet
Dim oRng As Range
For Each oWK In oWB.Worksheets
With oWK
With .PageSetup
sArea = .PrintArea
If Len(sArea) Then
Set oRng = Excel.Application.Evaluate(sArea)
MsgBox "当前的打印区域为" & oRng.Address(True, True, xlA1, True)
Else
MsgBox "你设置了打印整个工作表"
.PrintArea = oWK.Range("a1").CurrentRegion.Address
End If
End With
End With
Next
End Sub


发表评论