按降序插入文件夹中的多张图片

Insert multiple Pictures from Folder in descending order

我从一个文件夹中插入了一些图片。该程序应该从文件夹的顶部开始并按降序插入图片,但它没有。

前 3-5 张图片在演示文稿中排在最后,而其他所有图片都井然有序。

Sub createTransModel()

    Dim oSlide As Slide
    Dim oPicture As Shape
    Dim myFile As String
    Dim myFolder As String
    Dim pptLayout As CustomLayout
    Dim fileName As String
    Dim rotSlide As Slide

    Set pptLayout = ActivePresentation.Slides(1).CustomLayout
    myFolder = GetFolderPath()
    myFile = Dir(myFolder & "*.png")

    Do While myFile <> ""
        Set oSlide = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, _
        ppLayoutBlank)
        Set oPicture = oSlide.Shapes.AddPicture(myFile, _
          msoFalse, msoTrue, 1, 1, _
          ActivePresentation.PageSetup.SlideWidth, _
          ActivePresentation.PageSetup.SlideHeight)

        myFile = Dir
    Loop

    fileName = inputBox("Please enter the filename")
    ActivePresentation.SaveAs (fileName & ".pps")
End Sub


Public Function GetFolderPath() As String


    Dim myFile As Object
    Dim fileSelected As String
    Dim path As String
    Dim objPPT As Object
    Dim i As Integer
    Dim folderFromPath As String
    Dim directory As String

    directory = "M:\tm\public\Conti_Anlage\Voith Proben"

    Set myFile = Application.FileDialog(msoFileDialogOpen)
    With myFile
        .InitialFileName = directory
        .Title = "Choose File"
        .AllowMultiSelect = False
        If .Show <> -1 Then
        Exit Function
        End If
        fileSelected = .SelectedItems(1)
    End With


    For i = Len(fileSelected) To 1 Step -1
        If Mid(fileSelected, i, 1) = "\" Then
            folderFromPath = Left(fileSelected, i)
            Exit For
        End If
    Next

    GetFolderPath = folderFromPath

End Function

这里有一些东西
1. 要解决您的顺序问题,您可以获取文件夹中的所有文件(即使用 'For' 循环:For Each oFile in oMyFolder.Files),然后按照您想要的方式对它们进行排序(可能在数组中)。现在您可以添加它们了。
2. 您的 'GetFolderPath' 功能:据我所知,您希望用户 select 一个文件,然后返回 selected 文件的文件夹。您可以只使用“Application.FileDialog(msoFileDialogFolderPicker)”。这将要求用户 select 一个文件夹。这样您就不必担心提取文件夹。如果您仍想获取 selected 文件的文件夹,请查看“File System”对象。您可以使用它来获取文件夹(即 filesystemobject.GetParentFolderName(MyFile)

您在 Windows 文件浏览器 window 中看到的文件顺序取决于您的文件浏览器设置。文件可能按名称字母顺序、大小顺序或以各种其他方式排序。这是文件资源管理器所做的,与文件在磁盘上出现的实际顺序无关。

Dir$ 在重复调用时会按照文件在磁盘上出现的顺序为您提供文件。如果您希望它们按特定顺序排列,则必须对它们进行排序,或者按照您希望它们由 Dir 返回的顺序将它们复制到一个文件夹中。