使用Excel VBA调整图表对象的大小

时间:2016-05-04 18:48:30

标签: vba excel-2010

我想调整Chart对象的大小。我正在从Excel中的一组数据创建图形,并通过VBA将图形插入Word文档中。

我尝试使用.Width对象中的ChartObjects属性,但我无法将其扩展到图表。 (我在ws1.ChartObjects时遇到了应用程序错误。)

Sub CPU_Top_10_Chart()

Dim wdApp   As Word.Application
Dim wdDoc   As Word.Document
Dim cht     As Chart
Dim ws1     As Worksheet

 Set wdApp = CreateObject("Word.Application")
 Set wdDoc = wdApp.Documents.Add("C:\...\chart_test.docx")

 Set cht = Charts.Add
 Set ws1 = ActiveWorkbook.Sheets("Sheet1")

 With cht

        .SetSourceData Source:=ws1.Range("A2:C12")
        .ChartType = xlColumnClustered

End With

cht.CopyPicture
wdDoc.Bookmarks("insert_chart").Range.Paste
End Sub

2 个答案:

答案 0 :(得分:2)

您可以调用“Worksheet.Shapes.AddChart”来创建图表并设置大小:

With ActiveSheet.Shapes.AddChart(xlColumnClustered, Width:=100, Height:=100)
  With .Chart
    .SetSourceData ws1.Range("A2:C12")
  End With
  .CopyPicture
End With

答案 1 :(得分:0)

这就是我添加的内容

Set cht = ws1.Shapes.AddChart(xlColumnClustered, Width:=300, Height:=300).Chart