簡體   English   中英

如何使用變量來命名使用 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

我的代碼運行並帶我到新創建的幻燈片; 但是,我不知道如何判斷它是否真的命名了新幻燈片。 因為我使用變量來命名幻燈片,所以我懷疑它不起作用。 當我從不同的幻燈片運行另一個宏時,它不會將我跳轉到我重命名的幻燈片。 我還使用一個變量來標識我試圖跳轉到的幻燈片的名稱。 我一次一個地嘗試了以下所有 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM