Consulting

Results 1 to 8 of 8

Thread: Renaming a PowerPoint Object!

  1. #1
    VBAX Regular
    Joined
    Apr 2015
    Posts
    27
    Location

    Renaming a PowerPoint Object!

    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

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    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)
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    VBAX Regular
    Joined
    Apr 2015
    Posts
    27
    Location
    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

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    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.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  5. #5
    VBAX Regular
    Joined
    Apr 2015
    Posts
    27
    Location
    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?

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    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))
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  8. #8
    VBAX Regular
    Joined
    Apr 2015
    Posts
    27
    Location
    Thank you all for your best advice!

    Kindest regards
    Philippe

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •