Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VBA Powerpoint Selecting text boxes

Posted on 2014-12-10
4
Medium Priority
?
3,120 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
Jamie Garroch earned 2000 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 12

Assisted Solution

by:Jamie Garroch
Jamie Garroch earned 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

618 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