none
A Word doc from an outside source is missing a library? RRS feed

  • Question

  • Some of my users have been getting Word documents from a government office that generates VBA errors when opened on our machines. I looked at one and it was complaining about Outlook being unknown. So I looked through the references, checked off the reference to our Outlook library, and they work after that. I told the users how to do that and all was well. But now they're getting lots more of these documents and that workaround is not making them happy.

    I'm guessing that the documents are being created on a system with a more recent version of Office than our 2003 vintage and whatever it's referencing doesn't exist on our system - although I didn't see any missing references when I scrolled through the References section. Anyway, is there something we can push out that will allow them to open these documents without needing to set the reference themselves? And that will not mess up Office 2003?

    Thursday, June 14, 2012 6:14 PM

Answers

  • Hello Dave,

    I don't know what sort of relationship you have with this Government Office but if you can talk them around to altering the VBA code then the problem will go away.

    When calling other applications from VBA it can be done with Early Binding that requires the References to be set. However, if Late Binding is used then the References are not required.

    The following 2 subs are examples from my library. The first one uses Early Binding and requires the References. The second one has been edited for Late Binding and includes comments adjacent to the changes that are required.

    Hopefully you can negotiate the change.

    Sub SendEmailEarlyBinding()
        Dim objOutlook As Outlook.Application
        Dim objNameSpace As Outlook.NameSpace
        Dim objEmail As Outlook.MailItem
       
        Dim strAddress As String
        Dim strSubject As String
        Dim strBody As String
       
        'Test if Outlook already open by using GetObject
        Set objOutlook = Nothing    'In case already set with previous code
        On Error Resume Next
        Set objOutlook = GetObject(, "Outlook.Application")
        On Error GoTo 0 'Re-set error trapping ASAP
       
        If objOutlook Is Nothing Then   'GetObject did not work so Create
            Set objOutlook = CreateObject("Outlook.Application")
            Set objNameSpace = objOutlook.GetNamespace("MAPI")
            objNameSpace.GetDefaultFolder(olFolderInbox).Display
        End If
       
        Set objEmail = objOutlook.CreateItem(olMailItem)
       
        strAddress = "xxxxxx@yyyyyyyyyy.net.au"     'Edited for privacy reasons
        strSubject = "Test Email Early Binding"
        strBody = "This is a test sending an email from Word using Early Binding"
        With objEmail
            .To = strAddress
            .Subject = strSubject
            .BodyFormat = olFormatHTML
            .HTMLBody = strBody
            .Send
        End With
       
        Set objEmail = Nothing
        Set objNameSpace = Nothing
        Set objOutlook = Nothing

    End Sub

     

    Sub SendEmailLateBinding()
        Dim objOutlook As Object    'Object in lieu of Outlook.Application
        Dim objNameSpace As Object  'Object in lieu of Outlook.NameSpace
        Dim objEmail As Object      'Object in lieu of Outlook.MailItem
        Const MailItem = 0          'Replaces constant olMailItem
        Const FolderInbox = 6       'Replaces constant olFolderInbox
        Const FormatHTML = 2        'Replaces constant olFormatHTML
        'If any other constants being used with Early Binding then they
        'will need to have constants created for them also. When the
        'Reference is unchecked select Debug -> Compile and that will
        'pick up any constants that need attention.
       
        Dim strAddress As String
        Dim strSubject As String
        Dim strBody As String
       
        'Test if Outlook already open by using GetObject
        Set objOutlook = Nothing    'In case already set with previous code
        On Error Resume Next
        Set objOutlook = GetObject(, "Outlook.Application")
        On Error GoTo 0 'Re-set error trapping ASAP
       
        If objOutlook Is Nothing Then   'GetObject did not work so Create
            Set objOutlook = CreateObject("Outlook.Application")
            Set objNameSpace = objOutlook.GetNamespace("MAPI")
            objNameSpace.GetDefaultFolder(FolderInbox).Display
        End If
       
        'Set objEmail = objOutlook.CreateItem(olMailItem)
        Set objEmail = objOutlook.CreateItem(MailItem)
       
        strAddress = "xxxxxx@yyyyyyyyyy.net.au"    'Edited for privacy reasons
        strSubject = "Test Email Late Binding"
        strBody = "This is a test sending an email from Word using Late Binding"
        With objEmail
            .To = strAddress
            .Subject = strSubject
            .BodyFormat = FormatHTML
            .HTMLBody = strBody
            .Send
        End With
       
        Set objEmail = Nothing
        Set objNameSpace = Nothing
        Set objOutlook = Nothing

    End Sub


    Regards, OssieMac

    Monday, June 18, 2012 7:19 AM

