2013-05-31 283 views
9

上下文: C#中的PowerPoint幻燈片有一個屬性Slide.Name(通常包含任意字符串值)。 在我的C#應用​​程序中,我想使用此屬性來識別幻燈片(幻燈片順序是不可靠的)。Powerpoint:手動設置幻燈片名稱

問題: 如何手動設置PowerPoint應用程序中的Slide.Name屬性?

我的問題非常喜歡:「How to name an object within a PowerPoint slide?」,但只是在幻燈片上。

任何幫助,將不勝感激。

回答

4

您不能手動設置幻燈片名稱,但只需一點代碼,它很簡單。在VBA中,例如:

Sub NameThatSlide() 
    ActivePresentation.Slides(1).Name = "Whatever You Like Here" 
End Sub 
+0

這確實可以在幻燈片演示文稿編輯窗口中有效地更改幻燈片的名稱。但是,在VBA編輯器中,幻燈片*仍然*以*原始*名稱出現,例如「幻燈片1」和「幻燈片32」等。幻燈片被刪除時存在空白。是否有辦法讓VBAProject「Microsoft PowerPoint Object」列表中顯示的名稱更改爲與幻燈片的.Name屬性(在PowerPoint 2013中)一致? – DRC

+0

我不相信。每張幻燈片創建時的PowerPoint名稱。每張幻燈片還有一個.Name屬性,該屬性最初設置爲與幻燈片名稱相同的字符串。這是.Name屬性,而不是幻燈片的內部名稱,當我們設置.Name時會更改。它*可以通過編輯演示文稿XML來更改內部名稱。我很好奇:有什麼需要改變名字? –

+0

感謝您的回覆,@Steve Rindsberg。 「需求」是爲了便於在VBA中編程。我有一個「演示文稿」,它將成爲學生的培訓工具。它將完全由VBA控制,因此VBA代碼控制何時允許學生繼續下一張幻燈片等。因爲我已經重新排列了幻燈片的順序並完成了大量「測試」幻燈片,它令人困惑的是,在編輯視圖中使用幻燈片的幻燈片有一個明智的名稱,但隨後切換到幻燈片只有一個與編輯視圖中的名稱不一致的數字的VBAProject視圖。 – DRC

10

在PowerPoint中沒有允許您編輯幻燈片名稱的內置功能。正如史蒂夫所說,你必須使用VBA代碼來做到這一點。滑動名稱將永遠不會因更換幻燈片而更改,即使關閉PowerPoint,它也會保持不變;在VBA代碼中設置的幻燈片名稱是持久的。下面是一些代碼我寫的讓你輕鬆查看當前所選幻燈片的名稱,並允許您將其重命名:

'------------------------------------------------------------------ 
' NameSlide() 
' 
' Renames the current slide so you can refer to this slide in 
' VBA by name. This is not used as part of the application; 
' it is for maintenance and for use only by developers of 
' the PowerPoint presentation. 
' 
' 1. In Normal view, click on the slide you wish to rename 
' 2. ALT+F11 to VB Editor 
' 3. F5 to run this subroutine 
'------------------------------------------------------------------ 
Sub NameSlide() 
    Dim curName As String 
    curName = Application.ActiveWindow.View.Slide.name 

    Dim newName As String 
retry: 
    newName = InputBox("Enter the new name for slide '" + curName + "', or press Cancel to keep existing name.", "Rename slide") 
    If Trim(newName) = "" Then Exit Sub 

    Dim s As Slide 

    ' check if this slide name already exists 
    On Error GoTo SlideNotFound 
    Set s = ActivePresentation.Slides(newName) 
    On Error GoTo 0 

    MsgBox "Slide with this name already exists!" 
    GoTo retry 

    Exit Sub 

SlideNotFound: 
    On Error GoTo 0 
    Application.ActiveWindow.View.Slide.name = newName 
    MsgBox "Slide renamed to '" + newName + "'." 

End Sub 
+0

該腳本對於VBA編程非常有用:它允許您重命名任何幻燈片,以便在VBA代碼中可靠地使用幻燈片名稱。 –

1

