我从文件夹中插入了许多图片。该程序应该从文件夹的顶部开始,并按降序插入图片,但事实并非如此。
前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
答案 0 :(得分:0)
这里有几件事
1.要解决您的订单问题,您可以获取文件夹中的所有文件(即使用'For'循环:For Each oFile in oMyFolder.Files
),然后按照您希望的方式(可能在数组中)对它们进行排序。现在你可以添加它们
2.你的“GetFolderPath”功能:从我所看到的,你希望用户选择一个文件,然后你将返回所选文件的文件夹。你可以使用'Application.FileDialog(msoFileDialogFolderPicker)
'。这将要求用户选择一个文件夹。这样您就不必担心提取文件夹了。如果您仍想获取所选文件的文件夹,请查看“File System”对象。您可以使用它来获取文件夹(即filesystemobject.GetParentFolderName(MyFile)
)
答案 1 :(得分:0)
您在Windows文件资源管理器窗口中看到的文件顺序取决于您的文件资源管理器设置。文件可能按名称,大小顺序按字母顺序显示或以其他各种方式排序。这是文件浏览器正在做的,与文件在磁盘上显示的实际顺序无关。
重复调用时,Dir $会按照它们在磁盘上显示的顺序为您提供文件。如果您按特定顺序需要它们,则必须对它们进行排序,或者按照Dir返回的顺序将它们复制到文件夹中。