Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How To Print Two Excel Charts on Single Page w/OLE Automation


View products that this article applies to.

This article was previously published under Q114348

↑ Back to the top


Summary

It is possible using OLE Automation with Excel version 5.0 to print two charts on a single page. This article demonstrates how to do it in Visual Basic version 3.0.

↑ Back to the top


More information

Excel version 5.0 exposes its printing capabilities through OLE Automation. The Excel Worksheet object supports the PrintOut method. The PrintOut method prints the Worksheet to the printer using the current PageSetup.

The following example creates and positions two Charts on a Worksheet. Then it modifies the PageSetup, and prints the Worksheet to the printer.

Step-by-Step Example

  1. Start a new Project in Visual Basic. Form1 is created by default.
  2. Add a command button (Command1) to Form1.
  3. Place the following code in the command button click event:
     Sub Command1_Click()
    
          ' Define Excel Constants:
          ' Constant values can be found with "MsgBox xlConst" in Excel
          ' Constants are also in XLCONST.BAS in Office Developer's Kit 1.0
          Const xlColumn = 3
          Const xlRows = 1
          Const xlLine = 4
          Const xlPortrait = 1
          Const xlPaperLetter = 1
          Const xlAutomatic = -4105
          Const xlDownThenOver = 1
    
          ' Dimension Variables:
          Dim XL As Object
          Dim WS1 As Object
          Dim WS2 As Object
          Dim PS As Object
          Dim Col As Integer
    
          ' Create an Excel Application object:
          Set XL = CreateObject("Excel.Application")
    
          ' Make Excel visible:
          ' If the following line is changed to a comment, Excel will not be
          ' visible, but the charts will still print
          XL.Visible = True
    
          ' Add a Workbook and set Worksheet objects to the first two Sheets:
          XL.Workbooks.Add
          Set WS1 = XL.WorkSheets(1)
          Set WS2 = XL.WorkSheets(2)
    
          ' Fill in Cells of first Worksheet with data for Charts:
          Randomize Timer
          For Col = 1 To 10
             WS1.Cells(1, Col).Value = 10 * Rnd
             WS1.Cells(2, Col).Value = 10 * Rnd
          Next
    
          ' Display the second Worksheet:
          WS2.Select
    
          ' Add a Chart object at specified position:
          ' Top and Left are relative to Cell A1
          ' Enter the following two lines as one, single line of code:
          WS2.ChartObjects.Add(0, 0, XL.InchesToPoints(6),
             XL.InchesToPoints(4)).Select
    
          ' Use the ChartWizard method to fill in the Chart:
          ' Enter the following three lines as one, single line of code:
          WS2.ChartObjects("Chart 1").Chart.ChartWizard
             WS1.Range(WS1.Cells(1, 1), WS1.Cells(1, 10)), xlColumn, 1, xlRows,
             0, 0, 1, "Chart 1 (Column Chart)", "Columns", "Value", ""
    
          ' Create a second Chart on the same Worksheet:
          ' Enter the following two lines as one, single line of code:
          WS2.ChartObjects.Add(0, XL.InchesToPoints(5), XL.InchesToPoints(6),
             XL.InchesToPoints(4)).Select
          ' Enter the following three lines as one, single line of code:
          WS2.ChartObjects("Chart 2").Chart.ChartWizard
             WS1.Range(WS1.Cells(2, 1), WS1.Cells(2, 10)), xlLine, 4, xlRows,
             0, 0, 1, "Chart 2 (Line Chart)", "Points", "Value", ""
    
          ' The following lists various property settings for the PageSetup
          ' Object in Excel. There may be additional properties available for
          ' different printers. Please check the Excel documentation for
          ' details on the PageSetup object.
          Set PS = WS2.PageSetup
          PS.PrintTitleRows = ""
          PS.PrintTitleColumns = ""
          PS.PrintArea = ""
          PS.LeftHeader = ""
          PS.CenterHeader = "Two Charts on a Page"
          PS.RightHeader = ""
          PS.LeftFooter = ""
          PS.CenterFooter = "Page &P"
          PS.RightFooter = ""
          PS.LeftMargin = XL.InchesToPoints(.75)
          PS.RightMargin = XL.InchesToPoints(.75)
          PS.TopMargin = XL.InchesToPoints(1)
          PS.BottomMargin = XL.InchesToPoints(1)
          PS.HeaderMargin = XL.InchesToPoints(.5)
          PS.FooterMargin = XL.InchesToPoints(.5)
          PS.PrintHeadings = False
          PS.PrintGridlines = False
          PS.PrintNotes = False
          PS.CenterHorizontally = True
          PS.CenterVertically = True
          PS.Orientation = xlPortrait
          PS.Draft = False
          PS.PaperSize = xlPaperLetter
          PS.FirstPageNumber = xlAutomatic
          PS.Order = xlDownThenOver
          PS.BlackAndWhite = False
          PS.Zoom = 100
    
          ' Print the WorkSheet:
          WS2.PrintOut 1
    
          ' Close the Workbook without saving the contents:
          ' The brackets [] around Close are necessary because Close is
          ' a Visual Basic method.
          XL.ActiveWorkbook.[Close] (False)
          ' Shut down instance of Excel:
          XL.Quit
    
          Set XL = Nothing
          Set WS1 = Nothing
          Set WS2 = Nothing
          Set PS = Nothing
    
       End Sub
    						
  4. Save the project.
  5. Run the example, and click the command button.
Excel should become visible. You will see data being put into the cells. After the data is entered, you will see the creation of the charts. Finally, Excel will begin printing.

↑ Back to the top


Keywords: KB114348, kbprogramming, kbinterop, kbhowto

↑ Back to the top

Article Info
Article ID : 114348
Revision : 6
Created on : 2/12/2007
Published on : 2/12/2007
Exists online : False
Views : 561