我不能肯定,這將使您設置Slide.Name屬性,因爲我不是VBA程序員,但無論如何,AFAIK在PowerPoint 2010中命名幻燈片的最簡單方法是使用「大綱」視圖。

如果您將鼠標放置在創建的幻燈片上最左側,則可以向右拖動一種垂直幻燈片分類器。在該窗格的頂部,您會看到兩個標籤:幻燈片概要

選擇概述,您會看到每張幻燈片編號和一個灰色的抓取按鈕,可讓您對幻燈片進行重新排序。如果你點擊右邊的那個,你可以輸入你喜歡的任何名字,比如首頁

在主視圖窗格中,幻燈片將會有主頁被印上。然後,您可以將其保留在那裏,或者通過將字體顏色更改爲背景或將文本移到演示文稿框之外來隱藏它。

BTW您可以在超鏈接中使用這些名稱。

+0

雖然可能有某些原因需要執行此操作,但不幸的是,它實際上並未更改幻燈片的「.Name」屬性。 –

1

使用Sub SplitFile()函數從一組> 100張幻燈片創建單個幻燈片。一切順利!但任何人都可以告訴我什麼代碼我會用自動重命名文件,假設每個幻燈片在文本框中有一個標題?我希望幻燈片標題成爲新創建的單個幻燈片的文件名。

下面是我用來創建單個幻燈片(作爲單個文件)的代碼,感謝誰在線發佈它。

子SplitFile() 昏暗lSlidesPerFile只要 昏暗lTotalSlides只要 昏暗oSourcePres作爲呈現 昏暗otargetPres作爲呈現 昏暗sFolder作爲字符串 昏暗六重峯作爲字符串 昏暗sBaseName作爲字符串 昏暗lCounter只要 昏暗lPresentationsCount只要'多少會我們將其分成 昏暗的X只要 昏暗lWindowStart只要 昏暗lWindowEnd只要 昏暗sSplitPresName作爲字符串

On Error GoTo ErrorHandler 

Set oSourcePres = ActivePresentation 
If Not oSourcePres.Saved Then 
    MsgBox "Please save your presentation then try again" 
    Exit Sub 
End If 

lSlidesPerFile = CLng(InputBox("How many slides per file?", "Split Presentation")) 
lTotalSlides = oSourcePres.Slides.Count 
sFolder = ActivePresentation.Path & "\" 
sExt = Mid$(ActivePresentation.Name, InStr(ActivePresentation.Name, ".") + 1) 
sBaseName = Mid$(ActivePresentation.Name, 1, InStr(ActivePresentation.Name, ".") - 1) 

If (lTotalSlides/lSlidesPerFile) - (lTotalSlides \ lSlidesPerFile) > 0 Then 
    lPresentationsCount = lTotalSlides \ lSlidesPerFile + 1 
Else 
    lPresentationsCount = lTotalSlides \ lSlidesPerFile 
End If 

If Not lTotalSlides > lSlidesPerFile Then 
    MsgBox "There are fewer than " & CStr(lSlidesPerFile) & " slides in this presentation." 
    Exit Sub 
End If 

For lCounter = 1 To lPresentationsCount 

    ' which slides will we leave in the presentation? 
    lWindowEnd = lSlidesPerFile * lCounter 
    If lWindowEnd > oSourcePres.Slides.Count Then 
     ' odd number of leftover slides in last presentation 
     lWindowEnd = oSourcePres.Slides.Count 
     lWindowStart = ((oSourcePres.Slides.Count \ lSlidesPerFile) * lSlidesPerFile) + 1 
    Else 
     lWindowStart = lWindowEnd - lSlidesPerFile + 1 
    End If 

    ' Make a copy of the presentation and open it 

    For Each oSlide In ActiveWindow.Presentation.Slides 
strTitles = strTitles _ 
    & "Slide: " _ 
    & CStr(oSlide.SlideIndex) & vbCrLf _ 
    & oSlide.Shapes.Title.TextFrame.TextRange.Text _ 
    & vbCrLf & vbCrLf 

接着oSlide 對錯誤轉到的ErrorHandler

intFileNum = FreeFile

