• Open a exisitng Word doc (Microsoft office/excel)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Open a exisitng Word doc (Microsoft office/excel)

    Author
    Topic
    #384716

    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 “C:Documents and SettingsBBondDesktopCaseName.doc”

    Viewing 2 reply threads
    Author
    Replies
    • #660998

      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:=”C:Documents and SettingsBBondDesktopCaseName.doc”

      End Sub

      • #661075

        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:=”C:Documents 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.)

        • #661126

          >>>(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. cheers

        • #661180

          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.

          • #661313

            The “On Error Goto” or “Resume” statements clear the error automatically, so you don’t need to use Err.Clear to clear it.

    • #661039

      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:=”C:Documents and SettingsBBondDesktopCaseName.doc”

    • #1011671

      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:= doesn’t work. It opens the template, not a document based on the template.

      Anyone know an easy way of doing it?

      Thanks

      • #1011677

        (Edited by Andrew Lockton on 11-May-06 14:34. Realised this needed to be for excel)

        Try
        Documents.Add Template:=”C:WorkTemplatesTemplate.dot”

        Whoops, try a different one for Excel
        Workbooks.Add Template:=”C:WorkTemplatesTemplate.xlt”

    Viewing 2 reply threads
    Reply To: Open a exisitng Word doc (Microsoft office/excel)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: