以编程方式将图像添加到PowerPoint文件

时间:2010-07-26 19:54:59

标签: powerpoint powerpoint-vba

有没有办法以编程方式将图像添加到PowerPoint文件?

  1. 创建文件,打开文件
  2. 添加图片
  3. 关闭文件
  4. 我需要对大约1000个PowerPoint文件执行此操作。

3 个答案:

答案 0 :(得分:3)

您可能希望在此处使用FileSystemObject为特定目录中的每个文件运行Otaku代码

Dim objFSO As Object, objFile As Object, strPath As String, p as Presentation
strPath = "C:\Wherever\" Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strPath) Then For Each objFile In objFSO.GetFolder(strPath).Files If InStr(1, UCase(objFile.Name), ".JPG") + _ InStr(1, UCase(objFile.Name), ".GIF") + _ InStr(1, UCase(objFile.Name), ".PNG") + _ InStr(1, UCase(objFile.Name), ".BMP") > 0 Then '# use Otaku's code above to add a presentation, the image, then close Set p = Presentations.Add(msoFalse) With p .Slides.Add Index:=1, Layout:=ppLayoutBlank .Slides(1).Shapes.AddPicture FileName:=strPath & objFile.Name, _ LinktoFile:=msoFalse, _ SaveWithDocument:=msoTrue, Left:=10, Top:=10 .SaveAs strPath & Left(objFile.Name, InStr(1, objFile.Name, ".") - 1) .Close End With Set p = Nothing End If Next End If

答案 1 :(得分:2)

答案 2 :(得分:2)

是的,你可以在PowerPoint中使用这样的东西:

Sub CreatePresWithImage()
    Dim i As Integer
    Dim imagePath As String
    Dim savePath As String
    imagePath = "C:\Documents and Settings\XPMUser\My Documents\My Pictures\"
    savePath = "C:\Documents and Settings\XPMUser\My Documents\"
    Dim p As Presentation
    For i = 0 To 999
        Set p = Presentations.Add(msoFalse)
        With p
            .Slides.Add Index:=1, Layout:=ppLayoutBlank
            .Slides(1).Shapes.AddPicture FileName:=imagePath & "HueTint-30.png", _
                                            LinktoFile:=msoFalse, _
                                            SaveWithDocument:=msoTrue, Left:=10, Top:=10
            .SaveAs savePath & "Sample" & i + 1
            .Close
        End With
        Set p = Nothing
    Next
End Sub

您需要管理图片部分(在.Slides(1).Shapes.AddPicture FileName:=上方),以确保您每次都能获得所需的图片。在同一例程中,您可以设置图片大小(WidthHeight - 它们是可选的)和位置(LeftTop)。