sSplitPresName = sFolder & sBaseName & _ 「_」 & CStr的(lWindowStart)& 「 - 」 & CStr的(lWindowEnd)&「 「。 & SEXT oSourcePres.SaveCopyAs sSplitPresName,ppSaveAsDefault 集otargetPres = Presentations.Open(sSplitPresName,,,真)

With otargetPres 
     For x = .Slides.Count To lWindowEnd + 1 Step -1 
      .Slides(x).Delete 
     Next 
     For x = lWindowStart - 1 To 1 Step -1 
      .Slides(x).Delete 
     Next 
     .Save 
     .Close 
    End With 

Next ' lpresentationscount 

NormalExit: 退出小組 的ErrorHandler: MSGBOX 「時出現錯誤」 恢復NormalExit 結束子

0

您可以手動或使用VBA重命名幻燈片。一旦你知道了,門就會打開一些有趣的可能性,我將用下面的代碼來演示。

手動重命名幻燈片。此功能隱藏在VBA編輯器的「屬性」窗格中,但它不需要需要編碼。

  1. 如果開發色帶是不可見的,啓用它:文件>選項>自定義功能區>檢查開發主標籤。

  2. 開發區中,單擊的Visual Basic菜單項打開Visual Basic編輯器。

  3. Ctrl + R鍵導航到Project Explorer窗格。

  4. 展開 「微軟PowerPoint對象

  5. 點擊任何幻燈片將其選中。

  6. F4鍵導航到「屬性」窗格。

  7. 編輯(名稱)項目,然後按Enter鍵申請更名。

幻燈片名稱更改可能不會立即出現在VBA Project Explorer窗格中。只要名稱在「屬性」窗格中正確,名稱就會成功更改。

這VBA代碼也將這樣的伎倆(隱藏幻燈片編號1):

ActivePresentation.Slides(1).SlideShowTransition.Hidden = msoTrue 

此代碼塊介紹了管理幻燈片名稱的幾種方法和答案的主要問題。另外,我使用幻燈片命名技巧和一點VBA來有選擇地從打印中刪除某些幻燈片。爲了填充宏列表,我添加了一些額外的VBA宏。從任何幻燈片:開發人員功能區>宏>選擇宏>運行按鈕。使用此方法啓動我的PresentSlide,DontPresentSlide,PrintSlide和DontPrintSlide宏。一旦您正確標記了各種幻燈片,只需在呈現或打印前分別運行PrepToPresentSlides或PrepToPrintSlides宏即可。

用這些宏玩一下並閱讀註釋。您會發現我可以擴展編寫代碼,因此您可以根據需要輕鬆修改它。

下面的代碼可以幫助我管理打印哪些幻燈片和對象,哪些對象在屏幕上顯示。當我想打印參考幻燈片但不包含它們時,這特別有用。當我使用動畫幻燈片時,它更加有用。動畫通常不會很好地翻譯打印。所以,我選擇不打印一些動畫對象。事實上,我甚至可以在替代內容中添加僅用於打印的對象(呈現時隱藏) - 雖然我很少這樣做。相反,我通常會將動畫從打印中隱藏起來,或者創建一張幻燈片以呈現,並將其非動畫副本用於打印。通過這些宏,可以輕鬆管理幻燈片和對象的混合搭配,以便打印和幻燈片以及對象進行演示。我希望你喜歡。

Option Explicit 

' DontPresentSlide - run macro while on a slide you wish to skip while presenting. 
'     The slide name will be appended with "NoPresent". You still 
'     need to run PrepToPresent before presenting to hide slide. 
' PresentSlide - "NoPresent" will be removed from the slide. You still 
'    need to run PrepToPresent before presenting to hide slide. 
' PrepToPesentSlides() - Unhide slides and objects you want presented and 
'    hide slides and objects you do NOT want presented. 
' ShowNoPressnt() - show slides and shapes marked "NoPresent" 
' HideNoPresent() - hide slides and shapes marked "NoPresent" 

