VBA Powerpoint Selecting text boxes

Posted on 2014-12-10
Last Modified: 2014-12-12
Thanks to expert Jamie Garroch I created two macros, one to rename all the text boxes on a slide and one to tab though them based on their name. The problem is to use it now I have to have people first run the macro to rename all the text boxes and then have them run the macro to tab through them. I think a better solution is to rewrite the two macros into one so that a user can select the text boxes and run it. Do that that I added some code to add SELECTION to the .ALTERNATIVETEXT property of the selected text boxes. Then the macro runs to rename all the text boxes. Finally it should reselect those boxes that have SELECTION in .ALTERNATIVETEXT. How do I select all the text boxes that have SELECTION in .ALTERNATIVETEXT so I can run the rest of my macro?

Question by:Eddie_Aeffect
  • 2
  • 2
LVL 10

Accepted Solution

Jamie Garroch earned 500 total points
ID: 40492675
Thanks for the kind comment Eddie. Why not set a reference to the user selection at the beginning, do your actions and then reselect the original set of shapes when done?

Something like this:
Dim oSel as ShapeRange
Set oSel = ActiveWindow.Selection.ShapeRange
' do your actions
Set oSel = Nothing

Open in new window

If you want to identify objects by code, it's best to use a mechanism not exposed to users via the PowerPoint UI (because they can break your functionality) and that mechanism is Tags:
' Add a tag to each shape in the user selection
Public Sub AddTagToSelection()
  Dim oShp As Shape
  For Each oShp In ActiveWindow.Selection.ShapeRange
    oShp.Tags.Add "MY_TAG_NAME", "MY_TAG_VALUE"
End Sub

Open in new window

And to read them back:
' Pass a shape object, a tag name and value and the function returns true if that
' tag exists for the shape
Public Function ShapeHasTag(oShp As Shape, TagName As String, TagValue As String) As Boolean
  Dim counter As Integer
  For counter = 1 To oShp.Tags.Count
    If oShp.Tags.Name(counter) = TagName Then _
      If oShp.Tags.Value(counter) = TagValue Then ShapeHasTag = True: Exit Function
End Function

Open in new window

You need to take care of groups but this works independently of user edits.

Author Comment

ID: 40496314
It's great to have this site where people can go to get answers and a quick response. I use it almost exclusively since I switched over from my days writing VB6 programs to writing macros for Excel, Powerpoint and Word. I will play around with this code when I get a chance. As for why I don't set a reference to the selected objects and then return to those objects, since I wrote my macro to change the names of all text boxes on a slide, it has to select each text box so of course the text boxes my users selected get unselected. The ideal solution of course would be if I could rename all the text boxes on a slide without having to select them but I didn't think that was possible.
LVL 10

Assisted Solution

by:Jamie Garroch
Jamie Garroch earned 500 total points
ID: 40496430
Eddie, you do need to select objects to perform certain method operations such as grouping/ungrouping, aligning etc. but you don't need to when changing properties such as name, tags, alt text, position, size, text content etc. You don't even need the slide on which the shapes appear to be in view. So you can do this without the shape in view or selected:

' Reference the shape by its index
ActivePresentation.Slides(1).Shapes(1).Name = "my shape"
' Reference the shape by its name
ActivePresentation.Slides(1).Shapes("Rectangle 1").Name = "my shape"

Open in new window

But, because your macro starts with a user selection, you "could' store that selection as per my code example above and then do whatever you want, unselecting and selecting various shapes, before restoring the selection at the end of your code.

Good luck!

Author Closing Comment

ID: 40496452
Quick response time, perfect answer.

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Learn how to create interesting presentations by including videos to keep your audience engaged using Prezi. Select "Insert" from the top menu in your Prezi editor: Select "YouTube Video": Paste the video URL into the prompt: "Select "Insert":…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question