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

如何使用变量来使用VBA而不是文字字符串命名Powerpoint幻灯片,vba,powerpoint-2010,Vba,Powerpoint 2010,我正在尝试创建一个新幻灯片,同时使用VBA将其命名。我有一个主菜单页面,其中包含带有文本标签的形状。我已将每个形状的操作设置为运行以下宏:它旨在创建一个新幻灯片,并将其命名为与用户单击的按钮文本中显示的类别名称相同的名称 Sub ChangeSlideName(objCurrentShape As Shape) ' Place the clicked on shape in a variable. Dim objTextInBox As String Dim objCurren

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

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

这是VBA,与VBScript不同的语言。您应该将标记形式VBS更改为VBA。