Renaming Objects in PowerPoint - vba

Probably a very stupid question but I can't figure how to rename an object in PowerPoint.. For example, all my Graphs are called by default "Graph 1" etc.
Could someone help me on that?
Thanks!

In PowerPoint 2007 you can do this from the Selection pane.
To show the Selection pane, click on the Home tab in the ribbon, then click on Arrange and then 'Selection Pane...' at the bottom. The Selection pane will open on the right. (Or press CTRL+F10)
To rename an object, first select the object and then double click on the object name in the Selection pane and you will be able to type the new object name.

(This answer assumes you are merely assigning more meaningful names during development, so your other code that references the objects can be more readable).
Put the code below into a sub, then run it from the slide in question. Each shape will be selected in turn, so you can see what shape is being referenced. An input box will tell you the current name and ask you for a new name. If you cancel or OK a zero-length input, the old name will stay in place. There is no name entry validation in this code, so be sure you type only valid names. After running it once, you can run it again just to check that the names you typed in the first round were applied to the object you intended.
The loop will cover all objects on the current slide, so if you want to process multiple slides, you have to run this separately on each slide. Every object on the slide is considered: title, drawing objects, groups, embedded pictures, equations, etc. etc. - just don't type a new name for objects that you don't care.
After your development is finished, best hide (Private Sub) or erase this code, so your users don't change object names by mistake.
Dim s As Integer, NewName As String
With ActiveWindow.Selection.SlideRange
For s = 1 To .Shapes.Count
.Shapes(s).Select ' So you can see the object in question
NewName = InputBox(.Shapes(s).Name) ' Tell what current name it is and ask for new name
If Len(NewName) > 0 Then .Shapes(s).Name = NewName ' If you typed a new name, apply it
Next s ' 1 To .Shapes.Count
End With ' ActiveWindow.Selection.SlideRange

Thanks for your help but actually I am just doing it using VBA...
ActiveWindow.Selection.ShapeRange(1).Name = "newname"
Cheers

Related

PowerPoint VBA add shape event/ add shape with tag value

i'am currently trying to add a small function to PowerPoint using VBA, my goal is to create a gadget that works kind of like Photoshop graphics layer.
My plan is to add a layer name Tag on each shape that is drawn by user, so later I can parse through every item by loop and preform lock/unlock, show/hide shapes based on it's tag value, such as:
Sub add_shape_with_layer_tag()
Set islide = ActivePresentation.Slides(1)
Set ishape = islide.Shapes.AddShape(msoShapeRectangle, 5, 5, 80, 60)
ishape.Tags.Add "Layer", "1"
End Sub
Sub show_hide_layer_one_shapes()
Dim active_slide As Slide
Set active_slide = ActiveWindow.View.Slide
For Each ishape In active_slide.Shapes
If ishape.Tags("Layer") = "1" Then
ishape.Visible = Not (ishape.Visible)
End If
Next ishape
End Sub
However, I couldn't found a way to achieve this, so I would like to ask whether there's a method that provides following functions?
override add shape function so I can sneak tag value in to shape every time user drawn a shape
catch add shape event (if this thing did exist) so i can add tag to the last added item
a way to set a default tag value to shape, just like setting default shape color/line width
or is there any better options that is also viable?
thanks.
20200728
To John, thanks for the advice, I did found some interesting event that might be helpful for some of my other projects, however I couldn't found events that able to trigger after custom function while shape is added.
To Steve, my plan is to add a modeless userform with list UI that manage layers, the shape tag and shape fill texture/color will be determined based on what list item that currently selected.
As to saving settings, I'll use VBComponents.CodeModule to dump existing settings in userforms to a VBA module and store as text, so in theory I should able to make this function self contained in one file.
Not totally an answer, sorry SO, but comments don't allow enough scope for this. So ...
To Steve, my plan is to add a modeless userform with list UI that manage layers, the shape tag and shape fill texture/color will be determined based on what list item that currently selected.
Ah, so you're creating your own "pseudo-layers". That wasn't clear. Thanks for the add'l info. As it happens, I have a selection manager add-in that works very similarly to what you're proposing.
To John, thanks for the advice, I did found some interesting event that might be helpful for some of my other projects, however I couldn't found events that able to trigger after custom function while shape is added.
The SelectionChange event should get you there. When it fires, you'll need to check first to see if the current selection is a shape or something else. If a shape, check to see if its .Index = current slide's .Shapes.Count and also to see if you've already tagged it. If no tag AND if it's the correct index, it'll be a newly added shape. If you're tagging ALL shapes, you may only need to check to see if the .Tag(name) is blank.
As to saving settings, I'll use VBComponents.CodeModule to dump existing settings in userforms to a VBA module and store as text, so in theory I should able to make this function self contained in one file.
Why not save any needed slide or presentation level info as further slide- or presentation-level tags? PPT can absorb quite a lot of info as tags w/o getting cranky.

How to find the label of a shape on a Powerpoint slide

