Solved

VBA Powerpoint Selecting text boxes

Posted on 2014-12-10
4
1,927 Views
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?

Thanks!
0
Comment
Question by:Eddie_Aeffect
  • 2
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
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
oSel.Select
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"
  Next
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
  Next
End Function

Open in new window

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

Author Comment

by:Eddie_Aeffect
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.
0
 
LVL 9

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!
0
 

Author Closing Comment

by:Eddie_Aeffect
ID: 40496452
Quick response time, perfect answer.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In case Office 2010 has not been deployed in your environment, this article may be quite useful. In our office, we wanted a way to deploy Microsoft Office Professional Plus 2010 through an automated batch file via logon script. This article is docum…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
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":…
Not only does Prezi allow you to create non-linear presentations, it also makes it easy to apply transition animations between your frames. Learn how to apply a fade-in transition to select items and entire frames in this tutorial Select "Edit Path"…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now