如果我的 Outlook 目录集中不存在一个文件夹,我会使用以下代码创建一个文件夹。
Private Sub addOutlookFolderIfNotExists()
Set apOutlook = CreateObject("Outlook.Application")
apOutlook.Session.Logon
Dim myNameSpace As Outlook.Namespace
Dim myFolder As Outlook.Folder
Dim myNewFolder As Outlook.Folder
Set myNameSpace = apOutlook.GetNamespace("MAPI")
Set myFolder =
myNameSpace.GetDefaultFolder(olFolderInbox).Parent.Folders("Estimates")
For i = 1 To myFolder.Folders.Count
If myFolder.Folders.Item(i).Name = "Testing" Then
Exit Sub
End If
Next
addOutlookFolderIfNotExists = myFolder.Folders.Add("Testing")
End Sub
之后我想使用文件夹的属性。我想返回刚刚创建的 MAPIFolder 对象。我将 sub 更改为如下所示的函数。
Private Function addOutlookFolderIfNotExists() As MAPIFolder
Set apOutlook = CreateObject("Outlook.Application")
apOutlook.Session.Logon
Dim myNameSpace As Outlook.Namespace
Dim myFolder As Outlook.Folder
Dim myNewFolder As Outlook.Folder
Set myNameSpace = apOutlook.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox).Parent.Folders("Estimates")
For i = 1 To myFolder.Folders.Count
If myFolder.Folders.Item(i).Name = "Testing" Then
'Debug.Print TypeName(myFolder.Folders.Item(i))
addOutlookFolderIfNotExists = myFolder.Folders.Item(i)
Exit Function
End If
Next
addOutlookFolderIfNotExists = myFolder.Folders.Add("Testing")
End Function
这会返回一个错误
vba object variable or with block variable not set
但我不知道它指的是什么。
最佳答案
你做错了。甚至 For
循环不正确。设置或分配对象的正确方法是使用命令 SET
这是你正在尝试的吗?
Private Function addOutlookFolderIfNotExists() As MAPIFolder
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.Folder
Dim myNewFolder As Outlook.Folder
Dim i As Long
Set apOutlook = CreateObject("Outlook.Application")
apOutlook.Session.Logon
Set myNameSpace = apOutlook.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox).Parent.Folders("Estimates")
For i = 1 To myFolder.Folders.Count
If myFolder.Folders.Item(i).Name = "Testing" Then
'~~> Set the folder
Set addOutlookFolderIfNotExists = myFolder.Folders.Item("Testing")
Exit Function
End If
Next
'~~> Create the folder
myFolder.Folders.Add ("Testing")
'~~> Set the folder
Set addOutlookFolderIfNotExists = myFolder.Folders.Item("Testing")
End Function
您也可以在没有
For
的情况下执行上述操作环形。我们将使用 On Error Resume Next
取而代之的是。Private Function addOutlookFolderIfNotExists() As MAPIFolder
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.Folder
Dim myNewFolder As Outlook.Folder
Dim i As Long
Set apOutlook = CreateObject("Outlook.Application")
apOutlook.Session.Logon
Set myNameSpace = apOutlook.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox).Parent.Folders("Estimates")
'~~> Create the folder if it doesn't exists
'~~> If it exists then suppress the error message and continue
On Error Resume Next
myFolder.Folders.Add ("Testing")
On Error GoTo 0
'~~> Set the folder
Set addOutlookFolderIfNotExists = myFolder.Folders.Item("Testing")
End Function
关于excel - 使用 Excel VBA 返回 Outlook MAPIFolder 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53952752/