#1
|
|||
|
|||
simple change chart in VBA
I am trying to make a simple change to a single selected chart in a word document using VBA. I cannot seem to make this work. Any help appreciated.
The code that I have been trying looks like this: Code:
Sub ChangePieChart() Dim objshape as chart objshape.Height = 250 End Sub |
#2
|
||||
|
||||
Hi LKnomad,
You haven't told your macro what objshape is. Try: Code:
Sub ChangePieChart() Dim objShape As Shape Set objShape = Selection.ShapeRange(1) objShape.Height = 250 End Sub Code:
Sub ChangePieChart() Selection.ShapeRange(1).Height = 250 End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thanks for helping out. When I run your code I get the following error
"Invalid procedure call or agrument" The error points to Set objShape = Selection.ShapeRange(1) I get an error using either version of your code. VBA is going to kill me... |
#4
|
||||
|
||||
That tells me you didn't have a shape object selected when you ran the code. Since you originally said you're:
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
I had the chart selected when I ran the code. Is there an alternate code - or some kind of error checking I can add in.
|
#6
|
||||
|
||||
In that case, your chart may be formatted as an inline shape. Try changing:
Dim objShape As Shape to: Dim objShape As InlineShape
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
This is my code at this time - Same error
Sub individualPieChart() Dim objShape As InlineShape Set objShape = Selection.ShapeRange(1) objShape.Height = 250 end sub |
#8
|
||||
|
||||
Try:
Code:
Sub ChangePieChart() With Selection If .ShapeRange.Count > 0 Then .ShapeRange(1).Height = 250 ElseIf .InlineShapes.Count > 0 Then .InlineShapes(1).Height = 250 End If End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
That did it - thanks!
But why is it so complecated? |
#10
|
||||
|
||||
It's not complicated, just flexible and error-proof. The code now works with both in-line and floating shapes and won't generate an error if none is selected.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
Nevermind I simplified it. Thanks Paul, you're the best!
Sub ChangePieCharttest() With Selection .InlineShapes(1).Height = 200 .InlineShapes(1).Width = 465 End With End Sub |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
It has to be simple...formula | ClearSkies | Excel | 1 | 03-04-2011 01:06 PM |
Simple or what? | nebb | Publisher | 0 | 09-23-2010 07:00 AM |
Converting manual chart to bar chart? | aligahk06 | Excel | 0 | 07-03-2010 12:23 PM |
Simple formulas | den hobbelder | Word | 1 | 02-26-2010 01:53 PM |
Chart colors change during slideshow | chickasaw | PowerPoint | 0 | 02-06-2010 06:08 PM |