我在使用excel创建和复制vb6中的图表时遇到问题。我有这些下面的代码VBA Activechart.CopyPicture对象定义的错误
Private Sub CreateChart(Optional ByVal ChartTitle As String _
, Optional ByVal xAxis As Excel.Range _
, Optional ByVal yAxis As Excel.Range _
, Optional ByVal ColumnName As String _
, Optional ByVal LegendPosition As XlLegendPosition = xlLegendPositionRight _
, Optional ByVal rowIndex As Long = 2 _
, Optional ByRef ChartType As String = xlLineMarkers _
, Optional ByVal PlotAreaColorIndex As Long = 2 _
, Optional ByVal isSetLegend As Boolean = False _
, Optional ByVal isSetLegendStyle As Boolean = False _
, Optional ByVal LegendStyleValue As Long = 1)
Const constChartLeft = 64
Const constChartHeight = 300
Const constChartWidth = 700
Dim xlChart As Excel.ChartObject
Dim seriesCount As Long
Dim ColorIndex As Long
Dim marrayhold() As Variant
Dim counter As Long
Dim j As Long
With mWorksheet
.Rows(rowIndex).RowHeight = constChartHeight
Set xlChart = .ChartObjects.Add(.Rows(rowIndex).Left, .Rows(rowIndex).Top, constChartWidth, constChartHeight)
End With
With xlChart.chart
.ChartType = ChartType
.SetSourceData Source:=yAxis, PlotBy:=xlRows
.SeriesCollection(1).XValues = xAxis
.HasTitle = True
.Legend.Position = LegendPosition
.Legend.Font.Size = 7.3
.Legend.Font.Bold = True
.Legend.Border.LineStyle = xlNone
.Legend.Border.ColorIndex = 1
.ChartTitle.Characters.Text = ChartTitle
.ChartTitle.Font.Bold = True
.Axes(xlValue).TickLabels.Font.Size = 8 ' yAxis Labels
.Axes(xlCategory).TickLabels.Font.Size = 8 ' xAxis Labels
.PlotArea.Interior.ColorIndex = PlotAreaColorIndex
.PlotArea.Interior.ColorIndex = 15
.PlotArea.Interior.PatternColorIndex = 1
.PlotArea.Interior.Pattern = xlSolid
xlChart.Name = "Chart 1"
Call Copy_Chart
End With
End Sub
有用于复制图的功能,这是在发生错误时
Public Function Copy_Chart()
With mWorksheet
.ChartObjects("Chart 1").Activate
ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, format:=xlPicture
.Paste
.ChartObjects("Chart 1").Delete
End With
End Function
在线路
ActiveChart.CopyPicture
我得到一个错误信息,所述“应用程序定义的或
对象定义的错误“我试图研究,但我似乎找不到解决此错误的方法。
的错误是因为'ChartObject.CopyPicture'方法没有一个'Size'参数,但是'Chart'对象*做*。 – ThunderFrame
OP和这个答案都不会尝试执行'ChartObject.CopyPicture'。而且,如果'mWorksheet'处于活动状态,则OPs代码_does_工作。 –
嗯,的确如此。但是在ChartObject上使用CopyPicture可以避免激活'Worksheet'来访问'Chart'。 – ThunderFrame