Need help w/ XL macro to open WD doc

E

Ed

I have worked with this for many months, and just when I think it's good it
gives me more problems! The macro is in an Excel workbook. It takes the
file name from the ActiveCell and the file path from the workbook's path,
and opens a Word document in the same folder as the workbook. All docs
listed in the workbook do exist in that folder.

Most of the time it works fine. All too often, though, it opens an instance
of Word but not the doc. Yesterday was especially bad - after running
around this circle several times, I closed all programs and went to the
folder to open the doc directly. There was a "ghost" of the doc (ghosted
icon with "~name.doc"). I checked Task Manager and yep, there was a Word
instance still running. I terminated that and killed the ghost file. But
now I'm leery of running this macro.

As additional info, I use Outlook with Word as email editor, and
occasionally open a Word toolbar to access a Word macro while composing an
email. That was my situation yesterday when the first crash happened, but
it kept happening after several Restarts and Shutdowns. If anyone has any
ideas, I'd be more than happy to hear them.

Ed

Sub FindDoc()

'Dim WD As New Word.Application
Dim WD As Object
Dim doc As String
Dim Fname As String
Dim Fpath As String

Err.Clear

' Get file path
Fpath = ThisWorkbook.Path

Sheets("Sheet1").Activate

' Get doc name from list page
Fname = ActiveCell.Text

' Open doc
doc = Fpath & "\" & Fname & ".doc"

On Error Resume Next
Set WD = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set WD = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0

WD.Documents.Open doc
WD.Visible = True

Set WD = Nothing

End Sub
 
W

Word Heretic

G'day "Ed" <ed_millis@NO_SPAM.yahoo.com>,

You must be destroying the Word object without closing the Word
application. Thus, at the end of your excel macro, include a line like

Wordapp.Close

Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


Ed reckoned:
 
E

Ed

Thank you, Steve. I think I tried WD.Quit, but with larger results than I
intended, if I remember correctly. (I've slept since then - sorry!) Since
Word opens a new instance for each document, when I use
and end with WD.Close or WD.Quit, will it affect _only_ the object I just
created, or *all* instances of the application? In other words, if I have
other docs open, will they also close or quit?
Ed
 
W

Word Heretic

G'day "Ed" <ed_millis@NO_SPAM.yahoo.com>,

Ideally you kill every active document in that session and close the
session. If you are acquiring an existing session you should probably
note the number of open documents, make sure the total matches that at
end, then leave trhe app running.

Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


Ed reckoned:
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Top