在 Powerpoint 中的 .Slides 和 .Shapes 方法中使用变量 VBA

Using variables inside .Slides & .Shapes methods in Powerpoint VBA

我正在制作交互式 Powerpoint 演示文稿,用户可以在其中单击照片的缩略图并能够几乎全屏查看它。我在使用 .Shapes 和 .Slides 方法时遇到困难。

我希望在演示文稿的一张幻灯片上显示几张较小的图像。如果用户想查看非常大的图片,他们只需单击图片即可。然后我希望图像出现在它自己新生成的幻灯片上,因为它可以适合该幻灯片。当他们单击较大的图像时,他们将被带回他们正在查看的较小图像幻灯片。这很容易实现,只需为节目中的每个小图像制作一个单独的全尺寸图像幻灯片,并在单击小图像时简单地调用大幻灯片编号;然而,这很耗时,并且使演示文稿比需要的大得多。如果用户从不点击查看放大图片,那么包含大图片的页面将占用 space。我选择在点击图像时执行 vba 代码,应该是:

  1. 复制图片

  2. 在演示文稿中的最后一张幻灯片之后创建一张新幻灯片

  3. 将图像粘贴到新幻灯片中

  4. 将图像调整到适合屏幕的大小
  5. 用大图查看新幻灯片
  6. 让用户回到幻灯片 他们开始了。

代码:

Sub ViewFullSize()

    Dim pptNewSlide As Slide
    ' Dim objCurrentSlideIndex As Integer

    ' objCurrentSlideIndex = ActiveWindow.Selection.SlideRange.SlideIndex

    With ActivePresentation

        .Slides(2).Shapes("Picture 7").Copy

        .Slides(4).Shapes.Paste

    End With


    Set pptNewSlide = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, ppLayoutCustom)

    ActivePresentation.SlideShowWindow.view.Last

End Sub

