命名空间。 Logon 方法 (Outlook)NameSpace.Logon method (Outlook)

使用户登录到 MAPI,获取 MAPI 会话。Logs the user on to MAPI, obtaining a MAPI session.

语法Syntax

expressionexpression. Logon( _Profile_ , _Password_ , _ShowDialog_ , _NewSession_ )

_表达式_一个代表NameSpace对象的变量。expression A variable that represents a NameSpace object.

参数Parameters

名称Name 必需/可选Required/Optional 数据类型Data type 说明Description
ProfileProfile 可选Optional VariantVariant 要用于会话的 MAPI 配置文件名称, 以字符串形式表示。The MAPI profile name, as a String, to use for the session. 指定要用于当前会话的默认配置文件为空字符串。Specify an empty string to use the default profile for the current session.
PasswordPassword 可选Optional VariantVariant 与配置文件关联的密码 (如果有), 以字符串形式。The password (if any), as a String, associated with the profile. 此参数存在仅用于向后兼容性和出于安全原因,建议不要使用。This parameter exists only for backwards compatibility and for security reasons, it is not recommended for use. Microsoft Outlook 将提示用户在大多数系统配置中指定密码。Microsoft Outlook will prompt the user to specify a password in most system configurations. 这是您的登录密码,不应该混淆与 PST 密码。This is your logon password and should not be confused with PST passwords.
ShowDialogShowDialog 可选Optional VariantVariant True 指示显示的 MAPI 登录对话框,以允许用户选择的 MAPI 配置文件。True to display the MAPI logon dialog box to allow the user to select a MAPI profile.
NewSessionNewSession 可选Optional VariantVariant 若要创建新的 Outlook 会话。True to create a new Microsoft Outlook session. 由于不能在 Outlook 中创建多个会话,此参数应指定为仅当不存在会话,则返回 True。Since multiple sessions cannot be created in Outlook, this parameter should be specified as True only if a session does not already exist.

说明Remarks

仅当 Outlook 尚未运行时, 才能使用登录方法登录到特定的配置文件。Use the Logon method only to log on to a specific profile when Outlook is not already running. 这是因为只有一个 Outlook 进程可以运行一次,并且 Outlook 进程使用一个配置文件,并支持只有一个 MAPI 会话。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. 当用户启动 Outlook 时第二次时,Outlook 该实例同一 Outlook 进程内运行,不会创建新的进程,和使用相同的配置文件。When users start Outlook a second time, that instance of Outlook runs within the same Outlook process, does not create a new process, and uses the same profile.

如果尚未运行 Outlook,使用这种方法不创建新的 Outlook 会话或更改为一个不同的当前配置文件。If Outlook is already running, using this method does not create a new Outlook session or change the current profile to a different one.

如果 outlook 未运行, 并且您仅希望使用默认配置文件启动 outlook, 请不要使用Logon方法。If Outlook is not running and you only want to start Outlook with the default profile, do not use the Logon method. 下面的代码示例中, InitializeMAPI所示一个更好的替代方案: 首先,实例化的 Outlook应用程序对象,然后引用默认文件夹如收件箱。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. 这有不利的一面,初始化 MAPI 使用默认配置文件,并使对象模型完全正常。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

从 outlook 2010 开始, 如果您有多个配置文件, 则已将 Outlook 配置为始终使用默认配置文件, 使用Logon方法登录到默认配置文件, 而不提示用户, 用户将收到选择配置文件的提示不管怎样.Starting in Outlook 2010, if you have multiple profiles, you have configured Outlook to always use a default profile, and you use the Logon method to log on to the default profile without prompting the user, the user will receive a prompt to choose a profile anyway. 若要避免此问题,请使用 Logon方法中;使用前面的 InitializeMAPI示例中改为建议的解决办法。To avoid this behavior, do not use the Logon method; use the workaround suggested in the preceding InitializeMAPI example instead.

示例Example

此 Visual Basic for Applications 示例使用Logon方法登录到新的会话, 显示对话框以验证配置文件名称和输入密码。This Visual Basic for Applications example uses the Logon method to log on to a new session, displaying the dialog box to verify the profile name and enter password.

Sub StartOutlook() 
    Dim myNameSpace As Outlook.NameSpace 
  
    Set myNameSpace = Application.GetNamespace("MAPI") 
    myNameSpace.Logon "LatestProfile", , True, True 
End Sub

另请参阅See also

命名空间对象NameSpace Object

如何︰ 获取和登录到 Outlook 实例How to: Obtain and Log On to an Instance of Outlook

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.