none
manipulate selected members of a chart via VBA in Powerpoint RRS feed

  • Question

  • Hi,
    I'm currently facing this obstacle:
    I want to apply some doing to a member of a chart object in powerpoint, by selecting it via click and then running a macro. Let's say it's the x-axis-label, but could be any other label / legend or whatever you can insert via menu bar.
    The macro is NOT supposed to be used for simple shapes created inside a chart. Problem is, that I can't get the selected object, only the parent chart.
    I find it also odd, that I can't select items inside a chart via macro without selecting the parent chart shape first. any ideas on how to get the selected object?

    Thanks!

    Julian

    Example:

    Option Explicit
    Sub test()
    
    Dim sld As Slide
    Dim crt As Shape
    Dim leg As Legend
    Dim xlab As AxisTitle
    
    Set sld = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, ppLayoutBlank)
    sld.Select
    Set crt = sld.Shapes.AddChart(xlXYScatter)
    
    'add labels
    crt.Chart.HasLegend = True
    Set leg = crt.Chart.Legend
    crt.Chart.Axes(1).HasTitle = True
    Set xlab = crt.Chart.Axes(1).AxisTitle
    crt.Chart.Axes(2).HasTitle = True
    crt.Chart.HasTitle = True
    
    'select objects in chart and check what is selected
    
    'does not work:
    leg.Select
    
    'works, chart has to be selected first
    crt.Select
    leg.Select
    If ActiveWindow.Selection.ShapeRange(1) Is crt Then
        Debug.Print ("it's the chart")
    End If
    
    'works, chart has to be selected first
    crt.Select
    xlab.Select
    If ActiveWindow.Selection.ShapeRange(1) Is crt Then
        Debug.Print ("it's the chart")
    End If
    
    End Sub



    • Edited by muluman88 Thursday, July 23, 2015 6:56 AM better title
    Thursday, July 23, 2015 6:55 AM

All replies

  • The legend represents in a chart. Each chart can have only one legend, and PowerPoint has made the legend and the chart integral to each other. So you can try like below:

    Dim shp As Shape
    Set shp = ActivePresentation.Slides(1).Shapes.AddChart(xl3DPie)
    
    Dim cht As Chart
    Set cht = shp.Chart
    With cht.Legend
        ......
    End With
    

    Monday, July 27, 2015 9:00 AM
  • If I understand the question, you may be able to use the GetChartElement method of the chart object.

    • Proposed as answer by David_JunFeng Thursday, August 13, 2015 9:14 AM
    Monday, July 27, 2015 6:50 PM
  • Thanks for your suggestions! Problem is, that I do not create the chart nor the legend via vba, I also don't know the position of the embedded object I want to manipulate.

    I want to write something like the "Chart Alignment Add-in" but for powerpoint. My planned script has to work with the Selection-object only, and all I can get so far is the chart itself, but not the object selected inside the chart.

    My example code illustrates this: after selecting the legend, I check the Selection object to see what it represents and it's always the chart and not the selected contained object like legend or x-label.

    Any thoughts?

    Tuesday, July 28, 2015 6:35 AM
  • As far as I know that the chart will be selected when you manually select the object inside the chart in PowerPoint. So the selection object is always the chart and not the selected contained object like legend or x-label.

    It seems to be a issue of office. Since the issue is complex, I suggest you contacting Microsoft support to raise an incident so that our engineer could work closely with him to identify the root cause and resolve this issue as soon as possible. If the support engineer determines that the issue is the result of a bug the service request will be a no-charge case and you won't be charged. Please visit the below link to see the various paid support options that are available to better meet your needs.
    http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone


    Friday, July 31, 2015 1:13 AM