Results 1 to 5 of 5

Thread: Coding works for WORD but not POWERPOINT, how should i modify it?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    VBAX Newbie
    Joined
    Mar 2023
    Posts
    2
    Location
    Thanks! i tried to modify the code just now and it works on powerpoint but still i would like to update some functions

    1) it doesn't work when the file is at .jpeg
    2) the image is now resize, how should i keep it as it's own proportion?
    3) images are now on the left of the slide, how can i keep it centred?



    Sub PicWithCaption()
        Dim xFileDialog As FileDialog
        Dim xPath As String, xFile As String, xFileName As String
        Dim oSlide As Slide
        Dim oShape As Shape
        Dim i As Long, j As Long
        Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
        xFileDialog.Title = "Select a folder with pictures"
        If xFileDialog.Show = -1 Then
            xPath = xFileDialog.SelectedItems(1)
            If xPath <> "" Then
                xFile = Dir(xPath & "\*.*")
                j = 0
               Do While xFile <> ""
                   If UCase(Right(xFile, 3)) = "PNG" Or _
                       UCase(Right(xFile, 3)) = "TIF" Or _
                       UCase(Right(xFile, 3)) = "JPG" Or _
                       UCase(Right(xFile, 3)) = "GIF" Or _
                       UCase(Right(xFile, 3)) = "BMP" Then
                       xFileName = Left(xFile, Len(xFile) - 4)
                       If j Mod 6 = 0 Then
                           Set oSlide = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, ppLayoutBlank)
                       End If
                       i = j Mod 3
                       If j Mod 6 < 3 Then
                          Set oShape = oSlide.Shapes.AddPicture(FileName:=xPath & "" & xFile, _
                          LinkToFile:=msoFalse, _
                          SaveWithDocument:=msoTrue, _
                          Left:=100 + i * 150, _
                          Top:=100, _
                          Width:=120, _
                          Height:=90)
                      Else
                           Set oShape = oSlide.Shapes.AddPicture(FileName:=xPath & "" & xFile, _
                           LinkToFile:=msoFalse, _
                           SaveWithDocument:=msoTrue, _
                           Left:=100 + i * 150, _
                           Top:=250, _
                           Width:=120, _
                           Height:=90)
                       End If
                       oShape.Name = xFileName
                       Set oShape = oSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, _
                       Left:=100 + i * 150, _
                       Top:=oShape.Top + oShape.Height + 10, _
                       Width:=120, _
                       Height:=20)
                       oShape.TextFrame.TextRange.Text = xFileName
                       j = j + 1
                   End If
                   xFile = Dir()
                Loop
            End If
        End If
    End Sub

    Quote Originally Posted by June7 View Post
    Note for future: post code between CODE tags to retain indentation and readability.

    1. I don't think InlineShapes is valid in PP. Use Shapes. I doubt the exact same code can be used for both Word and PP.

    2. Don't see why not. Example of adding image:
    With .Shapes.AddShape(msoShapeRectangle, 360, 121, 220, 110) 'photo
          .Fill.UserPicture "image path\name"
    End With
    3. Use string manipulation functions to truncate filename: Left(xFile, InStrRev(xFile, ".") - 1).

    4. Try: .ParagraphFormat.Alignment = wdAlignParagraphCenter
    Last edited by Aussiebear; 03-02-2025 at 04:33 AM. Reason: added code tags to supplied code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •