[英]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
我的代码运行并带我到新创建的幻灯片; 但是,我不知道如何判断它是否真的命名了新幻灯片。 因为我使用变量来命名幻灯片,所以我怀疑它不起作用。 当我从不同的幻灯片运行另一个宏时,它不会将我跳转到我重命名的幻灯片。 我还使用一个变量来标识我试图跳转到的幻灯片的名称。 我一次一个地尝试了以下所有 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)
这允许我使用一个宏通过使用幻灯片名称作为演示文稿中形状中写入的文本来在幻灯片之间跳转。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.