Results 1 to 3 of 3
  1. #1
    Lounger
    Join Date
    Apr 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Accessing Word options from Excel (2000)

    I need to use Excel to change default file paths in Word. I am using create object to open an instance of Word and send the command through. But, no matter what I try it doesn't work. Here is my code:

    Sub test()
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    temp = objWord.Options.DefaultFilePath(wdUserTemplatesPat h)
    MsgBox "File path is: " & temp
    objWord.Quit
    Set objWord = Nothing
    End Sub


    The MsgBox always says "H:" even though the wdUserTemplatesPath is actually something else (eg.crogram filesOfficetemplates When I test for wdUserTemplatesPath from within Word I get the correct result."). Does anyone know of a restriction on getting or setting word options using CreateObject ?

    Many thanks.

    GB
    <img src=/S/flags/Australia.gif border=0 alt=Australia width=30 height=18>

  2. #2
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Accessing Word options from Excel (2000)

    If you add

    Option explicit

    at the top of your module
    It forces you to Dim all variables.

    You can tell Excel to do this automatically for each new module you add by setting "Require variable declaration" in Tools, options (from the VBE)
    This way, typo's are revealed as compile errors.
    After dimming both objWord and Temp, you get a compile error on wdUserTemplatesPath.
    Excel doesn't recognize Word constants automatically.
    You have two options:

    1.
    - browse to Word and find the value behind the keyword (use the object browser, F2 from the VBE), DIm a constant and set it to that value or use the value directly as the argument to the DefaultFilePath method

    2.
    - set a reference to the Word object lib (Tools, references) and change your code to this:

    Sub test()
    Dim objWord As Word.Application
    Dim temp As String
    Set objWord = New Word.Application
    temp = objWord.Options.DefaultFilePath(wdUserTemplatesPat h)
    MsgBox "File path is: " & temp
    objWord.Quit
    Set objWord = Nothing
    End Sub
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  3. #3
    Lounger
    Join Date
    Apr 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Accessing Word options from Excel (2000)

    Jan Karel, many thanks.

    Your second suggestion worked a treat. The final code is in the attachment.

    Cheers

    Garry Brooke
    Attached Files Attached Files
    <img src=/S/flags/Australia.gif border=0 alt=Australia width=30 height=18>

Posting Permissions

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