All replies

  • Hi Dave,

    About the errors " complaining about Outlook being unknown", will you please show us exact and complete error message?  That will help a lot.

    thanks,


    Forrest Guo | MSDN Community Support | Feedback to manager

    Monday, June 18, 2012 3:20 AM
    Moderator
  • Hello Dave,

    I don't know what sort of relationship you have with this Government Office but if you can talk them around to altering the VBA code then the problem will go away.

    When calling other applications from VBA it can be done with Early Binding that requires the References to be set. However, if Late Binding is used then the References are not required.

    The following 2 subs are examples from my library. The first one uses Early Binding and requires the References. The second one has been edited for Late Binding and includes comments adjacent to the changes that are required.

    Hopefully you can negotiate the change.

    Sub SendEmailEarlyBinding()
        Dim objOutlook As Outlook.Application
        Dim objNameSpace As Outlook.NameSpace
        Dim objEmail As Outlook.MailItem
       
        Dim strAddress As String
        Dim strSubject As String
        Dim strBody As String
       
        'Test if Outlook already open by using GetObject
        Set objOutlook = Nothing    'In case already set with previous code
        On Error Resume Next
        Set objOutlook = GetObject(, "Outlook.Application")
        On Error GoTo 0 'Re-set error trapping ASAP
       
        If objOutlook Is Nothing Then   'GetObject did not work so Create
            Set objOutlook = CreateObject("Outlook.Application")
            Set objNameSpace = objOutlook.GetNamespace("MAPI")
            objNameSpace.GetDefaultFolder(olFolderInbox).Display
        End If
       
        Set objEmail = objOutlook.CreateItem(olMailItem)
       
        strAddress = "xxxxxx@yyyyyyyyyy.net.au"     'Edited for privacy reasons
        strSubject = "Test Email Early Binding"
        strBody = "This is a test sending an email from Word using Early Binding"
        With objEmail
            .To = strAddress
            .Subject = strSubject
            .BodyFormat = olFormatHTML
            .HTMLBody = strBody
            .Send
        End With
       
        Set objEmail = Nothing
        Set objNameSpace = Nothing
        Set objOutlook = Nothing

    End Sub

     

    Sub SendEmailLateBinding()
        Dim objOutlook As Object    'Object in lieu of Outlook.Application
        Dim objNameSpace As Object  'Object in lieu of Outlook.NameSpace
        Dim objEmail As Object      'Object in lieu of Outlook.MailItem
        Const MailItem = 0          'Replaces constant olMailItem
        Const FolderInbox = 6       'Replaces constant olFolderInbox
        Const FormatHTML = 2        'Replaces constant olFormatHTML
        'If any other constants being used with Early Binding then they
        'will need to have constants created for them also. When the
        'Reference is unchecked select Debug -> Compile and that will
        'pick up any constants that need attention.
       
        Dim strAddress As String
        Dim strSubject As String
        Dim strBody As String
       
        'Test if Outlook already open by using GetObject
        Set objOutlook = Nothing    'In case already set with previous code
        On Error Resume Next
        Set objOutlook = GetObject(, "Outlook.Application")
        On Error GoTo 0 'Re-set error trapping ASAP
       
        If objOutlook Is Nothing Then   'GetObject did not work so Create
            Set objOutlook = CreateObject("Outlook.Application")
            Set objNameSpace = objOutlook.GetNamespace("MAPI")
            objNameSpace.GetDefaultFolder(FolderInbox).Display
        End If
       
        'Set objEmail = objOutlook.CreateItem(olMailItem)
        Set objEmail = objOutlook.CreateItem(MailItem)
       
        strAddress = "xxxxxx@yyyyyyyyyy.net.au"    'Edited for privacy reasons
        strSubject = "Test Email Late Binding"
        strBody = "This is a test sending an email from Word using Late Binding"
        With objEmail
            .To = strAddress
            .Subject = strSubject
            .BodyFormat = FormatHTML
            .HTMLBody = strBody
            .Send
        End With
       
        Set objEmail = Nothing
        Set objNameSpace = Nothing
        Set objOutlook = Nothing

    End Sub


    Regards, OssieMac

    Monday, June 18, 2012 7:19 AM
  • Thank you Ossie for this piece of code.

    Dave, have you been able to figure it out by above hint?

    thanks,


    Forrest Guo | MSDN Community Support | Feedback to manager

    Monday, July 2, 2012 3:07 AM
    Moderator
  • Thanks. I asked the user once but got no answer. I'll try again.
    Monday, July 2, 2012 1:59 PM
  • Hi Dave,

    Welcome to the MSDN forum!

    I temporarily marked the reply as answer and you can unmark it if it provides no help.

    Let us know if you need any help.

    Thanks for your understanding and have a nice day.


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, July 4, 2012 3:35 AM
    Moderator