View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Luca Brasi Luca Brasi is offline
external usenet poster
 
Posts: 28
Default Multiple chart selection in Excel 2007

Andy, many thanks! Using FOR EACH works for me too!
Luca


"Andy Pope" wrote:

Hi,

Looks like a bug with VBA and the OM.
If you step through your code and add ActiveWindow.Selection into the Watch
Window, you can see the correct names of the chart objects under each Item.
But using the code to access it returns different information.

This revised code worked for me.

Sub Test()
Dim obj As Object

If Not ActiveChart Is Nothing Then
' toggle legend to show which charts are processed
ActiveChart.SetElement IIf(ActiveChart.HasLegend, _
msoElementLegendNone, msoElementLegendBottom)
Else
For Each obj In Selection
If TypeName(obj) = "ChartObject" Then
' toggle legend to show which charts are processed
obj.Chart.SetElement IIf(obj.Chart.HasLegend, _
msoElementLegendNone, msoElementLegendBottom)
End If
Next
End If

End Sub


Cheers
Andy

--

Andy Pope, Microsoft MVP - Excel
http://www.andypope.info
"Luca Brasi" wrote in message
...
My add-in should process all charts the user currently has selected. Now I
ran into a problem when the user has selected several charts (but not
all!)
on a worksheet.
It looks to me as if there is a bug in the "Selection" object in Excel
2007.

Let's say I select two out of three charts on a worksheet and run the
sample
code below. Instead of the selected charts, the first two charts I
inserted
into the worksheet are processed.

Am I doing something wrong or can someone confirm this is a bug in Excel
2007? Knows someone a workaround? Thanks for any hints, Luca

'*** Sample code
Sub Test()
Dim i As Long
Dim obj As Object
If Windows.Count 0 Then
If TypeName(ActiveSheet) = "Worksheet" Then
If TypeName(ActiveWindow.Selection) = "DrawingObjects" Then
For i = 1 To ActiveWindow.Selection.Count
Set obj = ActiveWindow.Selection(i)
If TypeName(obj) = "ChartObject" Then
' toggle legend to show which charts are processed
obj.Chart.SetElement IIf(obj.Chart.HasLegend, _
msoElementLegendNone, msoElementLegendBottom)
End If
Next i
End If
End If
End If
End Sub
'*** End of code