用文件和缩略图填充列表视图表单

时间:2019-04-16 11:14:29

标签: vba powerpoint powerpoint-vba

我正在尝试为PowerPoint创建一个加载项,以使用户可以在下拉列表中选择目录,然后获取PowerPoint幻灯片列表,并以列表视图形式显示缩略图。然后,用户应该能够选择缩略图并将幻灯片添加到他/她正在处理的当前演示文稿中。 (类似于从幻灯片直接访问幻灯片的个人幻灯片库。)

我有填写列表框的通用代码。但是出现TypeError,我不知道如何解决。

Private Sub CommandButton1_Click()

    Dim fs, f, f1, fc, s As String
    Dim folderspec As String

    folderspec = "U:\PowerPointFiles\"

    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(folderspec)
    Set fc = f.SubFolders

    ComboBox1.Clear

    For Each f1 In fc
        ComboBox1.AddItem f1.Name

    Next f1

    'ComboBox1.Activate    --> doesnt' work

    'Application.SendKeys "^{F4}" --> doesnt' work

End Sub


Private Sub ComboBox1_Change()

    Dim fs, f, f1, MyFiles, s As String
    Dim MyFolder As String

    MyFolder = "U:\Methoden\power point trials\Addin Projekte\Slide Library Addin\" & ComboBox1

    MyFiles = Dir(MyFolder & "\*.pptx")

    ListView1.ListItems.Clear

    Do While MyFiles <> ""

        ListView1.ListItems.Add (MyFiles)  --> Type mismatch
        MyFiles = Dir

    Loop


End Sub

结果应为ListView中包含链接的.ppt文件列表。 由此,我需要创建代码以添加缩略图。

2 个答案:

答案 0 :(得分:0)

对不起,您错过了已经标记了问题代码行的事实。

我已经注释掉了原始代码的几行,并做了一些细微改动:

Private Sub CommandButton1_Click()

' You don't use all these variables, so no need to dim them here
'    Dim fs, f, f1, MyFiles, s As String
Dim MyFiles As String
    Dim MyFolder As String

    'MyFolder = "U:\Methoden\power point trials\Addin Projekte\Slide Library Addin\" & ComboBox1
' Substituted a folder that exists on my system and changed the filespec
' form *.PPTX to *.* to ensure some results at my end while testing.
MyFolder = "C:\Temp"

    MyFiles = Dir(MyFolder & "\*.*")

'    ListView1.ListItems.Clear
ListBox1.Clear
    Do While MyFiles <> ""

        'ListView1.ListItems.Add (MyFiles) '--> Type mismatch
ListBox1.AddItem (MyFiles)
        MyFiles = Dir

    Loop

End Sub

答案 1 :(得分:0)

我找到了解决方法:

Private Sub ComboBox1_Change()

Dim strLoad As String
Dim lvwItem As ListItem
Dim MyFolder As String

ListView1.ListItems.Clear

MyFolder = "U:\Methoden\power point trials\Addin Projekte\Slide Library Addin\" & ComboBox1
strLoad = Dir(MyFolder & "\*.*")

Do While strLoad > vbNullString

   Set lvwItem = ListView1.ListItems.Add(, , strLoad)

   strLoad = Dir
Loop

End Sub