Search code examples
vbapowerpoint

vba macros load image file name into slide & not the image


I'm just new here and wish your kind help please. I had a macro add-in for PowerPoint was working fine with older versions. The new 365 office didn't run it; and with a few tricks I was able to solve most of it. Now the only thing left is when try to open and select image files from a folder, it loads the image name to each slide and not the images!

Sub Insert1PicViaForm()

' Added on 21.05.06 to load single file using code from
'
'  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnovba00/html/CommonDialogsPartI.asp
'
'
Dim OFN As OPENFILENAME
Dim Ret
Dim N As Integer
Dim ddd
Dim oSld As Slide
Dim oPic As Shape

  With OFN
    .lStructSize = LenB(OFN)     ' Size of structure.
    .nMaxFile = 574             ' Size of buffer.
    ' Create buffer.
    .lpstrFile = String(.nMaxFile - 1, 0)
    Ret = GetOpenFileName(OFN)  ' Call function.
    If Ret <> 0 Then            ' Non-zero is success.
      ' Find first null char.
      N = InStr(.lpstrFile, vbNullChar)
      ' Return what's before it.
      ' MsgBox Left(.lpstrFile, n - 1)
      ' Full path and filename
      ddd = Left(.lpstrFile, N - 1)
      ' Add slide at end of presentation
            
      Set oSld = ActivePresentation.Slides.Add(ActivePresentation.Slides.count + 1, ppLayoutBlank)
      ' Insert pic as selected
      Set oPic = oSld.Shapes.AddPicture(FileName:=ddd, _
              LinkToFile:=msoCTrue, _
              SaveWithDocument:=msoCTrue, _
              Left:=60, _
              Top:=35, _
              Width:=98, _
              Height:=48)

    End If
  End With
End Sub

Solution

  • Some remarks about your code:

    • You're missing the GetOpenFileName function.
    • Opening the file dialog and returning the file path is now straightforward. No need to read and buffer the picture file

    Please read the code's comments and ajust it to fit your needs

    Public Sub InsertPicture()
    
        ' Declare and set a variable to ask for a file
        Dim fileDialogObject As FileDialog
        Set fileDialogObject = Application.FileDialog(msoFileDialogFilePicker)
        
        ' Adjust file dialog properties
        With fileDialogObject
            .InitialFileName = "C:\Temp"
            .Title = "Insert Picture"
            .ButtonName = "Insert picture"
            .InitialView = msoFileDialogViewDetails
            .Filters.Add "Images", "*.gif; *.jpg; *.jpeg,*.png,*.eps,*.tif,*.tiff", 1
        End With
        
        ' Show the file dialog to user and wait for response
        If fileDialogObject.Show = False Then Exit Sub
        
        ' Loop through each selected file (selectedFile returns the file path string)
        Dim selectedFile As Variant
        For Each selectedFile In fileDialogObject.SelectedItems
        
            ' Set new slide layout
            Dim pptLayout As CustomLayout
            Set pptLayout = ActivePresentation.Slides(1).CustomLayout
        
            ' Add a sile and reference it
            Dim newSlide As Slide
            Set newSlide = ActivePresentation.Slides.AddSlide(ActivePresentation.Slides.Count + 1, pptLayout)
            
            ' Insert picture in new slide
            Dim newPicture As Shape
            Set newPicture = newSlide.Shapes.AddPicture(FileName:=selectedFile, _
                            LinkToFile:=msoCTrue, _
                            SaveWithDocument:=msoCTrue, _
                            Left:=60, _
                            Top:=35, _
                            Width:=98, _
                            Height:=48)
        Next selectedFile
    
    End Sub
    

    Let me know if it works