Keeping Word open to make changes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am sending data from an access database using a function called by a macro
to send data from a form to a word document using bookmarks
The process works ok, but problem is after inserting data into document it
closes and returns to access and continues macro but i need to keep document
open to allow users to amend document then print it out and then return to
access and continue macro

Any suggestions?
 
Hi Gerry,

This is demonstration code, not production quality, but it shows one
approach:

Dim oWord As Word.Application
Dim oDoc As Word.Document

Public Sub TestWord()

Set oWord = New Word.Application
Set oDoc = oWord.Documents.Add
oWord.Visible = True
oDoc.Windows(1).Visible = True
oDoc.UserControl = True
oDoc.Range.InsertAfter "This is the text of the document"

MsgBox "Word is open and ready"

Do
DoEvents
Loop While WordStillThere(oWord)

MsgBox "Word has closed"

End Sub

Public Function WordStillThere(oWord As Variant) As Boolean
Dim Dummy As Variant

On Error Resume Next
Dummy = oWord.Application.Name
If Err.Number = 0 Then
WordStillThere = True
Else
WordStillThere = False
End If
End Function


In real life it would almost certainly be preferable to split the
TestWord() code into two parts. I'm not a macro expert but I think
you'll also need to convert the macro to VBA code.

The first part of the code would probably go in the Click event
procedure of a button on your form. It would contain everything up to
the first MsgBox() call, including of course the code to construct your
document. It would end by setting the form's TimerInterval to a suitable
value (maybe 500 milliseconds).

This would set the form's Timer going, and every 500 milliseconds the
OnTime event would run: use this to check whether Word is still there,
e.g.

If WordStillThere(oWord) Then
Exit Sub
Else
'do the other things you need to do
...
End if
 
Back
Top