Programmatically add an image to a PowerPoint file

Is there a way to programmatically add an image to a PowerPoint file?

  • Create a file, open a file
  • Add image
  • Close the file

I need to do this with up to about 1000 PowerPoint files.

+3
source share
3 answers
+2
source

You may need to use FileSystemObject here to run Otaku 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 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
+3

, - 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 will need to manage part of the image (in the .Slides(1).Shapes.AddPicture FileName:=above) to ensure that you receive the images you want each time. In the same procedure, you can set the image size ( Widthand Height- they are optional) and location ( Leftand Top).

+2
source

Source: https://habr.com/ru/post/1756632/


All Articles