共用方式為


How to Log On to a Specific Profile in Outlook 2010

You can use the Outlook object model to log on to a custom MAPI profile or the default profile. When you first start Outlook on a computer, if you would like to programmatically log on to a custom profile (that is different from the default profile), you can use the Logon method of the NameSpace object. Specify the profile name as an argument for the Profile parameter. Alternatively, you can specify the ShowDialog parameter as True to prompt the user to select a profile.

If Outlook is already running on a computer, you cannot change the current profile by starting another instance of Outlook on the same computer. Calling Logon and specifying a different profile does not change the current profile. This is because only one Outlook process can run at a time, and that Outlook process uses only one profile and supports only one MAPI session. Starting a subsequent instance of Outlook does not create a new process, but runs within the same initial Outlook process, and uses the same profile that the first Outlook instance uses.

Note that if you want to start Outlook and always log on to the default profile, you could call the Logon method. However, note that starting in Outlook 2010, even if you specify the ShowDialog parameter as False, Logon always prompts the user to select a profile. To avoid prompting the user, do not use the Logon method. A better alternative is shown in the following code example InitializeMAPI: first, instantiate the Outlook Application object, then reference a default folder such as the Inbox. This has the side effect of initializing MAPI to use the default profile and to make the object model fully functional.

Sub InitializeMAPI ()

' Start Outlook.
Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")

' Get a session object.
Dim olNs As Outlook.NameSpace
Set olNs = olApp.GetNamespace("MAPI")

' Create an instance of the Inbox folder.
' If Outlook is not already running, this has the side
' effect of initializing MAPI.
Dim mailFolder As Outlook.Folder
Set mailFolder = olNs.GetDefaultFolder(olFolderInbox)

' Continue to use the object model to automate Outlook.
End Sub