PowerPoint VBA:哪个命令(或一组命令)可以从我的 .jpg 图片中创建 ppt 框架?

发布于 2024-10-17 17:45:54 字数 193 浏览 3 评论 0原文

我在 C:\my_folder 中有一些 .jpg 文件,

它们的名称如下: pic_1.jpg 、 pic_2.jpg 、 pic_3.jpg 、 pic_4.jpg 、 pic_5.jpg 。

我应该使用 Power Point VBA 中的哪个命令或一组命令才能在 PowerPoint 中自动创建多个框架,以便每个框架包含一张图片?

I have a few .jpg files in C:\my_folder

Here are their names: pic_1.jpg , pic_2.jpg , pic_3.jpg , pic_4.jpg , pic_5.jpg .

What command or a group of commands in Power Point VBA should I use in order to be able to automatically create several frames in PowerPoint so that each frame would contain one picture?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹雨成花 2024-10-24 17:45:54

此 VBScript 创建一个新的 PowerPoint 演示文稿,并向其中添加两张幻灯片,每张幻灯片都有一张图片。您需要调整图片的位置和大小以适合您的口味。此外,如果您想自动抓取目录中存在的任何图片以嵌入到演示文稿中,您还需要利用 Scripting.FileScriptingObject 来枚举图像。如果您愿意,您的脚本还可以在生成幻灯片后调用 pptPresentation.SaveAs 来保存演示文稿。

MSDN 文档位于 http://msdn.microsoft.com/en-us /library/ff746873.aspx

Dim pptDoc
Dim pptPresentation
Dim pptSlide

Set pptDoc = WScript.CreateObject( "PowerPoint.Application" )
pptDoc.Visible = True
Set pptPresentation = pptDoc.Presentations.Add( True )

' Add a new slide with a blank layout to the end of the Slides collection
' 12 = ppLayoutBlank
Set pptSlide = pptPresentation.Slides.Add( pptPresentation.Slides.Count + 1, 12 )

' Add a picture into the slide, saving the picture into the PowerPoint document
' 10, 10 are the Left and Top coordinates respectively
pptSlide.Shapes.AddPicture "c:\FullPath\1.JPG", False, True, 10, 10

' Add another slide with a picture
Set pptSlide = pptPresentation.Slides.Add( pptPresentation.Slides.Count + 1, 12 )
pptSlide.Shapes.AddPicture "c:\FullPath\2.jpg", False, True, 10, 10

This VBScript creates a new PowerPoint presentation and adds two slides to it, each with a picture. You will need to adjust the picture's location and size to suit your taste. Also you'll need to utilize the Scripting.FileScriptingObject to enumerate your images if you want to automatically grab whatever pictures exist in a directory for embedding into the presentation. If you want your script can also save the presentation by calling pptPresentation.SaveAs after your slides are generated.

The MSDN documentation is located at http://msdn.microsoft.com/en-us/library/ff746873.aspx.

Dim pptDoc
Dim pptPresentation
Dim pptSlide

Set pptDoc = WScript.CreateObject( "PowerPoint.Application" )
pptDoc.Visible = True
Set pptPresentation = pptDoc.Presentations.Add( True )

' Add a new slide with a blank layout to the end of the Slides collection
' 12 = ppLayoutBlank
Set pptSlide = pptPresentation.Slides.Add( pptPresentation.Slides.Count + 1, 12 )

' Add a picture into the slide, saving the picture into the PowerPoint document
' 10, 10 are the Left and Top coordinates respectively
pptSlide.Shapes.AddPicture "c:\FullPath\1.JPG", False, True, 10, 10

' Add another slide with a picture
Set pptSlide = pptPresentation.Slides.Add( pptPresentation.Slides.Count + 1, 12 )
pptSlide.Shapes.AddPicture "c:\FullPath\2.jpg", False, True, 10, 10
愛上了 2024-10-24 17:45:54

由于之前的答案特定于 VBS 脚本,因此这里有一个作为 VBA 宏嵌入到 PowerPoint 中的版本。这是使用 PowerPoint 2010 创建的。

它对目录进行了硬编码,因此这是读者的一个练习,提示输入要扫描的目录名称。

Sub CreatePictureSlideshow( )
  Dim presentation
  Dim layout
  Dim slide

  Dim FSO
  Dim folder
  Dim file
  Dim folderName

  ' Set this to point at the folder you wish to import JPGs from
  ' Note: make sure this ends with a backslash \
  folderName = "c:\somedirectory\"

  ' Delete all slides and setup variables
  Set presentation = Application.ActivePresentation
  If presentation.Slides.count > 0 Then
     presentation.Slides.Range.Delete
  End If
  Set layout = Application.ActivePresentation.SlideMaster.CustomLayouts(1)
  Set FSO = CreateObject("Scripting.FileSystemObject")

  ' Retrieve the folder's file listing and process each file
  Set folder = FSO.GetFolder(folderName)
  For Each file In folder.Files

     ' Filter to only process JPG images
     If LCase(Mid(file.Name, Len(file.Name) - 3, 4)) = ".jpg" Then

        ' Create the new slide and delete any pre-existing contents
        Set slide = presentation.Slides.AddSlide(presentation.Slides.count + 1, layout)
        While slide.Shapes.count > 0
          slide.Shapes(1).Delete
        Wend

        ' Add the picture
        slide.Shapes.AddPicture folderName + file.Name, False, True, 10, 10

        ' Optional: create a textbox with the filename on the slide for reference
        '   Dim textBox
        '   Set textBox = slide.Shapes.AddTextbox(msoTextOrientationHorizontal, 10, 10, 200, 200)
        '   textBox.TextFrame.TextRange.Text = file.Name
     End If
  Next

End Sub

As the previous answer was specific to VBS scripting, here is a version for embedding into PowerPoint as a VBA macro. This was created with PowerPoint 2010.

This has the directory hard-coded so it's an exercise for the reader to prompt for a directory name to scan.

Sub CreatePictureSlideshow( )
  Dim presentation
  Dim layout
  Dim slide

  Dim FSO
  Dim folder
  Dim file
  Dim folderName

  ' Set this to point at the folder you wish to import JPGs from
  ' Note: make sure this ends with a backslash \
  folderName = "c:\somedirectory\"

  ' Delete all slides and setup variables
  Set presentation = Application.ActivePresentation
  If presentation.Slides.count > 0 Then
     presentation.Slides.Range.Delete
  End If
  Set layout = Application.ActivePresentation.SlideMaster.CustomLayouts(1)
  Set FSO = CreateObject("Scripting.FileSystemObject")

  ' Retrieve the folder's file listing and process each file
  Set folder = FSO.GetFolder(folderName)
  For Each file In folder.Files

     ' Filter to only process JPG images
     If LCase(Mid(file.Name, Len(file.Name) - 3, 4)) = ".jpg" Then

        ' Create the new slide and delete any pre-existing contents
        Set slide = presentation.Slides.AddSlide(presentation.Slides.count + 1, layout)
        While slide.Shapes.count > 0
          slide.Shapes(1).Delete
        Wend

        ' Add the picture
        slide.Shapes.AddPicture folderName + file.Name, False, True, 10, 10

        ' Optional: create a textbox with the filename on the slide for reference
        '   Dim textBox
        '   Set textBox = slide.Shapes.AddTextbox(msoTextOrientationHorizontal, 10, 10, 200, 200)
        '   textBox.TextFrame.TextRange.Text = file.Name
     End If
  Next

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文