我已經解決了獲取完整文件夾列表的問題(代碼如下),但是如果您需要更多的幫助,請添加註釋,我會在我的答案上展開。
我不明白你會用ComboBox做什麼。因此,對於此示例,我創建了一個表單並添加了一個ListBox(稱爲ListBox1
)。下面的代碼將向下填充選定文件夾中所有文件夾名稱的列表框。請閱讀評論,看看你能做什麼(例如通過子文件夾遞歸 - 我知道在這種情況下不需要)。
如果您需要更多幫助或信息,請讓我知道。
Private Sub PopulateListBoxWithFolders()
Dim objApp As Outlook.Application
Dim objNamespace As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
' Clear current contents of listbox
ListBox1.Clear
Set objApp = New Outlook.Application
Set objNamespace = objApp.GetNamespace("MAPI")
' Allow user to select folder.
' Replace this with either objNamespace.GetDefaultFolder(...) or objNamespace.GetFolderFromID(...)
' to avoid the user having to select a folder
Set objFolder = objNamespace.PickFolder
' See if the user cancelled or no folder found
If Not objFolder Is Nothing Then
' Addition of true here recurses through all subfolders
ProcessFolder objFolder ', True
End If
End Sub
' Populates the ListBox with the folders. Optionally you can recurse all folders
Sub ProcessFolder(objStartFolder As Outlook.MAPIFolder, Optional blnRecurseSubFolders As Boolean = False, Optional strFolderPath As String = "")
Dim objFolder As Outlook.MAPIFolder
Dim i As Long
' Loop through the items in the current folder
For i = 1 To objStartFolder.Folders.Count
Set objFolder = objStartFolder.Folders(i)
' Populate the listbox
ListBox1.AddItem ListBox1.Text + objFolder.FolderPath
If blnRecurseSubFolders Then
' Recurse through subfolders
ProcessFolder objFolder, True, strFolderPath + "\" + objFolder.FolderPath
End If
Next
End Sub
如果您需要代碼來識別多選列表框中的選定項目,那麼它就是。爲了使ListBox的多選你應該在表格編輯器中MultiSelect
屬性設置爲1 - fmMultiSelectMulti
Private Sub btnOK_Click()
Dim i As Long
' Loop through all items in the listbox identifying those that are selected
With ListBox1
For i = 0 To .ListCount - 1
If .Selected(i) Then
' Here goes the code to act on the selected item
' In the example below it outputs to the Immediate window
Debug.Print .List(i)
End If
Next i
End With
End Sub
來源
2013-03-28 14:49:14
Jim
感謝吉姆,我給這個一展身手。我不擅長創建用戶界面,但我會嘗試使其工作。 – 2013-04-03 01:54:10