添加一个内循环:
Set fso = CreateObject("Scripting.FileSystemObject")
For i = 0 To 9
For Each f In fso.GetFolder("F:\somedir").Files
If f.Type = "Bitmap-Image" Then
If CInt(Left(f.Name, 1)) = i Then
ActiveWindow.Selection.SlideRange.Shapes.AddPicture(f.Path)
End If
End If
Next
Next
可以像这样添加检查以确保第一个字符是数字:
If f.Type = "Bitmap-Image" Then
c = Left(f.Name, 1)
If IsNumeric(c) Then
If CInt(c) = i Then
'...
End If
End If
End If
处理图像的另一个(更复杂的)选项是将它们枚举到断开连接的记录集中,然后过滤记录集。这样您就不必在每个循环周期中枚举 images 文件夹中的所有文件。
Set fso = CreateObject("Scripting.FileSystemObject")
Set rs = CreateObject("ADOR.Recordset")
rs.Fields.Append "Name", 200, 80
rs.Fields.Append "Path", 200, 255
rs.Open
For Each f In fso.GetFolder("F:\somedir").Files
If f.Type = "Bitmap-Image" Then
rs.AddNew
rs("Name").Value = f.Name
rs("Path").Value = f.Path
rs.Update
End If
Next
For i = 0 To 9
rs.Filter = "Name LIKE '" & i & "%'"
rs.MoveFirst
Do Until rs.EOF
ActiveWindow.Selection.SlideRange.Shapes.AddPicture(rs("Path").Value)
rs.MoveNext
Loop
Next