Reputation: 867
Im having a problem while creating and copying a chart in vb6 using excel. I have these following code
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
There is a function for copying the chart and this is where the error occurs
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
in the line ActiveChart.CopyPicture
I get an error message that said "application-defined or object-defined error" I tried researching but I cant seem to find a way to fix this error.
Upvotes: 1
Views: 2866
Reputation: 9471
You should create a reference to the ChartObject rather than activating and relying on the Active ChartObject.
On 2013, there isn't any need to activate the sheet, but be aware of accessing the Chart property - That will require activating the sheet. See How do I reference charts with the same name, but on different worksheets?
Also, The CopyPicture method doesn't have an argument named Size
, so you'll need to remove the 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
Upvotes: 0
Reputation: 53136
As always, you should avoid using the Active*
objects.
change to
With mWorksheet
.ChartObjects("Chart 1").Chart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
.Paste
.ChartObjects("Chart 1").Delete
End With
Note, the reason you got the error is probably because mWorksheet
was not active
Upvotes: 1