Can someone please point me in the right direction to finding the label Powerpoint uses for each shape, active x control, etc... I've researched till I'm blue in the face. Here's what I'm talking about:
I have a very simple slideshow that asks a question in a text box. Just below the question I placed an active x control text box to get the user's answer. All I want to do is take the answer and append it to a text file. Here's the code:
Public Sub WriteAnswerToFile(slideNum As Integer, shapeNum As String)
Dim filePath As String
Dim objFSO As FileSystemObject
Dim objFile As Variant
filePath = "C:\Batch Files\Powerpoint\ButtonPushes\AnswerFile.txt"
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Batch Files\Powerpoint\ButtonPushes\AnswerFile.txt", ForAppending)
objFile.WriteLine (Application.ActivePresentation.Slides(slideNum).Shapes(shapeNum).TextFrame.TextRange)
objFile.Close
End Sub
From each slide, I pass the slide number and the shape designator to the sub routine. The shapeNum is a string because I found that it worked (no better reason than that) for me to pass something like "TextBox 1" as the param. It all works absolutely great if I know the Shape(designator); as in Shape(TextBox1). For the life of me I cannot figure out how to pass the text input by the user into the active x control text box. I have no idea what the designator is. The property sheet for the text input box calls it TextBox1. The Code sheet calls it TextBox1. When I pass that parameter, it prints the question I asked, not the answer, to my text file. I don't know how to call the input text box in my code.
My code prints "How did you hear about us?" to my text file when my code runs with Call WriteAnswerToFile(2, "TextBox 1"). I'm sure that textbox is called textbox 1; it's the first textbox on the slide. I just don't know what Powerpoint labels the user input box.
If there is some sort of Powerpoint scripting or layout page that will define all the shapes in the slidewhow, I'd sure like to be pointed that direction.
You just need to refer to the form part of the Object since this is not a normal shape.
objFile.WriteLine (Application.ActivePresentation.Slides(slideNum).Shapes(shapeNum).OLEFormat.Object.Text)
To clarify from your comment. I think that you have two "Text Box 1". One is "TextBox 1" (note the space) which is the normal textbox and the ActiveX control is "TextBox1" (no space).
If I create a new blank slide and first add a normal text box and then an ActiveX textbox and then run the following code:
For Each shp In Slide2.Shapes
Debug.Print shp.Name
Next shp
The Immediate window will show the following:
TextBox 1
TextBox1
By chance I happened to stumble upon the simplest answer to my question of "finding the label Powerpoint uses for each shape, active x control, etc.."
I am using the Office 16 (Office 365) suite that contains Powerpoint and would not be certain this feature is avaiable to other versions.
On the Home tab in Powerpoint there is an Editing submenu that contains a Select function. As pictured here:
When you click on Select, another submenu appears that shows a Selection Pane function. If you click on that, the selction pane shows up on the right hand of the screen. In that pane you will see all of the objects on the current slide and the names Powerpoint has given to each of them.
It shows there, the discrepancy I was having with calling TextBox(Space)1 and TextBox(NoSpace)1.
This works to be much more expedient for me to grab the name of the shape I want to call in my VBS Scripts.
I am thankful; however, for the time and frustration #Diederik Sieburgh saved me in allowing me to move forward with my project as it is now 2 weeks later that I stumbled upon this information.

How to assign Textshape 1 as the title?

With Office 2016 PPT, after sharing a PPT file and getting it back, it now has retained titles on the slides but they are now "TextShape 1", are in the correct location (top of slide), and do not show up as titles in the outline view, etc.
Merely resetting the slides overlays empty boxes.
How can I make "TextShape 1" on each of the slides to be seen as the Title for the respective slides? A redefinition? Add a new Title box with the content of "TextShape 1", and delete the old box?
Thanks for the views.
It was not easy as a vba-powerpoint beginner finding my way through the collections and terminology, and certainly time consuming, but I wrote a solution for the limited problem as presented.
You might think you could add a title object, but at least the way I tried, it complained that that could not be done with the current slide layout. I could not figure out how to designate a text box as a Title. So I used vba to set the formats to Titleonly for slides without one, copied the text into it, then deleted the old shape that was overlaid with the new one as below:
Attribute VB_Name = "Module1"
Sub newtitles()
Dim s As Variant
For s = 1 To ActivePresentation.Slides.Count
If Not ActivePresentation.Slides(s).Shapes.HasTitle Then
ActivePresentation.Slides(s).Layout = ppLayoutTitleOnly
' adds a title placeholder too; could not .addtitle to slide without a title in format
ActivePresentation.Slides(s).Shapes.Title.TextFrame.TextRange.Text = ActivePresentation.Slides(s).Shapes(1).TextFrame.TextRange.Text
ActivePresentation.Slides(s).Shapes(1).Delete ' remove redundant box
End If 'about title
Next ' slides
End Sub

Excel: Object Text Box can't do Carriage Returns in Protected vs. Unprotected Sheet states (w/ Text Unlocked)...why?

