Start MS Word with Document Opened

Discussion in 'AutoCAD' started by Nigel Downing, May 2, 2004.

  1. The following will open microsoft word 2003 with an existing document from
    within ADT 2005.

    ' run a word document from within adt 2005
    Public Sub OpenWord_ADT_2005()
    Set wordapp = CreateObject("WORD.application")
    wordapp.Visible = True
    wordapp.Documents.Open ("C:\My Documents\ADT 2005.doc")
    End Sub
    'end

    If word was already open when I ran the code I get 2 copies of word open,
    when I realy only want one. How can I prevent the second copy of word being
    opened, but still get the document opened?

    Also does "wordapp" or other stuff need to be reset to nil at the end?

    Thanks

    Nigel Downing (beginner with vba)
     
    Nigel Downing, May 2, 2004
    #1
  2. Nigel Downing

    Mark Propst Guest

    does GetObject work?
    Dim worddoc as Object
    set worddoc = GetObject ("C:\My Documents\ADT 2005.doc")

    I believe that is recommended...

    set worddoc = Nothing
    or
    set wordapp = Nothing
    End sub
     
    Mark Propst, May 2, 2004
    #2
  3. Hi Mark
    1. Didnt have any joy with your suggestion re GetObject, but wasnt really
    sure what you meant.
    2. Set wordapp = Nothing ' Release the object.
    on line just before the End Sub works.

    Thanks
    Nigel
     
    Nigel Downing, May 2, 2004
    #3
  4. Nigel Downing

    Mark Propst Guest

    Dim oDoc as Object
    set oDoc = GetObject (sFileName)

    will activate a running instance of acad that has sFileName open, if any
    instances do have it open.
    else it will find the file on disk, and open it in a running instance of
    acad if one exists, else it will start an instance of acad, then open
    sFileName.
    I didn't know if the same thing would work with Word. apparently not.
    Sorry, just a guess.
    Mark
     
    Mark Propst, May 2, 2004
    #4
  5. This worked for me in office 2000

    If you are happy to for the document to close when you are done use
    '
    Dim doc As Word.Document
    Set doc = GetObject("C:\Documents\My.doc")
    doc.Application.Visible = True
    set doc = nothing
    '
    Or use
    On Error Resume Next
    Dim MSWord As Word.Application
    Set MSWord = GetObject(, "WORD.application")
    'if err then no word running
    If Err.Number <> 0 Then
    Err.Clear
    Set MSWord = CreateObject("WORD.application")
    End If
    MSWord.Visible = True
    MSWord.Documents.Open ("C:\Documents\My.doc")
     
    Mark Gardiner, May 3, 2004
    #5
  6. No, you were on the right path Mark.

    Nigel -

    Instead of opening the doc, first try connecting to Word:
    On Error Resume Next
    Dim oWord As Object
    Set oWord = GetObject(,"Microsoft Word")
    If Err.Number <> 0 Then
    'Word is not running

    Else
    'Word is running
    'so iterate the open docs looking for the name of the file you
    'want to open. If not found, open it; if found, activate it
    End If
     
    Mike Tuersley, May 3, 2004
    #6
  7. '---This will load ms word 2003 with the intended ADT 2005.doc document
    open.
    Public Sub ADT2005()
    On Error Resume Next
    Dim MSWord As Word.Application
    Set MSWord = GetObject(, "WORD.application")
    'if err then no word running
    If Err.Number <> 0 Then
    Err.Clear
    Set MSWord = CreateObject("WORD.application")
    End If
    MSWord.Visible = True
    MSWord.Documents.Open ("C:\Documents and Settings\ADT 2005.doc")
    Set MSWord = Nothing ' Release the object.
    End Sub
    '---end

    To summarise:
    Word not loaded. The above code from within adt2005 will open word with the
    desired document open. Good!
    If word is already open but without any documents open, the above code will
    open word with the desired document open. Great!
    If word is already loaded with any OTHER document open, the above code will
    still work but I get a second word button on the Windows XP taskbar (one for
    each document).

    I think I have been assuming that two instances of word were fully open
    (hence double the overhead) when in fact it may just be a new microsoft
    feature to display things in this way.
    I guess they have to fiddle with something to justify the upgrade fees.

    Thanks all. The code now does its job and has the added benefit of handling
    long pathnames properly, something that wouldnt work with (startapp ...) in
    autolisp.
     
    Nigel Downing, May 4, 2004
    #7
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.