Results 1 to 9 of 9
  1. #1
    New Lounger
    Join Date
    Feb 2003
    Location
    Foothill Ranch, California, USA
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Open a exisitng Word doc (Microsoft office/excel)

    I'm attempting to have an existing word doc open from when a commandbutton is clicked from an excel user form:
    This is the direction I'm heading in and its not working...any ides

    Application.Open "Cocuments and SettingsBBondDesktopCaseName.doc"

  2. #2
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Reading/Swindon, Berkshire, United Kingdom
    Posts
    664
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Open a exisitng Word doc (Microsoft office/excel)

    Try the following (you'll need to set a reference to word in tools/references if you haven't already done so). the 9 in Word.Application.9 is for excel 2000 - change both occurences to 8 for 97 and 10 for XP.

    Private Sub CommandButton1_Click()
    Call OpenWordDoc
    End Sub


    Sub OpenWordDoc

    Dim WordObj As Word.Application

    On Error Resume Next
    Err.Number = 0
    Set WordObj = GetObject(, "Word.Application.9")
    If Err.Number = 429 Then
    Set WordObj = CreateObject("Word.Application.9")
    Err.Number = 0
    End If
    On Error GoTo 0

    WordObj.Visible = True
    WordObj.Documents.Open FileName:="Cocuments and SettingsBBondDesktopCaseName.doc"

    End Sub

  3. #3
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Open a exisitng Word doc (Microsoft office/excel)

    Brooke's method will give you a starting point to continue if you wish to then carry out some automation tasks in the freshly opened word file.
    A simpler suggestion would be to use FollowHyperlink if you don't want the code to do anything to the file other than open it.

    ActiveWorkbook.FollowHyperlink Address:="Cocuments and SettingsBBondDesktopCaseName.doc"
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Open a exisitng Word doc (Microsoft office/excel)

    You can make Brooke's solution more version agnostic. Also, by changing to "late binding," you bypass the requirement to set a reference in your template.

    Sub OpenWordDoc
    Dim WordObj As Object ' Word.Application
    On Error Resume Next
    Err.Clear ' Err.Number = 0
    Set WordObj = GetObject(, "Word.Application") ' GetObject(, "Word.Application.9")
    If Err.Number = 429 Then
    Set WordObj = CreateObject("Word.Application") ' CreateObject("Word.Application.9")
    Err.Clear ' Err.Number = 0
    End If
    On Error GoTo 0
    WordObj.Visible = True
    WordObj.Documents.Open FileName:="Cocuments and SettingsBBondDesktopCaseName.doc"
    End Sub

    (I changed Err.Number = 0 to Err.Clear because I'm not sure that the original syntax fully clears the error condition.)

  5. #5
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Reading/Swindon, Berkshire, United Kingdom
    Posts
    664
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Open a exisitng Word doc (Microsoft office/excel)

    >>>(I changed Err.Number = 0 to Err.Clear because I'm not sure that the original syntax fully clears the error condition.)

    you know, you might just have solved a completely unrelated but ongoing problem I have. <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>

  6. #6
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Open a exisitng Word doc (Microsoft office/excel)

    I don't think you actually need to clear the error. I have used the following code:

    On Error Resume Next
    Set WordObj = GetObject(, "Word.Application")

    If Err.Number = 429 Then
    Set WordObj = CreateObject("Word.Application")
    End If

    On Error Goto ErrorHandler

    Literally for years without any problems.
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  7. #7
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Open a exisitng Word doc (Microsoft office/excel)

    The "On Error Goto" or "Resume" statements clear the error automatically, so you don't need to use Err.Clear to clear it.
    Charlotte

  8. #8
    Star Lounger
    Join Date
    Mar 2006
    Posts
    63
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Re: Open a exisitng Word doc (Microsoft office/excel)

    I have a similar objective. From a macro, I want to do the equivalent of double-clicking a particular Word template (so that an unnamed and unsaved new document opens), and I'd dearly like to avoid some of the more convoluted calculations needed when using GetObject and similar.

    ActiveWorkbook.FollowHyperlink Address:=<templateName> doesn't work. It opens the template, not a document based on the template.

    Anyone know an easy way of doing it?

    Thanks

  9. #9
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Open a exisitng Word doc (Microsoft office/excel)

    <P ID="edit" class=small>(Edited by Andrew Lockton on 11-May-06 14:34. Realised this needed to be for excel)</P>Try
    Documents.Add Template:="C:WorkTemplatesTemplate.dot"

    Whoops, try a different one for Excel
    Workbooks.Add Template:="C:WorkTemplatesTemplate.xlt"
    Andrew Lockton, Chrysalis Design, Melbourne Australia

Posting Permissions

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