' DontPrintSlide - run macro while on a slide you wish to skip while presenting. 
'     The slide name will be appended with "NoPrint". You still 
'     need to run PrepToPresent before presenting to hide slide. 
' PrintSlide - "NoPrint" will be removed from the slide. You still 
'    need to run PrepToPresent before presenting to hide slide. 
' PrepToPrintSlides() - Unhide slides and objects you want printed and 
'    hide slides and objects you do NOT want printed. 
' ShowNoPrint() - show slides and shapes marked "NoPrint" 
' HideNoPrint() - hide slides and shapes marked "NoPrint" 

' ShowHideSlides() - Hide or Unhide slides based on slide name. 
' ShowHideShapes() - Hide or Unhide shapes based on shapes name. 


Public Const cjaHide = False 
Public Const cjaShow = True 
Public Const cjaToggle = 2 

Sub ShowHideSlides(NameContains As String _ 
       , Optional LMR As String = "R" _ 
       , Optional ShowSlide As Integer = False) 
' Show or Hide slides based on slide name. 
' Arguements: 
' NameContains (string): 
'  slides with this string will be modified. 
' LMR (string): enter L, M or R to indicate 
'  searching the Left, Middle or Right of 
'  the slide name, respectively. 
' ShowSlide (integer): 
'  Show: True (-1) 
'  Hide: False (0) 
'  Toggle: 2 
' 
' To show or hide slides manually: 
' Right-click the slide thumbnail, then click Hide Slide 
' To rename slides, 
' Use this VBA: ActiveWindow.View.Slide.Name = "NewSlideName" 
' Or, edit the (Name) property in the VBA Properties window. 
' 
    Dim sldCurrent As Slide 
    Dim found As Boolean 
    found = False 

    LMR = Trim(UCase(LMR)) 
    If LMR <> "L" And LMR <> "M" Then LMR = "R" 
    'Loop through each slide in presentation. 
    For Each sldCurrent In ActivePresentation.Slides 
     'Match shape name left, right or middle as per LMR arguement. 
     'ActiveWindow.View.Slide.Name or Slide.SlideNumber 
     found = False 
     If LMR = "R" And LCase(right(sldCurrent.Name, Len(NameContains))) = LCase(NameContains) Then 
      found = True 
     ElseIf LMR = "L" And LCase(left(sldCurrent.Name, Len(NameContains))) = LCase(NameContains) Then 
      found = True 
     ElseIf LMR = "M" And InStr(1, LCase(NameContains), LCase(sldCurrent.Name)) Then 
      found = True 
     End If 
     'If match found, then set shape visibility per ShowShape arguement. 
     If found Then 
      If ShowSlide = True Then 
       ActivePresentation.Slides(sldCurrent.SlideNumber).SlideShowTransition.Hidden = msoFalse 
      ElseIf ShowSlide = False Then 
       ActivePresentation.Slides(sldCurrent.SlideNumber).SlideShowTransition.Hidden = msoTrue 
      Else 
       ActivePresentation.Slides(sldCurrent.SlideNumber).SlideShowTransition.Hidden = Not ActivePresentation.Slides(sldCurrent.SlideNumber).SlideShowTransition.Hidden 
      End If 
     End If 
    Next  'sldCurrent 
End Sub 

Sub ShowHideShapes(NameContains As String _ 
       , Optional LMR As String = "R" _ 
       , Optional ShowShape As Integer = False) 
' Show or Hide shapes/objects based on object name. 
' Arguements: 
' NameContains (string): 
'  shapes with this string will be modified. 
' LMR (string): enter L, M or R to indicate 
'  searching the Left, Middle or Right of 
'  the slide name, respectively. 
' ShowSlide (integer): 
'  Show: True (-1) 
'  Hide: False (0) 
'  Toggle: 2 
' 
' To show, hide and/or rename objects: 
' 1. Turn on Selection Pane via: Home Ribbon > 
'  Select > Selection Pane. 
' 2. Double-click a shape name to rename it. 
' 3. Click the eye icon to the far right to show/hide a shape. 

    Dim shpCurrent As Shape 
    Dim sldCurrent As Slide 
    Dim found As Boolean 
    found = False 

    LMR = Trim(UCase(LMR)) 
    If LMR <> "L" And LMR <> "M" Then LMR = "R" 
    'Loop through each slide in presentation. 
    For Each sldCurrent In ActivePresentation.Slides 
     With sldCurrent 
      'Loop through each shape on current slide. 
      For Each shpCurrent In .Shapes 
      'Match shape name left, right or middle as per LMR arguement. 
      found = False 
      If LMR = "R" And right(shpCurrent.Name, Len(NameContains)) = NameContains Then 
       found = True 
      ElseIf LMR = "L" And left(shpCurrent.Name, Len(NameContains)) = NameContains Then 
       found = True 
      ElseIf LMR = "M" And InStr(1, NameContains, shpCurrent.Name) Then 
       found = True 
      End If 
      'If match found, then set shape visibility per ShowShape arguement. 
      If found Then 
       If ShowShape = True Then 
        shpCurrent.Visible = True 
       ElseIf ShowShape = False Then 
        shpCurrent.Visible = False 
       Else 
        shpCurrent.Visible = Not shpCurrent.Visible 
       End If 
      End If 
      Next 'sldCurrent 
     End With 'sldCurrent 
    Next  'sldCurrent 