这段代码执行并做了预期的事情。我的问题是,我需要将幻灯片编号和形状编号作为变量。我不想为 100 张可以点击的照片重写这段代码。我试过使当前幻灯片成为这样的变量:

    Dim objCurrentSlideIndex As Integer
    objCurrentSlideIndex = ActiveWindow.Selection.SlideRange.SlideIndex
    With ActivePresentation
        .Slides(objCurrentSlideIndex).Shapes("Picture 7").Copy
        .Slides(4).Shapes.Paste`
    End With

我试过的变量.Slides(objCurrentSlideIndex)导致整个子例程不执行,但不会使幻灯片崩溃。我使用了 Set 和许多其他语法,但无法让它使用变量而不是普通数字。有没有办法做到这一点? .Slides().Shapes() 方法甚至可以使用变量吗?我已经阅读了几个 Microsoft 和 PPTools 页面,但找不到使用变量的示例。

Sub ViewFullSize(objCurrentShape As Shape) ' Place shape clicked on into variable.

    Dim pptNewSlide As Slide
    Dim objCurrentSlideNum As Integer
    Dim objLastSlideNum As Integer

' Place current slide number into a variable.
    objCurrentSlideNum = ActivePresentation.SlideShowWindow.view.CurrentShowPosition

' Send shape to clipboard for later pasting.
    ActivePresentation.Slides(objCurrentSlideNum).Shapes(objCurrentShape.Name).Copy

' Place new blank slide at the end of the presentation.
    Set pptNewSlide = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, ppLayoutCustom)

' Make the new slide the active slide.
    ActivePresentation.SlideShowWindow.view.Last

' Place the new slide number into a variable.
    objLastSlideNum = ActivePresentation.SlideShowWindow.view.CurrentShowPosition

' Paste the shape image from the clipboard onto the new slide.
    ActivePresentation.Slides(objLastSlideNum).Shapes.Paste

End Sub

我无意中发现了一段代码,它显示当单击一个形状时,它可以将其标识符直接传递到子例程中并分配给一个变量。就我而言 (objCurrentShape As Shape)。然后可以将其与我用来调用复制形状的 .Shapes() 方法一起使用 .Shapes(objCurrentShape.Name).Copy.

.Slides() 方法更易于分配给变量(或者我相信),因为它不依赖于单击的形状。它只是活动幻灯片编号,是通过 .View.CurrentShowPosition 函数获得的。

此代码现在可以分配给幻灯片上的任意数量的形状,并且会在演示文稿结束时将该形状复制并粘贴到新创建的空白幻灯片中,以供进一步操作。

完整的代码!

对于任何感兴趣的人,这是我在 Powerpoint 2017 中使用的已完成(可能未收集)、完全可操作的代码。

这被设计为作为宏动作分配给幻灯片中的图片。当页面上有多个较小尺寸的图像时,可以为每个图像分配一个宏,该宏将在其自己的幻灯片上全屏显示图像,然后将用户直接发送回包含较小图像的屏幕。这有点像全屏缩放功能。

它已被记录在案,而且我可以记录下来,以允许任何人跟随每一步发生的事情。如果我说错了什么,欢迎修改正确的措辞和术语。

这不是特定于我的机器或路径或类似的东西。您可以简单地复制并粘贴到 PowerPoint 中的模块,然后开始将新宏分配给演示文稿中的任何图像。

Sub ViewFullSize(objCurrentShape As Shape) ' Place shape clicked-on into variable.
 ' Credit Shyam Pillai @ http://www.skphub.com/ppt00040.htm#2 for the method of
 ' bringing the shape into the macro as a variable allowing easier manipulation.

    Dim pptNewSlide As Slide
    Dim objCurrentSlideNum As Integer
    Dim objLastSlideNum As Integer
    Dim objLargeView As Shape

' Place current slide number into a variable.
    objCurrentSlideNum = ActivePresentation.SlideShowWindow.view.CurrentShowPosition

' Copy shape to clipboard for later pasting.
    ActivePresentation.Slides(objCurrentSlideNum).Shapes(objCurrentShape.Name).Copy

' Place new blank 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

' Place the new slide number into a variable.
    objLastSlideNum = ActivePresentation.SlideShowWindow.view.CurrentShowPosition

' Paste the shape image from the clipboard onto the new slide.
    ActivePresentation.Slides(objLastSlideNum).Shapes.Paste

' Put pasted image into a variable.
    Set objLargeView = ActivePresentation.Slides(objLastSlideNum).Shapes(1)

' Full credit for this next section of the code goes to PPTools & David Marcovitz
' @ http://www.pptfaq.com/FAQ00352_Batch_Insert_a_folder_full_of_pictures-_one_per_slide.htm
' Thanks for the hard work!

' Manipulate the image using the variable.
    With objLargeView
      ' Set mouse-click action on image to return user back to the slide they came from.
        .ActionSettings(ppMouseClick).Action = ppActionLastSlideViewed
      ' Reposition the image for proper resizing
        .Left = 0
        .Top = 0
        .ScaleHeight 1, msoTrue
        .ScaleWidth 1, msoTrue
      ' Resize the image to full screen while maintaining aspect ratio.
      ' This is wide screen mode.  If you are working with the more
      ' narrow mode, simply change the 9 to a 3 and the 16 to a 4
      ' to keep the correct aspect ratio.
        If 9 * .Width > 16 * .Height Then
            .Width = ActivePresentation.PageSetup.SlideWidth
            .Top = 0.5 * (ActivePresentation.PageSetup.SlideHeight - .Height)
        Else
            .Height = ActivePresentation.PageSetup.SlideHeight
            .Left = 0.5 * (ActivePresentation.PageSetup.SlideWidth - .Width)
        End If
    End With
' From here, the slideshow is now showing the originally clicked-on image
' full screen on its own page waiting for the user to click on it to return
' to the rest of the show.  If the slideshow isn't set to kiosk mode, then
' there is the possibility of the user clicking somewhere on the screen out
' of the picture area and it would end the slideshow.
End Sub