如何使用打开对话框将 Power Point 中的批量图片从文件夹导入到 Select 整个文件夹并为每张图片制作新幻灯片

How to import bulk pictures in Power Point from folder using Open Dialogue Box to Select Entire Folder and Make New Slide for each Picture

我正在尝试使 Powerpoint 演示自动化。我在网上找到了一个代码。此代码运行良好,但它适用于代码中的静态路径。我想使用 OpenFolder 对话框来实现它。这个想法是,当我点击按钮导入图片时,文件对话框应该打开并且我 select 文件夹。文件夹内的图片自动和图片的大小应该自动适合幻灯片。此过程完成后,幻灯片自动开始使用淡入淡出动画显示图片。代码如下

Sub main()
Dim i As Integer
Dim arrFilesInFolder As Variant
arrFilesInFolder = GetAllFilesInDirectory("C:\Users\Admin\OneDrive\Pictures\Screenshots")

For i = LBound(arrFilesInFolder) To UBound(arrFilesInFolder)
    Call AddSlideAndImage(arrFilesInFolder(i))
Next
End Sub
Private Function GetAllFilesInDirectory(ByVal strDirectory As String) As Variant
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim arrOutput() As Variant
Dim i As Integer

'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder(strDirectory)
ReDim arrOutput(0)
i = 1
'loops through each file in the directory and prints their names and path
For Each objFile In objFolder.Files
 
    'print file path
    arrOutput(i - 1) = objFile.Path
    ReDim Preserve arrOutput(UBound(arrOutput) + 1)
    i = i + 1
Next objFile
   ReDim Preserve arrOutput(UBound(arrOutput) - 1)
GetAllFilesInDirectory = arrOutput
End Function

Private Function AddSlideAndImage(ByVal strFile As String)
Dim objPresentaion As presentation
Dim objSlide As slide

Set objPresentaion = ActivePresentation

Set objSlide = objPresentaion.Slides.Add(1, PpSlideLayout.ppLayoutChart)
Call objSlide.Shapes.AddPicture(strFile, msoCTrue, msoCTrue, 100, 100, 650, 450)
End Function

请高手指点,我哪里做错了。谢谢

您似乎需要用提示用户输入的代码替换硬编码文件路径。似乎 Application.FileDialog 应该可以让你到达那里:

Dim path As String
With Application.FileDialog(Type:=msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    If .Show Then
        path = .SelectedItems(1)
    Else
        'user cancelled, bail out:
        Exit Sub
    End If
End With

Dim arrFilesInFolder As Variant
arrFilesInFolder = GetAllFilesInDirectory(path)
'...rest of the code...