Programmatically add an image to a PowerPoint file
Is there a way to programmatically add an image to a PowerPoint file?
- Create the file, open the file
- Add the image
- Close the file
开发者_开发知识库I need to do this to about 1000 PowerPoint files.
You might want to look into using FileSystemObject here to run Otaku's code for each file in a specific directory
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
Yeah, you can use something like this from within 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
You'll need to manage the picture portion (in .Slides(1).Shapes.AddPicture FileName:=
above) to ensure you're getting the pictures you want each time. In that same routine, you can set the picture size (Width
and Height
- they're optional) and location (Left
and Top
).
Something similar was answered in the past: An automated way to load multiple images into PowerPoint on different pages?
精彩评论