2016-09-14 122 views
1

我在使用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我得到一个错误信息,所述“应用程序定义的或

对象定义的错误“我试图研究,但我似乎找不到解决此错误的方法。

回答

1

一如既往,您应该避免使用Active*对象。

变化

With mWorksheet 
    .ChartObjects("Chart 1").Chart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture 
    .Paste 
    .ChartObjects("Chart 1").Delete 
End With 

注意,你得到了错误的原因可能是因为mWorksheet并不活跃

+0

的错误是因为'ChartObject.CopyPicture'方法没有一个'Size'参数,但是'Chart'对象*做*。 – ThunderFrame

+0

OP和这个答案都不会尝试执行'ChartObject.CopyPicture'。而且,如果'mWorksheet'处于活动状态,则OPs代码_does_工作。 –

+0

嗯,的确如此。但是在ChartObject上使用CopyPicture可以避免激活'Worksheet'来访问'Chart'。 – ThunderFrame

0

您应该创建,而不是激活并依托主动ChartObject到ChartObject参考。

在2013年,没有任何需要激活工作表,但请注意访问图表属性 - 这将需要激活工作表。见How do I reference charts with the same name, but on different worksheets?

此外,CopyPicture方法没有名为Size参数,所以你需要删除Size:=xlScreen

Public Function Copy_Chart() 
    Dim mWorksheet 
    Set mWorksheet = Sheet1 
    If Not mWorksheet Is Nothing Then 
    With mWorksheet 
     .Activate 
     Dim cht As ChartObject 
     Set cht = .ChartObjects("Chart 1") 
     cht.CopyPicture Appearance:=xlScreen, Format:=xlPicture 
     .Paste 
     cht.Delete 
    End With 
    End If 
End Function 
相关问题