PDA

View Full Version : Renaming a PowerPoint Object!



Exposian
04-16-2015, 05:36 AM
Hi all,

I have this piece of code to rename an object, but it doesn't seem to be working... Can anyone spot what's wrong?

All help greatly appreciated as always!
Regards,
Philippe



Public Sub ReName()
Dim o As Shape, s As String
Set o = ActiveWindow.Selection.ShapeRange(1)
s = InputBox("Enter the new name", "Rename '" & o.Name & "'", o.Name)
On Error Resume Next
If s <> "" Then o.Name = s
If Err.Number <> 0 Then MsgBox "Cannot rename object"
End Sub

John Wilson
04-16-2015, 05:51 AM
There's not a lot wrong with the code so it should work.

What do you see happening?

I presume you know you can rename shapes with the selection pane without code at all? (after version 2003)

Exposian
04-16-2015, 06:14 AM
Basically, I have a function that hides all object tagged 'HideMe'. When I use the rename function to rename an object 'HideMe' it does not get hidden.

The below is the code that hides the objects...



Sub HideUnhideTaggedShapes()




Dim oSl As Slide
Dim oSh As Shape


For Each oSl In ActivePresentation.Slides
For Each oSh In oSl.Shapes
If oSh.Tags("HideMe") = "YES" Then
' Make the visible hidden,
' or the hidden visible
oSh.Visible = Not oSh.Visible
End If
Next
Next


End Sub

John Wilson
04-16-2015, 06:35 AM
Do you mean you think renaming the object to "HideMe" will add trhe tag so that it gets hidden??

It won't

To add the tag you need to say

ActiveWindow.Selection.ShapeRange(1).Tags.Add "HIDEME", "YES"

It doesn't really make any difference but most PPT programmers use upper case for tags by convention.

Exposian
04-16-2015, 06:44 AM
Ahh, I see.. Mmmm the problem is I might want to use the 'ReName' function to Tag objects with different names...

Is there a way I can create 'TagMe' function instead of my 'ReName' function?

Paul_Hossler
04-16-2015, 07:07 AM
Names are not Tags

and

Tags are not Names



I might want to use the 'ReName' function to Tag objects with different names...

So you can 'Rename' an object to give it a new NAME, or you can add and delete and look for a TAG associated with an object that might have a NAME


Here's a simple example. Just insert a shape onto the first slide




Option Explicit
Sub test()
Dim i As Long
Dim o As Shape

Set o = ActivePresentation.Slides(1).Shapes(1)

o.Tags.Add "TAG1", "VALUE1"
o.Tags.Add "TAG2", "VALUE2"
o.Tags.Add "TAG3", "VALUE3"

o.Name = "MyShapeWithTags"
With ActivePresentation.Slides(1).Shapes("MyShapeWithTags")
MsgBox .Tags("TAG3")
MsgBox .Tags("TAG2")
MsgBox .Tags("TAG1")
MsgBox .Tags("TAG0")

MsgBox .Name

.Name = "NewName"

End With
With ActivePresentation.Slides(1).Shapes("NewName")
MsgBox .Tags("TAG3")
MsgBox .Tags("TAG2")
MsgBox .Tags("TAG1")
MsgBox .Tags("TAG0")

MsgBox .Name

End With

End Sub

John Wilson
04-16-2015, 08:36 AM
What Paul said and just an aside the routine you posted to change the name is NOT technically a Function. I know it a bit pedantic but it will help you later to use the correct terms.

Functions look like this (this is a trivial one) and return an answer value.

Function getName(oshp As Shape) As String
getName = oshp.Name
End Function

Anywhere in your code you can then say

MsgBox getName(ActiveWindow.Selection.ShapeRange(1))

Exposian
04-16-2015, 08:52 AM
Thank you all for your best advice!

Kindest regards
Philippe