Excel VBA:如何检测打印宽度是否大于打印高度

时间:2014-07-14 09:18:39

标签: excel vba printing width area

要将打印区域自动调整为工作表内容,我使用以下Excel 2010 VBA代码:

Application.PrintCommunication = False
With ActiveSheet.PageSetup
    .Zoom = False
    .FitToPagesWide = 1
    .FitToPagesTall = 1
End With
Application.PrintCommunication = True

有效。现在另外我想检测打印宽度是否大于打印高度,然后更改为横向打印。像这样:

Application.PrintCommunication = False
With ActiveSheet.PageSetup
    .Zoom = False
    .FitToPagesWide = 1
    .FitToPagesTall = 1

    if .PrintedWidth > .PrintedHeight then      ' how to detect it?
        .Orientation = xlLandscape
    end if
End With
Application.PrintCommunication = True

如何检测打印宽度是否大于打印高度?

我看到自动调整后PrintArea属性为空,所以我不能用它来检测条件。

1 个答案:

答案 0 :(得分:0)

已解决:只需比较ActiveSheet.UsedRange范围的宽度和高度:

If ActiveSheet.UsedRange.Width > ActiveSheet.UsedRange.Height Then
    .Orientation = xlLandscape
End If