End Sub 

Sub HideNoPrint() 
' Hide slides and shapes you do NOT want printed. 
' 
' Run this macro to hide all slides and shapes that 
' end with the string "NoPrint". 

' Usage. Assume you have slides that contain animations that 
' make the printed slide difficult or impossible to read. 
' Let's further suppose you plan to present certain slides 
' but not print them. 
' 1. Add the"NoPrint" suffix to any shapes that clutter 
'  the printed page. 
' 2. Add the "NoPrint" suffix to slides you don't want to 
'  print. 
' 3. Run this macro to hide shapes and slides. 
' 4. Print the slides. 
' 5. Optionally, run the ShowNoPrint() macro in preparation 
'  for presenting the slides. 
    ShowHideShapes "NoPrint", "R", False 
    ShowHideSlides "NoPrint", "R", False 
End Sub 

Sub ShowNoPrint() 
' Unhide slides and shapes that were hidden 
' to prevent them from being printed in handouts. 
' 
    ShowHideShapes "NoPrint", "P", True 
    ShowHideSlides "NoPrint", "P", True 
End Sub 

Sub HideNoPressent() 
' Hide objects you do NOT want to present on screen. 
' 
' Run this macro to hide all slides and shapes that 
' end with the string "NoPresent". 
' 
' Usage. Assume you have slides that contain supporting material 
' that you wish to provide as printed handouts but not show. 
' You can manually hide those slides and objects of course. I 
' prefer to use these macros. 
' 1. Add the"NoPresent" suffix to any shapes that you want 
'  to print to handouts but not show on-screen. 
' 2. Add the "NoPresent" suffix to slides you want to 
'  print but not display on screen, such as reference slides. 
' 3. Run this macro to hide the "NoPresent" shapes and slides. 
' 4. Present your slides. 
' 5. Optionally, run the ShowNoPresent() macro in preparation 
'  for printing the slides. 
' 
    ShowHideShapes "NoPressent", "R", False 
    ShowHideSlides "NoPressent", "R", False 
End Sub 

Sub ShowNoPresent() 
' Unhide objects that were hidden to prevent them from 
' being presented on screen. 
' 
    ShowHideShapes "NoPressent", "P", True 
    ShowHideSlides "NoPressent", "P", True 
End Sub 

Sub PrepToPrintSlides() 
' Unhide objects you want printed and 
' hide objects you do NOT want printed. 
    ShowNoPresent 
    HideNoPrint 
End Sub 

Sub PrepToPresentSlides() 
' Unhide objects you want presented and 
' hide objects you do NOT want presented. 
    ShowNoPrint 
    HideNoPresent 
End Sub 

Sub DontPresentSlide() 
    Dim RetVal, sldName As String 
    sldName = Application.ActiveWindow.View.Slide.Name 
    If InStr(1, sldName, "NoPresent", vbBinaryCompare) = 0 Then 
     RetVal = RenameSlide(sldName, sldName & "-NoPresent") 
    End If 
    HideNoPresent 
End Sub 

Sub PresentSlide() 
    Dim RetVal, sldName As String, strStart As String, newName As String 
    'Remove the NoPresent suffix from the current slide. 

    'get slide name 
    sldName = Application.ActiveWindow.View.Slide.Name 
    'Unhide slide 
    ActivePresentation.Slides(sldName).SlideShowTransition.Hidden = msoFalse 
    'remove "-NoPresent" from slide name 
    Do 
     strStart = InStr(1, sldName, "-NoPresent") 
     If InStr(1, sldName, "-NoPresent") Then 
      newName = left(sldName, strStart - 1) & right(sldName, Len(sldName) - strStart - 9) 
      RetVal = RenameSlide(sldName, newName) 
     End If 
     sldName = Application.ActiveWindow.View.Slide.Name 
    Loop Until InStr(1, sldName, "-NoPresent") = 0 
    'remove "NoPresent" from slide name 
    Do 
     strStart = InStr(1, sldName, "NoPresent") 
     If InStr(1, sldName, "NoPresent") Then 
      newName = left(sldName, strStart - 1) & right(sldName, Len(sldName) - strStart - 8) 
      RetVal = RenameSlide(sldName, newName) 
     End If 
     sldName = Application.ActiveWindow.View.Slide.Name 
    Loop Until InStr(1, sldName, "NoPresent") = 0 

End Sub 

Sub DontPrintSlide() 
    Dim RetVal, sldName As String 
    sldName = Application.ActiveWindow.View.Slide.Name 
    If InStr(1, sldName, "NoPrint", vbBinaryCompare) = 0 Then 
     RetVal = RenameSlide(sldName, sldName & "-NoPrint") 
    End If 
    HideNoPrint 
End Sub 

Sub PrintSlide() 
    Dim RetVal, sldName As String, strStart As String, newName As String 
    'Remove the NoPrint suffix from the current slide. 

    'get slide name 
    sldName = Application.ActiveWindow.View.Slide.Name 
    'Unhide slide 
    ActivePresentation.Slides(sldName).SlideShowTransition.Hidden = msoFalse 
    'remove "-NoPrint" from slide name 
    Do 
     strStart = InStr(1, sldName, "-NoPrint") 
     If InStr(1, sldName, "-NoPrint") Then 
      newName = left(sldName, strStart - 1) & right(sldName, Len(sldName) - strStart - 7) 
      RetVal = RenameSlide(sldName, newName) 
     End If 
     sldName = Application.ActiveWindow.View.Slide.Name 
    Loop Until InStr(1, sldName, "-NoPrint") = 0 
    'remove "NoPrint" from slide name 
    Do 
     strStart = InStr(1, sldName, "NoPrint") 
     If InStr(1, sldName, "NoPrint") Then 
      newName = left(sldName, strStart - 1) & right(sldName, Len(sldName) - strStart - 6) 
      RetVal = RenameSlide(sldName, newName) 
     End If 
     sldName = Application.ActiveWindow.View.Slide.Name 
    Loop Until InStr(1, sldName, "NoPrint") = 0 
End Sub 

Sub HideAllCovers() 
' Run this macro to hide all Covers. 
    ShowHideShapes "Cover", "L", False 
End Sub 

Sub ShowAllCovers() 
' Run this macro to hide all Covers. 
    ShowHideShapes "Cover", "L", True 
End Sub 

Sub HideAllAnswers() 
' Run this macro to hide all Covers. 
    ShowHideShapes "Answer", "L", False 
End Sub 

Sub ShowAllAnswers() 
' Run this macro to hide all Covers. 
    ShowHideShapes "Answer", "L", True 
End Sub 

Sub HideAllQuestions() 
' Run this macro to hide all Covers. 
    ShowHideShapes "Question", "L", False 
End Sub 

Sub ShowAllQuestions() 
' Run this macro to hide all Covers. 
    ShowHideShapes "Question", "L", True 
End Sub 

Sub ShowAll() 
' Run this macro to hide all shapes (Covers and Answers). 
    ShowAllQuestions 
    ShowAllAnswers 
    ShowAllCovers 
    ShowNoPrint 
End Sub 

Sub HideAll() 
' Run this macro to hide all shapes (Covers and Answers). 
    HideAllQuestions 
    HideAllAnswers 
    HideAllCovers 
    HideNoPrint 
End Sub