Results 1 to 4 of 4
  1. #1
    Star Lounger
    Join Date
    Apr 2002
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Accessing charts in a presentation (VB,VBA/Powerpoint/W2000)

    Hi guys,

    I would like to know if it is possible to access from VB to the charts objects that I have inside a specific slide. I was doing this with text using the following:

    For j = 1 To nShapes
    pp = ppApp.Presentations(1).Slides(i).Shapes( j ).HasTextFrame
    If pp = -1 Then
    Text1 = ppApp.Presentations(1).Slides(i).Shapes( j ).TextFrame.TextRange.Text
    End If
    Next j

    I need to do something similar but with charts. I need to know how many charts and its properties contains a presentation.

    Thanks a lot
    Luis Espinosa

  2. #2
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Accessing charts in a presentation (VB,VBA/Powerpoint/W2000)

    Luis, here's some sample code. Remember to use the Project | References menu to add MSOffice, PowerPoint, and MSGraph object libraries. Notice how I have prefixed by dim's with PowerPoint and Graph. This is important because you have VB Shapes and PowerPoint Shapes. I left the presentation open so that you can see the results. I'll also post a VB project that creates a chart, so you can see some of the methods. HTH --Sam
    <pre>Option Explicit

    Private Sub Command1_Click()
    ' Be sure to add a reference to MSOffice, PoPo and MSGraph
    Dim ppApp As New PowerPoint.Application
    Dim ppPres As PowerPoint.Presentation
    Dim ppSlide As PowerPoint.Slide
    Dim s As PowerPoint.Shape
    Dim ppChart As PowerPoint.Shape
    Dim gChart As Graph.Chart
    ppApp.Visible = True
    Set ppPres = ppApp.Presentations.Open(App.Path & "Sample.ppt")
    For Each ppSlide In ppPres.Slides
    For Each s In ppSlide.Shapes
    If s.Type = msoEmbeddedOLEObject Then
    Set gChart = s.OLEFormat.object
    With gChart.Application.DataSheet
    ' Double one of the points
    .Cells(2, 3) = .Cells(2, 3) * 2
    End With
    gChart.Application.Update
    ' Deactivate the chart
    gChart.Application.Quit
    Set gChart = Nothing
    End If
    Next s
    Next ppSlide

    ' Clean up!
    Set ppPres = Nothing
    Set ppApp = Nothing
    End Sub</pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  3. #3
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Accessing charts in a presentation (VB,VBA/Powerpoint/W2000)

    Just for completeness, here is a VB project that creates a presentation with a chart in it. Note -- this code was adapted from sample code in Q244589. HTH --Sam
    <pre>Option Explicit

    Private Sub Command1_Click()
    ' Be sure to add a reference to PoPo and MSGraph
    Dim ppApp As New PowerPoint.Application
    Dim ppPres As PowerPoint.Presentation
    Dim ppSlide As PowerPoint.Slide
    Dim ppChart As PowerPoint.Shape
    Dim gChart As Graph.Chart
    ppApp.Visible = True
    Set ppPres = ppApp.Presentations.Add
    Set ppSlide = ppPres.Slides.Add( _
    Index:=1, Layout:=ppLayoutTitleOnly)
    ppSlide.Shapes(1) _
    .TextFrame.TextRange.Text = "Sample Chart"
    Set ppChart = ppSlide.Shapes.AddOLEObject _
    (Left:=50, Top:=150, ClassName:="MSGraph.Chart")
    Set gChart = ppChart.OLEFormat.object
    With gChart
    ' Setup chart options
    .ChartArea.Font.Size = 8
    .ChartType = xl3DBarClustered
    .HasTitle = True
    .ChartTitle.Text = "Sales per Product"
    .ChartTitle.Font.Size = 12
    .Axes(xlValue).HasTitle = True
    .Axes(xlValue).AxisTitle.Caption = "Dollars ($)"
    .ChartArea.AutoScaleFont = False

    ' Add chart data
    With .Application.DataSheet
    .Cells.Clear

    ' Add the chart row labels.
    .Cells(2, 1).Value = "Widgets"
    .Cells(3, 1).Value = "Gadgets"
    .Cells(4, 1).Value = "Gizmos"

    ' Add the chart column labels.
    .Cells(1, 2).Value = "1999"
    .Cells(1, 3).Value = "2000"

    ' Add data to the chart.
    Dim r As Integer, c As Integer
    For r = 2 To 4
    For c = 2 To 3
    .Cells(r, c).Value = Rnd() * 100000
    Next
    Next
    End With
    .Application.Update 'Update the changes
    .Application.Quit 'and deactivate the chart.
    End With

    'Clean up.
    Set gChart = Nothing
    Set ppChart = Nothing
    Set ppSlide = Nothing
    Set ppPres = Nothing
    Set ppApp = Nothing
    End Sub</pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  4. #4
    Star Lounger
    Join Date
    Apr 2002
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Accessing charts in a presentation (VB,VBA/Powerpoint/W2000)

    Sam,

    Thanks a lot for being always there to help. Your examples works great.

    Thanks again

Posting Permissions

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