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

时间:2019-04-21 01:01:19

标签: vba powerpoint

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

我希望在演示文稿的一张幻灯片上显示几个较小的图像。如果用户希望将其放大,则只需单击该图像。然后,我希望图像显示在它自己的新生成的幻灯片上,该幻灯片应尽可能大。当他们单击较大的图像时,它们将被带回到他们正在查看的较小图像的幻灯片。通过为节目中的每个小图像制作一个单独的全尺寸图像幻灯片,并在单击小图像时简单地调用大幻灯片编号,就可以轻松实现。但是,这很耗时,并且使演示文稿远远超出了实际需要。如果用户从不单击以查看放大的图像,则具有较大图像的页面将占用空间。单击图像后,我选择执行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的几页,但是找不到使用变量的示例。

2 个答案:

答案 0 :(得分:0)

e_object

我偶然发现了一段代码,该代码段显示了单击形状时可以将其标识符直接传递给子例程并分配给变量。就我而言,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()函数获得的。

现在可以将此代码分配给幻灯片上任意数量的形状,并将其复制并粘贴到演示文稿末尾的新创建的空白幻灯片上,以进行进一步的操作。

答案 1 :(得分:0)

完整的工作代码!

对于有兴趣的人来说,这是我在Powerpoint 2017中使用的已完成(也许没有收集到的)完全可操作的代码。

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

已记录在案,也可以在文件中记录下来,以使任何人都可以遵循allong在每一步骤中所发生的事情。如果我说错了什么,欢迎编辑适当的措词和术语。

这不特定于我的机器或路径或类似的东西。您可以简单地将其复制并粘贴到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