Merging from form to Word Document (1 Viewer)

wrightyrx7

Registered User.
Local time
Today, 03:56
Joined
Sep 4, 2014
Messages
104
Hi all,

I am having a problem. I have some code that takes a copy of the Master word document from a shared drive, then puts it in the users Documents. Each time it does this it KILLS the old version so that the user is always using the most upto date Master version.

The reason i take a copy is because there are 15+ people using this tool and if any of them merge the same document a the same time it was causing problems.

I have some code that then opens this copied documents and replaces many bookmarks with data from my Access form.

The problem i am having is that on SOME occasions (not all) the word document is not closing down properly WINWORD.exe is still running in processes. Then when they run the code again it cannot KILL the old file because it is still running in the processes.

I cannot replicate this problem but have seen it happen on a few of the users computers.

So my question is...is the way i am doing it above the way you would do this?

Thanks in advance
Chris


My code has a lot more bookmarks to replace but below is pretty much the code i am using

Code:
Option Compare Database

Private Sub Merge_Click()

Dim strFileName As String
Dim strCopyFile As String
    
'==Master Document location
strCopyFile = "C:\SHARED\MYDOC.docx"
    

'==copy to this location
strFileName = SpecialFolderPath("MyDocuments") & "\MYDOC.docx"

'==delete old file
If Dir(strFileName) <> "" Then Kill (strFileName) 'this is where it errors if Word wasnt closed properly last time
    
'==copy the template
FileCopy strCopyFile, strFileName


'==save location
    With Application.FileDialog(msoFileDialogSaveAs)
        .Title = "Save Location for PDF"
        .InitialFileName = Me.FORENAME & " " & Me.SURNAME & "(" & Format(Date, "dd-MM-yy") & ")"
        If .Show = True Then
            If Right(.SelectedItems(1), 4) = ".pdf" Then
                wordDoc = .SelectedItems(1)
            Else
                wordDoc = .SelectedItems(1) & ".pdf"
            End If
        Else
            strFileName = ""
            Exit Sub
        End If
    End With
    
    
'==OPEN WORD AND MERGE
Dim MyWord As Word.Application
Dim MyDoc As Object

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

    
With MyWord
    .Visible = False
    Set MyDoc = .Documents.Open(strFileName)
            '-----------------------
            'MERGE DATA TO BOOKMARKS
            '-----------------------
                If .ActiveDocument.Bookmarks.Exists("JOBTITLE") Then .ActiveDocument.Bookmarks("JOBTITLE").Range.Text = Me.JOB_TITLE
                'THIS IS WHERE I MERGE ALL MY FIELDS

            '------------------------------
            'END OF MERGE DATA TO BOOKMARKS
            '------------------------------
            .ActiveDocument.SaveAs2 wordDoc, 17
End With

    DoEvents

    MyWord.Quit savechanges:=wdDoNotSaveChanges
    Set MyWord = Nothing
    Set MyDoc = Nothing
    strFileName = ""
    wordDoc = ""
MsgBox ("Complete")

End Sub
 
Last edited:

Ranman256

Well-known member
Local time
Today, 06:56
Joined
Apr 9, 2015
Messages
4,337
I don't see any error handling. If the user hits an error,the code will leave Word open. ( resume next may not work)
Is this happening?

As long as there are no errors, it should hit the myWord.Quit.
 

wrightyrx7

Registered User.
Local time
Today, 03:56
Joined
Sep 4, 2014
Messages
104
Thank you Ranman256, i will try adding an 'On error goto' that will Quit if there is any errors.
 

Ranman256

Well-known member
Local time
Today, 06:56
Joined
Apr 9, 2015
Messages
4,337
Be sure to MsgBox the error so you know what's wrong.
 

Users who are viewing this thread

Top Bottom