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

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

我正在尝试创建一张新幻灯片并同时使用 VBA 为其命名。我有一个主菜单页面,其中包含带有文本标签的形状。我已将每个形状的操作设置为 运行 以下宏:它旨在创建一个新幻灯片并将其命名为与用户单击的按钮上文本中显示的类别名称相同的名称。

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

我的代码 运行s 并将我带到新创建的幻灯片;但是,我不知道如何判断它是否真的命名了新幻灯片。因为我使用变量来命名幻灯片,所以我怀疑它不起作用。当我 运行 来自不同幻灯片的另一个宏时,它不会跳转到我重命名的幻灯片。我还使用一个变量来标识我要跳转到的幻灯片的名称。我一次尝试了以下所有 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

我的代码中是否遗漏了括号或引号之类的东西?我不能使用变量代替像“新幻灯片名称”这样的文字字符串吗?另外,谁能告诉我如何验证幻灯片的名称?

这些适用于 Powerpoint-2010。我还没有在其他版本中尝试过。 当用户点击一个形状(在我的例子中是一个矩形,里面有文本显示类别名称)时,此代码将创建一个新幻灯片并将其命名为所点击形状中的任何类别名称。

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

这是使用变量跳转到新创建的幻灯片的命令:

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

这允许我使用一个宏在幻灯片之间跳转,方法是将幻灯片名称用作演示文稿中形状中写入的文本。