Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 07-05-2012, 10:13 AM
LKnomad LKnomad is offline Windows XP Office 2007
Novice
 
Join Date: Jul 2012
Posts: 8
LKnomad is on a distinguished road
Question 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
This should be so simple but I cannot figure it out. I get a variety of errors when I try different options.
Reply With Quote
  #2  
Old 07-05-2012, 01:28 PM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,666
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
This could be reduced to:
Code:
Sub ChangePieChart()
Selection.ShapeRange(1).Height = 250
End Sub
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 07-05-2012, 01:38 PM
LKnomad LKnomad is offline Windows XP Office 2007
Novice
 
Join Date: Jul 2012
Posts: 8
LKnomad is on a distinguished road
Default

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...
Reply With Quote
  #4  
Old 07-05-2012, 01:46 PM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,666
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

That tells me you didn't have a shape object selected when you ran the code. Since you originally said you're:
Quote:
trying to make a simple change to a single selected chart in a word document using VBA
I didn't add any error-checking.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #5  
Old 07-05-2012, 01:51 PM
LKnomad LKnomad is offline Windows XP Office 2007
Novice
 
Join Date: Jul 2012
Posts: 8
LKnomad is on a distinguished road
Default

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.
Reply With Quote
  #6  
Old 07-05-2012, 01:54 PM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,666
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
  #7  
Old 07-05-2012, 02:08 PM
LKnomad LKnomad is offline Windows XP Office 2007
Novice
 
Join Date: Jul 2012
Posts: 8
LKnomad is on a distinguished road
Default

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
Reply With Quote
  #8  
Old 07-05-2012, 02:13 PM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,666
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
  #9  
Old 07-05-2012, 02:18 PM
LKnomad LKnomad is offline Windows XP Office 2007
Novice
 
Join Date: Jul 2012
Posts: 8
LKnomad is on a distinguished road
Default

That did it - thanks!

But why is it so complecated?
Reply With Quote
  #10  
Old 07-05-2012, 02:21 PM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,666
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
  #11  
Old 07-05-2012, 02:22 PM
LKnomad LKnomad is offline Windows XP Office 2007
Novice
 
Join Date: Jul 2012
Posts: 8
LKnomad is on a distinguished road
Default

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
Reply With Quote
Reply

Thread Tools
Display Modes


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


All times are GMT -7. The time now is 04:32 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft