简体   繁体   English

通过Power Point VBA中的复选框选择某些幻灯片

[英]Select certain slides by check boxes in Power Point VBA

I need to be able to create a new .ppt (PowerPoint presentation) from selected slides in my original .ppt . 我需要能够从原始.ppt选定幻灯片中创建一个新的.ppt (PowerPoint演示文稿)。 The following macro will take whatever slides you currently have selected and copy them into a new .ppt . 以下宏将采用您当前选择的任何幻灯片,并将它们复制到新的.ppt I've found the following nice code to do most of the work. 我发现以下不错的代码可以完成大部分工作。

Private Sub NytPPT_Click()

'PURPOSE: Copies selected slides and pastes them into a brand new presentation file
'SOURCE: www.TheSpreadsheetGuru.com

Dim NewPPT As Presentation
Dim OldPPT As Presentation
Dim Selected_slds As SlideRange
Dim Old_sld As Slide
Dim New_sld As Slide
Dim x As Long, y As Long
Dim myArray() As Long
Dim SortTest As Boolean

'Set variable to Active Presentation
  Set OldPPT = ActivePresentation

'Set variable equal to only selected slides in Active Presentation
  Set Selected_slds = ActiveWindow.Selection.SlideRange

'Sort Selected slides via SlideIndex
  'Fill an array with SlideIndex numbers
    ReDim myArray(1 To Selected_slds.Count)
      For y = LBound(myArray) To UBound(myArray)
        myArray(y) = Selected_slds(y).SlideIndex
      Next y

  'Sort SlideIndex array
    Do
      SortTest = False
      For y = LBound(myArray) To UBound(myArray) - 1
        If myArray(y) > myArray(y + 1) Then
          Swap = myArray(y)
          myArray(y) = myArray(y + 1)
          myArray(y + 1) = Swap
          SortTest = True
        End If
      Next y
    Loop Until Not SortTest

'Set variable equal to only selected slides in Active Presentation (in numerical order)
  Set Selected_slds = OldPPT.Slides.Range(myArray)

'Create a brand new PowerPoint presentation
  Set NewPPT = Presentations.Add

'Align Page Setup
  NewPPT.PageSetup.SlideHeight = OldPPT.PageSetup.SlideHeight
  NewPPT.PageSetup.SlideOrientation = OldPPT.PageSetup.SlideOrientation
  NewPPT.PageSetup.SlideSize = OldPPT.PageSetup.SlideSize
  NewPPT.PageSetup.SlideWidth = OldPPT.PageSetup.SlideWidth

'Loop through slides in SlideRange
  For x = 1 To Selected_slds.Count

    'Set variable to a specific slide
      Set Old_sld = Selected_slds(x)

    'Copy Old Slide
      yy = Old_sld.SlideIndex
      Old_sld.Copy

    'Paste Slide in new PowerPoint
      NewPPT.Slides.Paste
      Set New_sld = Application.ActiveWindow.View.Slide

    'Bring over slides design
      New_sld.Design = Old_sld.Design

    'Bring over slides custom color formatting
      New_sld.ColorScheme = Old_sld.ColorScheme

    'Bring over whether or not slide follows Master Slide Layout (True/False)
      New_sld.FollowMasterBackground = Old_sld.FollowMasterBackground

  Next x


End Sub

What I need to do, is to select which slides to copy - based on check boxes. 我需要做的是根据复选框选择要复制的幻灯片。 So, for example if I select Check Box 1 = TRUE, it will create slides 1, 2 and 3. Or if I select Check box 2 = TRUE, that it could select slide 3, 4, 5 and 6. And so, if I selected both boxes it would create slides = 1, 2, 3, 4, 5, 6. Leaving out any duplicates. 因此,例如,如果我选择“复选框1 = TRUE”,它将创建幻灯片1、2和3。或者,如果我选择“复选框2 = TRUE”,则可以选择幻灯片3、4、5和6。因此,如果我选择了两个框,它将创建幻灯片= 1,2,3,4,5,6。不包含任何重复项。

I've tried a lot, including this: 我已经尝试了很多,包括:

Private Sub CheckBox1_Click()
    If CheckBox1.Value = True Then
        ActivePresentation.Slides.Range(Array(1, 2, 3)).Select
    Else
        MsgBox "nothing"
    End If
End Sub


Private Sub CheckBox2_Click()
    If CheckBox2.Value = True Then
        ActivePresentation.Slides.Range(Array(3, 4, 5, 6)).Select
    Else
        MsgBox "nothing"
    End If
End Sub

I get the error: Slide (unknown member) : Invalid request. 我收到错误:幻灯片(未知成员):无效的请求。 This view does not support selection. 该视图不支持选择。

I am not sure how I could get this to work? 我不确定如何使它正常工作? Any help is appreciated, I'am very new to VBA coding. 感谢您的帮助,对于VBA编码我还是很陌生。

All credit for code goes to. 代码的全部功劳归于。 http://www.thespreadsheetguru.com/the-code-vault/2014/4/3/copy-selected-slides-into-new-powerpoint-presentation http://www.thespreadsheetguru.com/the-code-vault/2014/4/3/copy-selected-slides-into-new-powerpoint-presentation

You can switch the view to enabled the slides to be selected as follows: 您可以如下切换视图以启用幻灯片:

ActiveWindow.ViewType = ppViewSlideSorter

For some reason, the slides aren't selected in the normal view! 由于某些原因,在普通视图中未选择幻灯片!

But selecting things in PowerPoint brings its own challenges (as seen with the view type) and you don't need to select them in order to copy and paste them as per this example: 但是在PowerPoint中选择内容会带来自身的挑战(如视图类型所示),您无需按照以下示例选择它们来复制和粘贴它们:

With ActivePresentation.Slides
  .Range(Array(1, 2)).Copy
  .Paste
End With

This will simplify your code as you don't need to manage windows and their views. 这将简化您的代码,因为您不需要管理窗口及其视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM