Results 1 to 11 of 11
  1. #1
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Portland, Maine, USA
    Posts
    296
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Add Reference (Word 2003)

    I have a quick little macro in Word that is calling Outlook to generate a message. It works fine as long as I have added the reference to Outlook under Tools, References in the VBA Editor. Is there a way to make this happen though code?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Add Reference (Word 2003)

    Although it is possible to set a reference in code, I have always found it to be tricky - you need to set another reference in order to be able to set references in code! <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    You only need to set the reference once, it is not necessary to set it each time you run the macro.

    You can avoid setting the reference by using late binding, i.e., declare variables as Object instead of as Outlook.Application etc. You must replace all symbolic constants such as olMailItem by their value; you can use the Object Browser to find the value of symbolic constants.

    Dim objApp As Object
    Dim objMail As Object
    Set objApp = CreateObject("Outlook.Application")
    Set objMail = objApp.CreatItem(0) ' olMailItem = 0

    It is easiest to keep the reference to the Outlook object library while developing, and to declare variables as Outlook.Application etc.; IntelliSense makes writing code much easier. When the code has been tested, replace all constants by their values, replace As Outlook.MailItem by As Object etc., and remove the reference.

  3. #3
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Portland, Maine, USA
    Posts
    296
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Add Reference (Word 2003)

    Thank you! This is very helpful and now I am 90% of the way there. However, in my code I need to set bodyformat = olFormatRichText. Is there a way I can do this using late binding?

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Add Reference (Word 2003)

    You can look up olFormatRichText in the Object Browser, or type <code>? olFormatRichText</code> in the Immediate window, then press Enter. You will learn that its value is 3. So you can use

    objMail.BodyFormat = 3

    (replace objMail by the name of the item you are using)

  5. #5
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Portland, Maine, USA
    Posts
    296
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Add Reference (Word 2003)

    Thank you. I searched in the Objectbrowser (In Outlook VBE) and came up as not found. I did not know about ? in the immediate window.

  6. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Add Reference (Word 2003)

    ? is a visual basic abbreviation for Debug.Print

    StuartR

  7. #7
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Add Reference (Word 2003)

    The correct way to do this is to use early binding and to build the template using the earliest version of Word and Outlook you intend to support..
    If you do not have access to the earliest version that will be supported then, ugh!!, you can use late binding.

  8. #8
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Portland, Maine, USA
    Posts
    296
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Add Reference (Word 2003)

    The problem is not the verions of Word and Outlook. It is how to get this installed to many different users. I can distribute something via a Word doc that will copy the module and create the button. Late binding came in as a way to work around the problem of setting the reference. I am open to other ideas.

  9. #9
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Add Reference (Word 2003)

    You can set the reference in the module that you create, before you ship the code.

    So long as other people are running the same, or later, versions everything will work fine. The only time you would need to set the reference again on the target computer is if they are running an earlier version of office.

    StuartR

  10. #10
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Portland, Maine, USA
    Posts
    296
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Add Reference (Word 2003)

    Perhaps the problem then is in my process.

    I have a module in a Word document which I email to the users, the reference is set, and the person with whom I am testing is using the same versions of Word and Outlook. The document has a description of the button and instructions on using it in the document with OK and Cancel buttons. When they click OK, the module is copied to their normal.dot via Application.OrganizerCopy and I copy a button to their standard toolbar.

  11. #11
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Add Reference (Word 2003)

    That is very different to what I was describing. If you send them a document that they save to their Startup or Templates folder then it will carry any references with its modules. If you are transferring a module in code using the organiser then I have no idea what happens to references.

    StuartR

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •