+ Reply to Thread
Results 1 to 2 of 2

Workbook_BeforePrint causes runtime 1004 error only on chart tab

Hybrid View

  1. #1
    Registered User
    Join Date
    08-31-2011
    Location
    Virginia, USA
    MS-Off Ver
    Excel 2003/Excel 2007
    Posts
    37

    Workbook_BeforePrint causes runtime 1004 error only on chart tab

    I am working on setting up a footer that can't be changed. The code I used is
    Private Sub Workbook_BeforePrint(Cancel As Boolean)
    With ActiveSheet.PageSetup
    '.LeftHeader = "header"
    '.CenterHeader = ""
    '.RightHeader = ""
    .LeftFooter = "footer"
    '.CenterFooter = ""
    '.RightFooter = ""
    End With
    End Sub
    (Courtesy of a previous thread)

    This works perfectly for all my tabs except the Chart that is created by a macro. When I test the above code for that tab by using print preview, I get "Run-time error '1004': Unable to set the LeftFooter property of the PageSetup class"

    The chart is made using the following code

    Sub MakeAPlot()
    'This macro will delete an old plot if it exists and generate a new plot using only the applicable data range
       
        Dim rChartXData As Range
        Dim rChartYData As Range
        Worksheets("PLOT Data").Cells(6, 1).Select                          'select a single cell as the initial plot data
    
        
        On Error Resume Next                                                ' remove existing chart if exists
        Application.DisplayAlerts = False
        ActiveWorkbook.Charts("aaa").delete
        Application.DisplayAlerts = True
        On Error GoTo 0
    
        
        With ActiveSheet                                                    ' determine chart data ranges using only actual data
            Set rChartXData = .Range(.Range("G2"), .Range("G2").End(xlDown))
            Set rChartYData = rChartXData.Offset(, 1)
        End With
    
        
        With ActiveSheet.Shapes.AddChart.Chart                              'create new chart in PLOT DATA
            .ChartArea.ClearContents                                        'clear any existing chart contents
            
            With .SeriesCollection.NewSeries                                'add the new series of selected data
             m = Sheets("REPORT").Cells(2, "I")
             yr = Sheets("REPORT").Cells(2, "J")
             .Name = "Report ending " & m & " " & yr                   'Chart title
                .Values = rChartYData
                .XValues = rChartXData
            End With
    
         ' .ChartType = xl3DColumnStacked
           .ChartType = xlColumnStacked                                     'select the chart type
            .Legend.delete                                                  'delete the legend-not needed
            .Axes(xlCategory).HasTitle = True                               'title the graph and axes
            .Axes(xlValue).HasTitle = True
            .Axes(xlCategory).AxisTitle.Characters.Text = "bbb"
            .Axes(xlValue).AxisTitle.Characters.Text = "ccc"
            
            .Location xlLocationAsNewSheet, "Graph"               'move the graph to its own sheet
            
            
            With ActiveSheet.PageSetup                                      'insert header and footer
             .LeftHeader = ""
             .CenterHeader = "&""Calibri,Bold""&18 issues"  
             .RightHeader = ""
             .LeftFooter = "FOOTER"              
             .CenterFooter = "&T  &D"
             .RightFooter = ""
            End With
        End With
    
    End Sub
    Doing some investigating, the cause of the error is supposed to be not having a default printer set-up. I am stuck because I do have a default printer set-up; more confusing for me is that the first code works on every single tab in the workbook EXCEPT the chart. The chart has the same level of protection set as several of the other tabs also; the larger macro handles the protection for the sheets.

    Any thoughts on what is triggering the error or what else to check would be greatly appreciated. Thank you.

  2. #2
    Registered User
    Join Date
    08-31-2011
    Location
    Virginia, USA
    MS-Off Ver
    Excel 2003/Excel 2007
    Posts
    37

    Re: Workbook_BeforePrint causes runtime 1004 error only on chart tab

    I've also tried different types of printers while troubleshooting this. I was testing by printing to CutePDF; I also tried having my default set to an actual printer. I was sure it shouldn't matter but I figured I would rule it out.

    I also tried working backwards through the macro that makes the plot. I tried removing the original graph footer created in the make a plot macro, didn't help.

    If I leave the graph on the tab it is created in and do print preview (with the spreadsheet data still visible behind the graph), the beforeprint footer DOES show up. If I just select the graph for print preview, the beforeprint footer DOES NOT show up. But neither do I get the runtime error in this case. Is it possible what I am seeing is just how Excel handles the beforeprint event for a graph?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1