VBA for altering PowerPoint 2003 Presentations- Active not new
-
20-09-2019 - |
Question
If I set up a template on PowerPoint slides, that contain all the text boxes I need, the what Visual Basic do I use to enter text that I want into those text boxes?
It is easier for me to use a template, because these ppt briefs contain (or need to contain) a lot of data:
- how do I enter text into those text boxes
- how do you alter the font, size, fontbold, etc of a particular text box?
- is there a way for determining the "name" of a text box/shape ("text box 52") other than recording macros after macros and selecting the text box, just to get the object name from selection?
- if I use a macro to determine the vba, why will it not work to use that as vba? the vb I get from a macro seems to have active.selection which just doesn't seem to work as a vba procedure because it doesn't know what to select???? i think
what i am looking to do is create the end game ppt from access, using the template. on an access form I want to have multiple text boxes that relay the information into the ppt text boxes on the slide.
i know how to launch a template (or new presentation) from access, and how to add new items (slides, graphs, charts, text) but I do not know how to alter pre-existing text boxes!!
Please help....my work is needing this like yesterday!
Thanks as always!
Solution
You can access shapes by name, as in:
Dim oSlide As Slide
Set oSlide = ActivePresentation.Slides(1)
Dim oShape As Shape
Set oShape = oSlide.Shapes(strShapeName)
Dim oTextRange As TextRange
Set oTextRange = oShape.TextFrame.TextRange
oTextRange.Text = "this is some text"
oTextRange.Font.Bold = msoTrue
Note that whatever you're looking to do, just record a macro of you doing it via the UI, and copy that. You're right that the recorded macro will use the Selection
object a lot, but that's easily fixed - just get a reference to the appropriate Shape
object (or whatever) and then subsitute that in the generated code.
So, for example, if the recorded macro for changing the fill color of a shape is this:
With ActiveWindow.Selection.ShapeRange
.Fill.Visible = msoTrue
.Fill.Solid
.Fill.ForeColor.RGB = RGB(255, 0, 0)
.Fill.Transparency = 0#
End With
... and you want to apply the fill color to a shape which you already have a reference to as oShape
, then change the code to this:
With oShape
.Fill.Visible = msoTrue
.Fill.Solid
.Fill.ForeColor.RGB = RGB(255, 0, 0)
.Fill.Transparency = 0#
End With
To get the current name of a shape, you can enter this in the "immediate" window in the VBA editor:
?ActiveWindow.Selection.ShapeRange(1).Name
You could turn that into a (single) macro very easily:
Sub ShowMeTheName()
MsgBox ActiveWindow.Selection.ShapeRange(1).Name
End Sub
Note that I would personally rename the shapes to something meaningful rather than using the default names. Simply turn do this in the immediate window:
ActiveWindow.Selection.ShapeRange(1).Name = "MyName"
...or create a macro to prompt for a name.
OTHER TIPS
When I need to automate Word or Excel (nobody I know or work with has any use for PowerPoint), I open the app in question, turn on Macro recording and do the task I want to automate. Then I use the generated code as the basis for my Access code. It's often a pretty straightforward process, sometimes as simple as copying and pasting and then prefacing each line with the application object I'm using from Access.
If I had to do what you're doing, that's exactly how I'd start, by doing recording the task being performed interactively, and then experimenting with what part of the code is essential.