我设置了一个全局字典对象来跟踪文件路径(键是“项目名称”,这是文件名的第一个单词)。该程序的工作方式是打开一个表单,然后用户可以打开文件对话框将文件添加到列表框中。完成后,它应该添加字典的名称和路径。
主要代码:
Public project_dict As Scripting.Dictionary
Public Sub excelToPPT()
Set project_dict = New Scripting.Dictionary
...
Dim u As UserForm1
Set u = New UserForm1
u.Show
...
End Sub
UserForm代码:
Private Sub cmdAddProject_Click()
Dim project_fd As FileDialog
Set project_fd = Application.FileDialog(msoFileDialogFilePicker)
Dim it As Variant
Dim path_split() As String
Dim f_split() As String
Dim f As String
With project_fd
.AllowMultiSelect = True
.Title = "Select project excel file(s)"
.Filters.Clear
If .Show = -1 Then
For Each it In .SelectedItems
path_split = Split(it, "\")
f_split = Split(path_split(UBound(path_split)))
f = f_split(0)
' WHERE ERROR OCCURS
project_dict.Add f, it
Me.ListBox1.AddItem f
Next it
End If
End With
End Sub
运行此错误时
project_dict.Add f, it
是不好的,因为project_dict不是我在这种情况下假设的范围问题的对象。有什么想法吗?
答案 0 :(得分:0)
通过将以下属性放在UserForm代码中并将字典传递给表单而不是使用全局变量来解决该问题。
Dim project_dict As Scripting.Dictionary
Public Property Get ProjectDictionary() As Scripting.Dictionary
Set ProjectDictionary = project_dict
End Property
Public Property Let ProjectDictionary(ByVal pd As Scripting.Dictionary)
Set project_dict = pd
End Property