Error 1004 with SetBackgroundPicture

aarbrown

New Member
Joined
Aug 12, 2005
Messages
4
Hi all,

I have written some VBA in Access that is controlling an Excel spreadsheet I have created using OLE automation. Everything works fine, except I am having trouble getting my VBA to set the background picture of a chart on my spreadsheet. Here is the code from the subroutine that is adding the background to the chart:
Code:
Private Sub fillchart(ByVal rngDataSource As range)
Dim iDataRowsCt As Long
Dim iDataColsCt As Integer
Dim iSrsIx As Integer
Dim srsNew As Series
    With rngDataSource
        iDataRowsCt = .Rows.Count
        iDataColsCt = .Columns.Count
    End With
    With chtCTQChart
        .ChartType = xlXYScatter
        For iSrsIx = 2 To iDataRowsCt
            Set srsNew = .SeriesCollection.NewSeries
            With srsNew
                .Name = rngDataSource.Cells(iSrsIx, 1)
                .Values = rngDataSource.Cells(iSrsIx, 2)
                .XValues = rngDataSource(iSrsIx, 3)
            End With
        Next iSrsIx
        With .Axes(xlValue)
            .MaximumScale = 5
            .MinimumScale = 0
        End With
    .SetBackgroundPicture FileName:="c:\chartbackground.gif"
    End With

End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
When I record a macro I am getting the following code:
Code:
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 8/12/2005 by Aaron Brown
'

'
    ActiveChart.Axes(xlValue).MajorGridlines.Select
    ActiveChart.ChartArea.Select
    ActiveChart.PlotArea.Select
    With Selection.Border
        .ColorIndex = 16
        .Weight = xlThin
        .LineStyle = xlContinuous
    End With
    Selection.Fill.UserPicture PictureFile:="C:\newpicture.GIF"
    Selection.Fill.Visible = True
    ActiveChart.ChartArea.Select
End Sub
I tried substituting the chtCTQChart.SetBackgroundPicture with the Selection.Fill.UserPicture PictureFle:="C:\newpicture.GIF" from above, but I am recieving an error 1004 saying the chtCTQChart.PlotArea.Select Failed. Here is the code from Access:
Code:
Private Sub fillchart(ByVal rngDataSource As range)
Dim iDataRowsCt As Long
Dim iDataColsCt As Integer
Dim iSrsIx As Integer
Dim srsNew As Series
    With rngDataSource
        iDataRowsCt = .Rows.Count
        iDataColsCt = .Columns.Count
    End With
    With chtCTQChart
        .ChartType = xlXYScatter
        For iSrsIx = 2 To iDataRowsCt
            Set srsNew = .SeriesCollection.NewSeries
            With srsNew
                .Name = rngDataSource.Cells(iSrsIx, 1)
                .Values = rngDataSource.Cells(iSrsIx, 2)
                .XValues = rngDataSource(iSrsIx, 3)
            End With
        Next iSrsIx
        With .Axes(xlValue)
            .MaximumScale = 5
            .MinimumScale = 0
        End With
       .ChartArea.Select
       .PlotArea.Select
                
    End With
    With Selection.Fill
        .UserPicture PictureFile:="C:\newpicture.GIF"
        .Visible = True
    End With
End Sub
 
Upvote 0
You need to activate or select the chart object before you can work with it:

Charts("chart1").Activate

or

Charts(1).Activate
With ActiveChart

or

Worksheets(1).ChartObjects(1).Activate
 
Upvote 0
Thank you all very much for your help. Yes, I did find that I had to activate the chart before I could use that property. However, when I activated the chart Excel would crash if I clicked on the first worksheet for some reason. Therefore I needed to set the background picture without activating the chart. I found, thanks to the macro, a property that would allow me to do that:

PlotArea.Fill.UserPicture PictureFile:="C:\newpicture.GIF"

I added that to my code, using the chart object, and it worked perfectly!
 
Upvote 0
Yes, Excel will do that, it lacks Inheritance: the ability for one object to modify another object directly, without some intermediary. Glad you found it.
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top