简体   繁体   English

如何使用变量来命名使用 VBA 而不是文字字符串的 Powerpoint 幻灯片

[英]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.我正在尝试使用 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.我一次一个地尝试了以下所有 4 个命令。

' 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.这些适用于 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.这允许我使用一个宏通过使用幻灯片名称作为演示文稿中形状中写入的文本来在幻灯片之间跳转。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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