简体   繁体   中英

How to use a variable to name a Powerpoint slide using VBA rather than a literal string

I am trying to create a new slide and name it at the same time using VBA. I have a main menu page that contains shapes with text labels in them. I've set the action for each shape to run the following macro: It is intended to create a new slide and name it the same as the category name that is shown in text on the button the user clicked.

Sub ChangeSlideName(objCurrentShape As Shape) ' Place the clicked on shape in a variable.

    Dim objTextInBox As String
    Dim objCurrentSlideNum As Integer
    
' Place current slide number into a variable.
    objCurrentSlideNum = ActivePresentation.SlideShowWindow.View.CurrentShowPosition
    
' Set value of string variable to the text in the shape clicked on.
    objTextInBox = ActivePresentation.Slides(objCurrentSlideNum).Shapes(objCurrentShape.Name).TextFrame.TextRange.Text
    
' Create a new slide at the end of the presentation.
    Set pptNewSlide = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, ppLayoutBlank)
    
' Make the new slide the active slide.
    ActivePresentation.SlideShowWindow.View.Last
    
' Set the name of the new slide to the text contained on the button clicked.
    ActiveWindow.View.Slide.Name = objTextInBox
    
End Sub

My code runs and takes me to the newly created slide; however, I don't know how to tell if it actually named the new slide. Because I'm using a variable to name the slide, I suspect it isn't working. When I run another macro from a different slide, it will not jump me to the slide I renamed. I also use a variable to identify the name of the slide I am trying to jump to. I've tried all 4 of the following commands one at a time.

' Take user to the page of the category clicked on.
    SlideShowWindows(1).View.GotoSlide GetSlideIndex(objTextInBox)
    
    ActivePresentation.SlideShowWindow.View.GotoSlide (objTextInBox)
    
    SlideShowWindows(1).View.GotoSlide GetSlideIndex(objTextInBox), 1
    
    ActivePresentation.Slides(objTextInBox).Select

Am I missing something in my code like parenthesis or quotation marks or something? Can I not use a variable in place of a literal string like "New Slide Name"? Also, can someone tell me how to verify the name of a slide?

These work for Powerpoint-2010. I haven't tried it in other version. When a user clicks on a shape (in my case it's a rectangle with text inside that shows a category name) this code will create a new slide and name it whatever category name was in the shape clicked on.

Sub ChangeSlideName(objClickedShape As Shape) ' Place the clicked on shape in a variable.

    Dim objText As String
    Dim objSlideNum As Integer
    Dim pptNewSlide

    
' Place current slide number into a variable.
    objSlideNum = ActivePresentation.SlideShowWindow.View.CurrentShowPosition
    
' Set value of string variable to the text in the shape clicked on.
    objText = ActivePresentation.Slides(objSlideNum).Shapes(objClickedShape.Name).TextFrame.TextRange.Text
    
' Create a new slide at the end of the presentation.
    Set pptNewSlide = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, ppLayoutBlank)
    
' Make the new slide the active slide.
    ActivePresentation.SlideShowWindow.View.Last
    
' Change slide # variable to the number of the newly created slide.
    objSlideNum = ActivePresentation.SlideShowWindow.View.CurrentShowPosition
    
' Set the name of the new slide to the text contained on the button clicked.
    ActivePresentation.Slides(objSlideNum).Name = objText
 
End Sub

This is the command that will jump to the newly created slide using the variable:

    Dim osld As Slide
    Set osld = ActivePresentation.Slides(objTextInBox)
    SlideShowWindows(1).View.GotoSlide (osld.SlideIndex)

This allows me to use one macro to jump between slides by using the slide names as the text written in the shapes in the presentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM