VBA/Excel/Access/Word/Outlook/Outlook NameSpace
Содержание
- 1 Accessing Default Folders within the NameSpace Object
- 2 Accessing Other Folders within the NameSpace Object
- 3 Creating a New Folder
- 4 Deleting a Folder
- 5 olFolderContacts
- 6 olFolderDeletedItems,
- 7 olFolderDrafts,
- 8 olFolderInbox,
- 9 olFolderJournal,
- 10 olFolderNotes,
- 11 olFolderOutbox,
- 12 olFolderSentMail,
- 13 olFolderTasks,
- 14 olPublicFoldersAllPublicFolders.
- 15 Returns the NameSpace and uses the CurrentUser property to display the name of the current user in a message box:
- 16 Understanding General Methods for Working with Outlook Objects
- 17 Using the NewMail Event and NewMailEx Event
Accessing Default Folders within the NameSpace Object
"olFolderCalendar,
Sub folder()
Dim myCal As MAPIFolder
Set myCal = Application.GetNamespace("MAPI") _
.GetDefaultFolder(FolderType:=olFolderCalendar)
End Sub
Accessing Other Folders within the NameSpace Object
"A list of all the folders contained in the namespace:
Sub List_All_NameSpace_Folders()
Dim myNS As NameSpace
Dim myFolder As MAPIFolder
Dim mySubfolder As MAPIFolder
strFolderList = "Your Outlook NameSpace contains these folders:"
Set myNS = Application.GetNamespace("MAPI")
With myNS
For Each myFolder In myNS.Folders
strFolderList = strFolderList & myFolder.Name & vbCr
For Each mySubfolder In myFolder.Folders
Debug.Print mySubfolder.Name
Next mySubfolder
Next myFolder
End With
End Sub
Creating a New Folder
Sub newFolder()
Application.GetNamespace("MAPI").GetDefaultFolder(olFolderTasks) _
.Folders.Add Name:="Personal Tasks", Type:=olFolderTasks
End Sub
Deleting a Folder
Sub delete()
Application.GetNamespace("MAPI").GetDefaultFolder(olFolderTasks) _
.Folders("Personal Tasks").Delete
End Sub
olFolderContacts
Sub folder()
Dim myCal As MAPIFolder
Set myCal = Application.GetNamespace("MAPI") _
.GetDefaultFolder(FolderType:=olFolderContacts)
End Sub
olFolderDeletedItems,
Sub folder()
Dim myCal As MAPIFolder
Set myCal = Application.GetNamespace("MAPI") _
.GetDefaultFolder(FolderType:=olFolderDeletedItems)
End Sub
olFolderDrafts,
Sub folder()
Dim myCal As MAPIFolder
Set myCal = Application.GetNamespace("MAPI") _
.GetDefaultFolder(FolderType:=olFolderDrafts)
End Sub
olFolderInbox,
Sub folder()
Dim myCal As MAPIFolder
Set myCal = Application.GetNamespace("MAPI") _
.GetDefaultFolder(FolderType:=olFolderInbox)
End Sub
olFolderJournal,
Sub folder()
Dim myCal As MAPIFolder
Set myCal = Application.GetNamespace("MAPI") _
.GetDefaultFolder(FolderType:=olFolderJournal)
End Sub
olFolderNotes,
Sub folder()
Dim myCal As MAPIFolder
Set myCal = Application.GetNamespace("MAPI") _
.GetDefaultFolder(FolderType:=olFolderNotes)
End Sub
olFolderOutbox,
Sub folder()
Dim myCal As MAPIFolder
Set myCal = Application.GetNamespace("MAPI") _
.GetDefaultFolder(FolderType:=olFolderOutbox)
End Sub
olFolderSentMail,
Sub folder()
Dim myCal As MAPIFolder
Set myCal = Application.GetNamespace("MAPI") _
.GetDefaultFolder(FolderType:=olFolderSentMail)
End Sub
olFolderTasks,
Sub folder()
Dim myCal As MAPIFolder
Set myCal = Application.GetNamespace("MAPI") _
.GetDefaultFolder(FolderType:=olFolderTasks)
End Sub
olPublicFoldersAllPublicFolders.
Sub folder()
Dim myCal As MAPIFolder
Set myCal = Application.GetNamespace("MAPI") _
.GetDefaultFolder(FolderType:=olPublicFoldersAllPublicFolders)
End Sub
Returns the NameSpace and uses the CurrentUser property to display the name of the current user in a message box:
Sub current()
MsgBox Application.GetNamespace("MAPI").CurrentUser
End Sub
Understanding General Methods for Working with Outlook Objects
Sub display()
Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Display
End Sub
Using the NewMail Event and NewMailEx Event
Sub Application_NewMail()
If MsgBox("You have new mail. Do you want to see your Inbox?", _
vbYesNo + vbInformation, "New Mail Alert") = vbYes Then
Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Display
End If
End Sub