Outlook对象库不会在版本12和14之间切换

For*_*oop 7 outlook vba outlook-vba outlook-2007 outlook-2010

我在网络共享上有一个.dotm模板文件.有些宏引用了Word,Office和Outlook对象库.我们使用两个不同的平台,Windows XP和Windows 7,以及Microsoft Office 2007和Office 2010.当用户打开模板文件时,Word和Office的引用会自动调整(也就是说,它们被设置为Microsoft Word 12对象)库或Microsoft Word 14对象库根据需要),并且宏运行没有问题.

Microsoft Outlook对象库从版本12到14正常切换.它无法从版本14正确切换到12.在这种情况下,它会给出找不到库的错误.这是一个错误吗?有解决方法吗?我忽视的东西?

Jac*_*ern 3

对于每个循环,

看来你的问题已经基本得到解答了。为了清楚起见,我仅添加一些信息,并为这个问题提供答案。Microsoft 论坛上的一位用户 Ossiemac 指出,正如 Siddarth Rout 所说,LateBinding 是最佳选择。正如 Siddarth 所暗示的那样,这意味着您不必担心引用。

Ossiemac 提供了一些在发送电子邮件时使用 LateBinding 的示例代码,我已将其重新格式化并放置在此处:

Private Sub btnLateBindMethod_Click()
    ' Variables used for LateBinding
    Dim objOutlook As Object    'Outlook.Application  
    Dim objEmail As Object      'Outlook.MailItem     
    Dim objNameSpace As Object  'Outlook.NameSpace    
    Const OutLookMailItem As Long = 0    'For Late Binding
    Const OutLookFolderInbox As Long = 6 'For Late Binding
    Const OutLookFormatHTML As Long = 2  'For Late Binding
    Dim strSubject As String
    Dim strAddress As String


On Error Resume Next
Set objOutlook = GetObject(, "Outlook.Application")
On Error GoTo 0     

    If objOutlook Is Nothing Then
        Set objOutlook = CreateObject("Outlook.Application")
        Set objNameSpace = objOutlook.GetNamespace("MAPI")
        objNameSpace.GetDefaultFolder(OutLookFolderInbox).Display
    End If

Set objEmail = objOutlook.CreateItem(OutLookMailItem)

strSubject = "Hello World"

    With objEmail

        '.To = strToAddress  'Commented to prevent accidental send

        .Subject = strSubject

        .BodyFormat = OutLookFormatHTML

        .Display

        'Full Name of window can change depending on Tools -> Options -> Mail Format
        'Changing this option for outgoing mail changes the window name.
        'However, AppActivate appears not to require entire name but needs up to end
        'of - Message which is included in heading following the Subject string
        'irrespective of the Mail Format option chosen.
        AppActivate (strSubject & " - Message")

    End With    
End Sub
Run Code Online (Sandbox Code Playgroud)

Jimmy Pena 有一篇文章讨论了EarlyBinding 和 LateBinding 的对比-

〜乔尔