简体   繁体   English

使用VBA更改Excel图表的方向(纵向或横向)

[英]Changing the orientation (portrait or landscape) of an Excel chart using VBA

I am trying to write a macro to automatically print all the charts I have created in a workbook using another macro. 我正在尝试编写一个宏,以使用另一个宏自动打印在工作簿中创建的所有图表。 (literally hundreds) The problem I'm having is that I cannot figure out how to change the graph from a portrait layout to a landscape layout using VBA. (实际上是数百个)我遇到的问题是我无法弄清楚如何使用VBA将图形从纵向布局更改为横向布局。 I was wondering if anyone could help me out. 我想知道是否有人可以帮助我。 I tried the code bellow but it gives me an error at the line " .ChartObjects(x).PageSetup.Orientation = xlLandscape " I understand that for a chart object that this isn't the correct property but I can't figure out what else it is. 我尝试了下面的代码,但是它在行“ .ChartObjects(x).PageSetup.Orientation = xlLandscape”处给了我一个错误,我知道对于图表对象来说,这不是正确的属性,但是我无法弄清楚否则。

Any help would be appreciated! 任何帮助,将不胜感激!

Option Explicit

Sub Print_All_Charts()
    Dim szASheet As String
    szASheet = ActiveSheet.Name

    Dim lChartObjCount As Long
    lChartObjCount = ActiveSheet.ChartObjects.Count

    With Application
        .ScreenUpdating = False

        .ActivePrinter = "HP Color LaserJet 5550 PS on Ne08:"

        'On Error Resume Next
        Dim wks As Worksheet
        For Each wks In ActiveWorkbook.Worksheets

            Dim x As Long
            For x = 1 To lChartObjCount

                With wks

                    .ChartObjects(x).PageSetup.Orientation = xlLandscape

                    .ChartObjects(x).Select

                    .ChartObjects(x).Activate

                    .PrintOut , , 1

                End With

            Next x

        Next wks

        ActiveChart.Deselect
        With Sheets(szASheet)
            .Select
            .Range("A1").Select
        End With

        .ScreenUpdating = True
    End With
End Sub

Manipulating Excel charts using VBA is always a bit confusing, because there are ChartObject objects and then there are Chart objects. 使用VBA操纵Excel图表总是有些混乱,因为先有ChartObject对象,再有Chart对象。 Each ChartObject object has a child Chart object. 每个ChartObject对象都有一个子Chart对象。 It isn't always very intuitive which properties and methods belong to the Chart and which are to be found on its parent ChartObject . 哪些属性和方法属于Chart以及哪些属性和方法可以在其父ChartObject上找到并不总是很直观的。 Quoting VBA help: 引用VBA帮助:

[ ChartObject ] represents an embedded chart on a worksheet. [ ChartObject ]表示工作表上的嵌入式图表。 The ChartObject object acts as a container for a Chart object. ChartObject对象充当Chart对象的容器。 Properties and methods for the ChartObject object control the appearance and size of the embedded chart on the worksheet. ChartObject对象的属性和方法控制工作表上嵌入式图表的外观和大小。

Reading VBA help can drive you nuts if you don't have your glasses on, because ChartObject means something different than Chart object! 如果您不戴眼镜,那么阅读VBA帮助可能会让您发疯,因为ChartObject含义不同于Chart对象!

Anyhow, as it turns out, .PageSetup.Orientation sits on Chart and not ChartObject as you were inferring. 无论如何,事实证明, .PageSetup.Orientation位于Chart而不是您推断的ChartObject

    Dim wks As Worksheet
    Dim chartObject As ChartObject

    For Each wks In ActiveWorkbook.Worksheets
        For Each chartObject In wks.ChartObjects
            .Chart.PageSetup.Orientation = xlLandscape ' or xlPortrait
            .Chart.PrintOut Preview:=True, ActivePrinter:="PDFCreator"
        Next
    Next

This assumes that you want to print each chart on a separate page. 假设您要在单独的页面上打印每个图表。 What you were doing in your code was printing out each entire worksheet at once, but that doesn't seem to square with the rest of your question. 您在代码中所做的工作是一次打印出每个工作表,但这似乎与其余的问题不一致。

Here I used PDFCreator as my printer, because I didn't want to waste a bunch of paper while testing this code. 在这里,我使用PDFCreator作为打印机,因为我不想在测试此代码时浪费很多纸。 You can of course adjust this as you see fit. 当然,您可以根据自己的喜好进行调整。

Also I set the active printer at the time of printing. 另外,我在打印时设置了活动打印机。 Of course you can also use Application.ActivePrinter instead, but that will affect the active printer even when the macro is done running. 当然,您也可以改用Application.ActivePrinter ,但这将影响活动打印机,即使宏运行完成后也是如此。

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

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