7

I have a .dotm template file on a network share. There are macros with references to the Word, Office, and Outlook object libraries. We use two different platforms, Windows XP and Windows 7, along with Microsoft Office 2007 and Office 2010. When users open the template file the references for Word and Office adjust automatic and accordingly (that is, they’re set to Microsoft Word 12 Object Library or Microsoft Word 14 Object Library as needed), and the macros run without a problem.

Microsoft Outlook Object Library switches properly from version 12 to 14. It does not switch properly from version 14 to 12. In that case, it gives the error that the libary is not found. Is this a bug? Is there a workaround? Something I’m overlooking?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
ForEachLoop
  • 2,508
  • 3
  • 18
  • 28
  • 5
    I believe you are using `EarlyBinding` Have you considered using `LateBinding (LB)`? The benefit of LB is that you don't need to worry about different versions being run on different computers. The code will always bind with the version which is present in the pc where the code is being run. See t his link: http://support.microsoft.com/kb/245115 – Siddharth Rout Jun 25 '12 at 22:46
  • Thanks. I’m keeping that option in reserve. Rather, I’m asking why the other two references update automatically in either direction but that Outlook specifically, does not. If that’s the way it is, I’ll have to use late binding, ye – ForEachLoop Jun 26 '12 at 14:11
  • I ran into the same problem, using Excel VBA. I also note that (early bound) references to Microsoft Office and Microsoft Excel libraries are automatically switched between versions 12 and 14, but the reference to Microsoft Outlook doesn't. That is, it does auomatically change from 12 to 14, but never back to 12. – comecme Jul 05 '12 at 06:42
  • Ossiemac of Microsoft.com noted that EarlyBinding was the way to go, as Siddharth Rout has already noted. He also provided code for the EarlyBinding, which meant that references were not necessary in that particular case: – Scott Conover Jul 27 '12 at 21:46
  • ...I will detail a bit more in an answer, even though your question is pretty much answered already by Siddharth Rout :) – Scott Conover Jul 27 '12 at 22:00

1 Answers1

3

ForEachLoop,

It appears that your question has largely been answered. I will merely add a bit of information for clarity's sake, and to provide this question with an answer. A user on the Microsoft Forums, Ossiemac, noted that LateBinding was the way to go, as has been stated by Siddarth Rout. As implied by Siddarth, that means you do not have to worry about references.

Ossiemac provided some sample code for using the LateBinding in the sending of an email, which I have reformatted and placed here:

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 has an article discussing the contrast of EarlyBinding and LateBinding -

~JOL

Scott Conover
  • 1,421
  • 1
  • 14
  • 27
  • 1
    Thanks. I did end up using late binding. It's a great article! The solution's awkward since you lose a lot of the cool features of the editor. Also, if you use a compiler (such as Visual Studio) you can workaround this, for example, by having Word and Excel 2007 and 2010 loaded side by side and choose which version want to compile to. Unfortunately, two versions of Outlook can't be loaded side by side, so you're stuck with late binding anyhow. – ForEachLoop Jul 31 '12 at 15:16
  • Fascinating! I am glad it functioned, and the concept of passing between different versions of word for compilation by using VS bears further exploration in the future... Thanks for the details on the implementation! ~JOL – Scott Conover Jul 31 '12 at 15:23
  • Thanks for this, this was very helpful. I had to Google for all of the constants I needed, as I'm dealing with calendar appointments instead of mail items, but this got me up and running again. I'm having to do this because of different versions of Outlook. – Ben Strombeck Feb 18 '16 at 16:52