我在网络共享上有一个.dotm模板文件。有一些宏,它们引用了Word,Office和Outlook对象库。我们使用Windows XP和Windows 7这两个不同的平台,以及Microsoft Office 2007和Office2010。当用户打开模板文件时,Word和Office的引用会自动调整,并因此进行相应的调整(即,它们被设置为Microsoft Word 12 Object库或Microsoft Word 14对象库(如果需要),宏运行就没有问题。

Microsoft Outlook对象库从版本12正确切换到14。它没有从版本14正确切换到12。在这种情况下,它会给出错误消息,即找不到该库。这是一个错误吗?有解决方法吗?我忽略了什么?

最佳答案

对于每个循环,

您的问题似乎已经得到了解答。为了清楚起见,我将仅添加一些信息,并为该问题提供答案。微软论坛的一个用户Ossiemac注意到Siddarth Rout所说的LateBinding was the way to go。正如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

吉米·佩纳(Jimmy Pena)的一篇文章讨论了contrast of EarlyBinding and LateBinding-

〜JOL

关于vba - Outlook对象库不会在版本12和14之间切换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11196432/

10-09 06:30