First and foremost, my due diligence rounded up a ton of answers regarding ActiveX Text Boxes, but nothing really about Object Text Boxes. For my project, I cannot use any ActiveX.
OK, so when a Sheet is Unprotected, an Object Text Box (from Insert > Shapes) works pretty much the way I want it to: the most important thing being that I can hit the Enter key and get a carriage return. Then, I go into the Shape Properties, and uncheck Lock Text, and protect the sheet.
Once the sheet is protected, though, the ability to do carriage returns (type Enter, and go down one line) goes away. Shift+Enter and Alt+Enter are no-goes as well.
Is it just not possible to have this functionality available? Are there any workarounds? Why does Excel hate me? Here are some of my ideas:
Unprotect Sheet when Text Box is clicked/activated, Protect when not
(couldn't figure out the syntax in VBA for this. "If Intersect..." is what I'm thinking)
Insert Word Doc Object (don't like this because one-click enters the
formula bar editing, and I can't get the font to stay)
Just use a merged cell and instruct users to double-click to enter
and use Alt+Enter for a new line.
The winning option for now is using a merged cell, but I may just have to see if ActiveX will work on our network. I really want to stick to the KISS principle here if at all possible for the end user...I don't mind coding in the backend to make it work, though.
Thank you for your thoughts!
EDIT: Here's some images to help...
Here's the functionality that I would like to have when my Sheet is protected:
Next, this is an ActiveX text box with it's properties window displayed (Developer > Design Mode > Properties). The properties that make it somewhat usable when the Sheet is protected are circled in red, the Multiline and Enter Key Behavior. But again, I'd prefer to not have to use ActiveX...plus, the user cannot change font color by line.
Finally, I found this interesting: There is another Text box under Form Control that is grayed out. From a search, it looks like this was taken away in favor of the drawing objects version of the text box...or maybe it's the same? The left is the drawing objects one, the middle is the grayed out Form Control, and the right is the ActiveX.
In sum, I would just like to see if there is a way to have the functionality of an unprotected Sheet's Shapes Text Box when the sheet is protected.
Assuming you are working with a TextBox shape, inserted from the ribbon, here:
Then you can use the optional parameter in the Protect method:
Sheet1.Protect DrawingObjects:=False
This will allow the user to edit text boxes on the worksheet, but the sheet itself will remain protected.
If you are using a Form Control (inserted from the Develper/Design ribbon) then you can set the .MultiLine property by accessing the shape's OLEFormat.Object:
Sub test()
Dim tb As Shape
Dim x As Object
'Get a handle on the SHAPE
Set tb = ActiveSheet.Shapes(2)
'You have to access its properties from the OLEFormat.Object:
tb.OLEFormat.Object.Object.MultiLine = True
End Sub
In my test, even on a Protected worksheet this allows the user to Shift + Enter to insert carriage returns:
Ctrl + Shift + Enter seems to work on a protected sheet.

How to name an object within a PowerPoint slide?

So I know how to name a textbox, or a like object in PowerPoint with VB, but I was wondering if there was a way to name objects through the Ribbon (PowerPoint 2007). For instance, if I add a text box onto a slide, is there a way to assign it a name (sort of like the properties window in access, or the textbox in Excel 2003 at the top left side where you can enter the name)?
Basically so I can reference it in code later; without having to use code to name each and every object i add after the fact. Perhaps an easier way through the Ribbon?
PowerPoint for Windows:
Click on the object (textbox, shape, etc.) to select it.
In the Drawing Tools | Format tab, click on Selection Pane in the Arrange group.
From there, you'll see names of objects.
Double click (or press F2) on any name and rename it. By deselecting it, it becomes renamed.
You can also get to this from the Home tab -> Drawing group -> Arrange drop-down -> Selection pane or by pressing ALT + F10.
Select the Object -> Format -> Selection Pane -> Double click to change the name
While the answer above is correct I would not recommend you to change the name in order to rely on it in the code.
Names are tricky. They can change.
You should use the ShapeId and SlideId.
Especially beware to change the name of a shape programmatically since PowerPoint relies on the name and it might hinder its regular operation.
THIS IS NOT AN ANSWER TO THE ORIGINAL QUESTION, IT'S AN ANSWER TO #Teddy's QUESTION IN #Dudi's ANSWER'S COMMENTS
Here's a way to list id's in the active presentation to the immediate window (Ctrl + G) in VBA editor:
Sub ListAllShapes()
Dim curSlide As Slide
Dim curShape As Shape
For Each curSlide In ActivePresentation.Slides
Debug.Print curSlide.SlideID
For Each curShape In curSlide.Shapes
If curShape.TextFrame.HasText Then
Debug.Print curShape.Id
End If
Next curShape
Next curSlide
End Sub
Click Insert ->Object->Create from file ->Browse.
Once the file is selected choose the "Change icon" option and you will be able to rename the file and change the icon if you wish.
Hope this helps!