我試圖使用此頁面上的功能:http://www.outlookcode.com/d/code/getfolder.htm使用文件夾路徑導航到文件夾。 (我會將該代碼複製到這個問題的底部 - 我原樣使用它,根本沒有修改)。我需要使用它的原因是Outlook中的默認收件箱與我需要的收件箱不同積極點。我通過右鍵點擊相關收件箱並打開屬性並查看位置,從而瞭解相關收件箱的路徑。從文件夾路徑獲取Outlook中的MAPI文件夾
這是我使用的代碼:
Set objOutlook = CreateObject("Outlook.Application", "localhost")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set Inbox = GetFolder("\\[email protected]\inbox")
Debug.Print Inbox '<-- This fails
Set InboxItems = Inbox.Items '<-- This also fails
InboxItems.SetColumns ("SentOn")
這將返回運行時錯誤91,對象未設置變量或With塊變量。
我不知道這是什麼意思。如果你能幫我解決這個錯誤,那會很棒,如果你有辦法完全避免這個問題,那也是很棒的。謝謝!
Public Function GetFolder(strFolderPath As String)As MAPIFolder
' strFolderPath needs to be something like
' "Public Folders\All Public Folders\Company\Sales" or
' "Personal Folders\Inbox\My Folder"
Dim objApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim colFolders As Outlook.Folders
Dim objFolder As Outlook.MAPIFolder
Dim arrFolders() As String
Dim I As Long
On Error Resume Next
strFolderPath = Replace(strFolderPath, "/", "\")
arrFolders() = Split(strFolderPath, "\")
Set objApp = Application
Set objNS = objApp.GetNamespace("MAPI")
Set objFolder = objNS.Folders.Item(arrFolders(0))
If Not objFolder Is Nothing Then
For I = 1 To UBound(arrFolders)
Set colFolders = objFolder.Folders
Set objFolder = Nothing
Set objFolder = colFolders.Item(arrFolders(I))
If objFolder Is Nothing Then
Exit For
End If
Next
End If
Set GetFolder = objFolder
Set colFolders = Nothing
Set objNS = Nothing
Set objApp = Nothing
End Function