If Word already open, creating new doc via code fouls things up

K

kiln

I use VBA to generate Word 2000 docs, calling from Access 2000. The doc
that is build is for use later, so I don't need Word to open and show
the doc at creation time. Everything in the code below works fine,
except that if an instance of Word is already open when the code is run,
the open Word instance sort of goes dead. What I mean is, the toolbars
are dead; they don't respond any more. The doc that was open is
editable. Is there something in the code below that fouls up the running
instance of word?

Thanks

Private Function CreateDoc()

Dim objWord As Object
Dim boolInitobjWord As Boolean
Dim strPath As String

If fIsAppRunning("Word") Then
Set objWord = GetObject(, "Word.Application")
boolInitobjWord = False
Else
Set objWord = CreateObject("Word.Application")
boolInitobjWord = True
End If

strPath = "c:\whatever\anewdoc.doc"
objWord.Documents.Add "c:\whatever\mytemplate.dot"
With objWord.ActiveDocument.Bookmarks
.Item("SomeName").Range.Text = FullName
End With
objWord.ActiveDocument.SaveAs strPath
objWord.ActiveDocument.Close

ExitHere:
If boolInitobjWord = True Then
objWord.Quit
End If
Set objWord = Nothing
Exit Function

End Function
 
P

Peter

kiln said:
I use VBA to generate Word 2000 docs, calling from Access 2000. The doc
that is build is for use later, so I don't need Word to open and show
the doc at creation time. Everything in the code below works fine,
except that if an instance of Word is already open when the code is run,
the open Word instance sort of goes dead. What I mean is, the toolbars
are dead; they don't respond any more. The doc that was open is
editable. Is there something in the code below that fouls up the running
instance of word?

Thanks

Private Function CreateDoc()

Dim objWord As Object
Dim boolInitobjWord As Boolean
Dim strPath As String

If fIsAppRunning("Word") Then
Set objWord = GetObject(, "Word.Application")
boolInitobjWord = False
Else
Set objWord = CreateObject("Word.Application")
boolInitobjWord = True
End If

strPath = "c:\whatever\anewdoc.doc"
objWord.Documents.Add "c:\whatever\mytemplate.dot"
With objWord.ActiveDocument.Bookmarks
.Item("SomeName").Range.Text = FullName
End With
objWord.ActiveDocument.SaveAs strPath
objWord.ActiveDocument.Close

ExitHere:
If boolInitobjWord = True Then
objWord.Quit
End If
Set objWord = Nothing
Exit Function

End Function

Don't reuse the open Word Application, just always create a new Application object.
According to http://support.microsoft.com/default.aspx?kbid=184974, GetObject could be unpredictable.

My $0.02 (US),

-Peter
 

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