Do NOT open second instance of Word (1 Viewer)

Local time
Today, 02:14
Joined
Aug 3, 2005
Messages
66
Good day.

I have the following function that does this:

- Opens a MS Word Template with form fields.
- Fills the Word form fields with the data from the access form.

This works as expected:

Code:
Function FillReport()

Dim appword As Word.Application
Dim doc As Word.Document
Dim path As String

On Error Resume Next
Error.Clear

path = "C:\kdb_reports\report.dotx"
Set appword = GetObject(, "Word.Application")

If Err.Number <> 0 Then
Set appword = New Word.Application
appword.Visible = True

End If

Set doc = appword.Documents.Open(path, , False)

With doc

    .formfields("txt_pt_name1").Result = Me.txt_pt_name1
    .formfields("txt_pt_name2").Result = Me.txt_pt_name2
    .formfields("txt_dob").Result = Me.txt_dob
    .formfields("txt_age").Result = Me.txt_age
    .formfields("txt_tel2").Result = Me.txt_tel2
    .formfields("txt_tel1").Result = Me.txt_tel1
    .formfields("txt_func").Result = Me.txt_func
    .formfields("txt_reportdate").Result = Me.txt_reportdate

End With

appword.Visible = True
appword.Activate
appword.ActiveWindow.View.ReadingLayout = False

Set doc = Nothing
Set appword = Nothing

End Function

At this stage the Word doc is open and the form fields are filled in.

It is important that I can at this stage finalise (save and close) the word doc, or just keep it open if I wanted to manually add more info later, but I also want to do the following:

Back in my the Access front-end, on another form, I copied the above function, but with different...

Code:
.formfields("txt_myfield").Result = Me.txt_myfield

However, this second set of fields are OPTIONAL and might or might not be added to the already opened word doc.

So my question is, how to change or add to the above code in order to:

-Check if the Word doc is ALREADY open, then use the 'already-opened' file and add the second set of fields.

At the moment, when I 'submit' the second set of fields, it opens a second instance of the same word template.

Please point me in the right direction to solve this.

Thank you.
Jamie.
 

JHB

Have been here a while
Local time
Today, 01:14
Joined
Jun 17, 2012
Messages
7,732
Put the below code in the other form.
Code:
Sub AnOther    
    Dim wordDoc As Word.Document
    Dim wordAppl As Word.Application
    Dim mydoc As String
    Dim myAppl As String

    On Error GoTo ErrorHandler
    
    mydoc = "C:\kdb_reports\report.dotx"
    myAppl = "Word.Application"

    If Not IsRunning(myAppl) Then
        Set wordAppl = CreateObject("Word.Application")
        Set wordDoc = wordAppl.Documents.Open(mydoc)
        wordAppl.Visible = True
    Else
       'bind the wordDoc variable to a specific Word document
        Set wordDoc = GetObject(mydoc)
    End If
End Sub

Function IsRunning(ByVal myAppl As String) As Boolean
    Dim applRef As Object
    On Error Resume Next

    Set applRef = GetObject(, myAppl)
    If Err.Number = 429 Then
        IsRunning = False
    Else
        IsRunning = True
    End If
    'clear object variable
    Set applRef = Nothing
End Function
 
Local time
Today, 02:14
Joined
Aug 3, 2005
Messages
66
SOLVED by @JHB

Your solution worked perfectly.

Thank you very much.
 

JHB

Have been here a while
Local time
Today, 01:14
Joined
Jun 17, 2012
Messages
7,732
You're welcome, good luck. :)
 

Users who are viewing this thread

Top Bottom