PDA

View Full Version : Solved: Formatting dynamically added Textboxes



badbee
01-25-2006, 12:20 PM
I am adding textboxes to a slide by using AddTextBox method.

Now i want to format these textboxes according to the chosen template (which may vary according to user's selection).

Currently the textboxes contain some default styles. but i want to be the same as defined in the SlideMaster. for example, if the MasterSlide's body text is Arial 12 Bold, then i want the dynamically added textbox to be following the same conventions....

Any ideas ??

Killian
01-26-2006, 03:13 AM
Hi badbee and welcome to VBAX :hi:

Here's some code that shows the basic method - as you can see, by referring to the placeholder object on the slidemaster, you can get its properties and use them to set up the new textbox.
Sub AddCustomTextbox()

Dim objTextBox As Shape
Dim objMasterTextBox As Shape
Dim shp As Shape

'get a reference to the body text placeholder on the slide master
For Each shp In ActivePresentation.SlideMaster.Shapes.Placeholders
If shp.PlaceholderFormat.Type = ppPlaceholderBody Then
Set objMasterTextBox = shp
Exit For
End If
Next

If objMasterTextBox Is Nothing Then
MsgBox "No body textbox found on slidemaster"
Else
'add textbox with size
Set objTextBox = ActiveWindow.Selection.SlideRange.Shapes.AddTextbox( _
msoTextOrientationHorizontal, 50, 50, 400, 24)
With objTextBox.TextFrame
.AutoSize = ppAutoSizeShapeToFitText
.TextRange.Text = "[Enter Text]"
.TextRange.ParagraphFormat.Bullet.Visible = msoFalse
End With
'match format with that of objMasterTextBox
'use the first paragraph, in case there is a mixture in the placeholder
With objTextBox.TextFrame.TextRange.Font
.Name = objMasterTextBox.TextFrame.TextRange.Paragraphs(1).Font.Name
.Size = objMasterTextBox.TextFrame.TextRange.Paragraphs(1).Font.Size
.Bold = objMasterTextBox.TextFrame.TextRange.Paragraphs(1).Font.Bold
.Italic = objMasterTextBox.TextFrame.TextRange.Paragraphs(1).Font.Italic
End With
'set more properties as required...

End If
objTextBox.TextFrame.TextRange.Characters.Select

End Sub
There are a lot of approaches to building this into custom functionality. If you frequently want to add pre-defined shapes (dynamic or otherwise), it might be better to create a class module and use the code it's Initialize event - then each time you want a new shape, just create an instance of that class.

badbee
01-26-2006, 06:20 AM
:clap:Thanks,
The problem is solved!!:bow:
Cheers! :thumb