PDA

View Full Version : Create a shape with a predefined format?



elunicotomas
04-10-2012, 10:21 AM
Hi there, Im trying to create a macro that paste "flags" (shapes of different colors) to clasify the slides of a presentation.

I´m totally new to VBA. I managed to create a rectangular shape in the TOP RIGHT corner of the current slide, but it create a blue shape with blue border, and i want this shape to be red, 35% transparency and no border.

Is there any way to pre-define the format of the shape before creating it? or should I change the shape´s format once it´s created?

Thanks!

Sub AZ_Banderitas()
'if there´s no slide selected the shape will be pasted in the previous slide
On Error Resume Next
Err.Clear
Debug.Print "The current slide is Slide " & ActiveWindow.View.Slide.SlideIndex
If Err <> 0 Then
ActiveWindow.ViewType = ppViewNotesPage
ActiveWindow.ViewType = ppViewNormal
Debug.Print "The current slide is Slide " & ActiveWindow.View.Slide.SlideIndex
End If

'creates shape
slideLocation = ActiveWindow.View.Slide.SlideIndex

myDocument.Shapes.AddShape Type:=msoShapeRectangle, Left:=570, Top:=0, width:=150, Height:=100

End Sub

elunicotomas
04-10-2012, 10:48 AM
I´ve changed the code a bit..

Sub AZ_Banderitas()
'if there´s no slide selected the shape will be pasted in the previous slide
On Error Resume Next
Err.Clear
Debug.Print "The current slide is Slide " & ActiveWindow.View.Slide.SlideIndex
If Err <> 0 Then
ActiveWindow.ViewType = ppViewNotesPage
ActiveWindow.ViewType = ppViewNormal
Debug.Print "The current slide is Slide " & ActiveWindow.View.Slide.SlideIndex
End If

'creates shape
slideLocation = ActiveWindow.View.Slide.SlideIndex

Set myDocument = ActivePresentation.Slides(slideLocation)

Dim shp As Shape

Set shp = myDocument.Shapes.AddShape(msoShapeRectangle, 570, 0, 150, 100)

End Sub

Cosmo
04-10-2012, 03:39 PM
Creating a new shape will use the default properties for that shape. If you want it to always be a certain color (or change any other property) when you run the code, then you should set the properties after creating it.
Dim shp As Shape

Set shp = myDocument.Shapes.AddShape(msoShapeRectangle, 570, 0, 150, 100)
shp.Fill.ForeColor.RGB = RGB(255, 0, 0)


If you have multiple properties you can set them like this:

Dim shp As Shape

Set shp = myDocument.Shapes.AddShape(msoShapeRectangle, 570, 0, 150, 100)
with shp
.Fill.ForeColor.RGB = RGB(255, 0, 0)
.Height = 200
'... and any other properties here
end with



Otherwise, your script will always use whatever are set as the default shape properties when the script runs.

elunicotomas
04-11-2012, 08:50 AM
Creating a new shape will use the default properties for that shape. If you want it to always be a certain color (or change any other property) when you run the code, then you should set the properties after creating it.
Dim shp As Shape

Set shp = myDocument.Shapes.AddShape(msoShapeRectangle, 570, 0, 150, 100)
shp.Fill.ForeColor.RGB = RGB(255, 0, 0)


If you have multiple properties you can set them like this:

Dim shp As Shape

Set shp = myDocument.Shapes.AddShape(msoShapeRectangle, 570, 0, 150, 100)
with shp
.Fill.ForeColor.RGB = RGB(255, 0, 0)
.Height = 200
'... and any other properties here
end with



Otherwise, your script will always use whatever are set as the default shape properties when the script runs.

Thanks! That worked, now im trying to put some text in those boxes...

Set myDocument = ActivePresentation.Slides(slideLocation)

Dim shp As Shape

Set shp = myDocument.Shapes.AddShape(msoShapeRectangle, 570, 0, 150, 100)

With shp.Fill
.Transparency = 0.35
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 0, 0)
.Solid
End With

With shp.Line
.Visible = msoFalse
End With

Cosmo
04-11-2012, 10:04 AM
shp.TextFrame.TextRange.Text = "Enter your text here"

elunicotomas
04-11-2012, 11:27 AM
shp.TextFrame.TextRange.Text = "Enter your text here"


Thanks again!