Super Easy Word Merge - Open System File

craigachan

Registered User.
Local time
Today, 14:10
Joined
Nov 9, 2007
Messages
285
I've used Albert Kallal's Super Easy Word Merge with good success. I have however found that this code lease the Word System Files open. I check around this forum to see if there was a fix for this but have not been successful.

Can someone help me with closing this system file? I'll get something like: ~$somewordfile (no extension) that stays open and I have to go in manually to close them.

I"ve tried WordApp.Quit, and this does not do it.
 
What is the code you are using for it. Maybe there's a bit of disconnected code (much like with Excel) when you use a reference to an object that isn't connected to the application object then it opens another instance of the application and holds it open until Access closes.
 
Depending on how you declare it and such, this problem will occur (I've seen it more with Excel). To 'fix' this problem, I don't let Access call the application upon completion but 'output it' to a specified directory and let the user go and open it.

I am not sure how you are doing it, but will post what I use. It goes without a hitch and haven't had an issue in over 2 years with it. Basically, the code calls on a Word doc that it uses as a template and inserts data into bookmarks. I will post the highlights of the code .... Oh yeah, done in Access '07.

Code:
Dim objWord As Object
Dim objDoc As Object
 
Set objWord = New Word.Application
Set objDoc = objWord.Documents.Add("C:\Templates\" & sTemplateName)
 
[COLOR=seagreen]'load some recordset stuff and use it to update bookmarks in word[/COLOR]
[COLOR=seagreen]'the if-then is so I can call it once for multiple documents so[/COLOR]
[COLOR=seagreen]'it checks for the bookmark first[/COLOR]
 
If objDoc.Bookmarks.Exists("BookMark1") = True Then
      objDoc.Bookmarks("BookMark1").Select
      objWord.Selection.Text = rst!Field1
End If
 
[COLOR=seagreen]'save the updated doc[/COLOR]
objDoc.SaveAs filename:=sVariableNameforFileName
 
[COLOR=seagreen]'show the word document - this is where the persistent problem occurs. [/COLOR]
[COLOR=seagreen]'I kept it in the module in case this problem was ever fixed but you notice[/COLOR]
[COLOR=seagreen]'that I have remarked out[/COLOR].
'objWord.Visible = True
'objWord.ActiveWindow.WindowState = wdWindowStateMaximize
 
objDoc.Close
objWord.Quit
 
Set objDoc = Nothing
Set objWord = Nothing

HTH,
-dK

EDIT: That way if anyone knows why mine has errors, they can correct it, too. =]
 
Last edited:
This code opens the template doc, merges the info, saves the doc with a different name, closes the template doc, and then opens the SaveAs doc. But it seems to leave a system file open from the template doc. Hopefully we can solve the original problem, but I'll start looking for alternative ways around as suggested above.

Here is the code that I use:

Code:
Function RidesMergeWord(strDocName As String, _
strDataDir As String, _
Optional strOutDocName As String)
' This code takes a word document that has been setup as a MERGE document.
' This merge document is opened, then mailmerge is executed. The original
' document is then closed. The result is a raw word document with no connectons
' to the merge.txt (a csv source data file).
 
'Parms:
' strDocName - full path name of word doc (.doc)
' strDataDir - dir (full path) where docuemnts and the merge.888 file is placed
' strOutDocName - full path name of merged document (saved).
'
' The above parms are suppled by other routines. You likey should not need to call this
' routine directly. See the sub called MergeNoPrompts.
 
' Albert D. Kallal (c) 2001
' [EMAIL="kalla@msn.com"]kalla@msn.com[/EMAIL]
'
 
Dim WordApp As Object ' running instance of word
Dim WordDoc As Object ' one instance of a word doc
Dim strActiveDoc As String ' doc name (no path)
Dim lngWordDest As Long ' const for dest, 0 = new doc, 1 = printer
Dim MyPbar As New clsRidesPBar ' create a instance of our Progress bar.
 
 
MyPbar.ShowProgress
MyPbar.TextMsg = "Launching Word...please wait..."
MyPbar.Pmax = 4 ' 4 steps to inc
MyPbar.IncOne ' step 1....start!
 
On Error GoTo CreateWordApp
Set WordApp = GetObject(, "Word.Application")
On Error Resume Next
 
MyPbar.IncOne ' step 2, word is loaded.
 
Set WordDoc = WordApp.Documents.Open(strDocName)
 
MyPbar.IncOne ' step 3, doc is loaded
 
strActiveDoc = WordApp.ActiveDocument.Name
 
WordDoc.MailMerge.OpenDataSource _
Name:=strDataDir & TextMerge, _
ConfirmConversions:=False, _
ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False, Format:=0, _
Connection:="", SQLStatement:="", SQLStatement1:=""
 
 
 
 
With WordDoc.MailMerge
.Destination = 0 ' 0 = new doc
.MailAsAttachment = False
.MailAddressFieldName = ""
.MailSubject = ""
.SuppressBlankLines = True
With .datasource
.FirstRecord = 1
' .LastRecord = 1
End With
.Execute Pause:=True
End With
MyPbar.IncOne ' step 4, doc is merged
WordDoc.Close (False)
 
WordApp.Visible = True
WordApp.Windows(WordApp.Windows.Count).Activate
If strOutDocName <> "" Then
WordApp.ActiveDocument.SaveAs strOutDocName
End If
 
MyPbar.HideProgress
 
' AppActivate "Microsoft Word"
WordApp.Activate
WordApp.WindowState = 0 'wdWindowStateRestore
 
Set WordApp = Nothing
Set WordDoc = Nothing
Set MyPbar = Nothing
DoEvents
' If bolShowMerge = True Then
' WordApp.Dialogs(676).Show 'wdDialogMailMerge
' End If
 
Exit Function
 
CreateWordApp:
' this code is here to use the EXISTING copy of
' ms-access running. If getobject fails, then
' ms-word was NOT running. The below will then
' launch word
Set WordApp = CreateObject("Word.Application")
Resume Next
 
End Function
 
What I would do is to have your Task Manager open and set a break point in the code at the start and then F8 through and after each step look to see where an extra instance opens up, if it does. That might help narrow things down as I didn't see anything that jumped out at me.
 
I don't have this problem on my version of S.E.W.M
 
Okay I narrowed it down to this line:

Set WordDoc = WordApp.Documents.Open(strDocName)

But....If I run the code up to it and then step thru it, it doesn't open a system file. But If I run the code thru it to the next line, it opens the system file.

Any ideas anyone?
 
Personally I just create a new Word Instance and don't use the GetObject part. I have never had good luck with that part so I just create a new instance when using it and it seems to work fine for me.

So I would just change:

Set WordApp = GetObject(, "Word.Application")

to

Set WordApp = CreateObject("Word.Application")

and then remove the special error handling for the GetObject part.
 
No Change. I haven't seen it open a 2nd instance of word if that is what you mean. It just doesn't close the system file that is related to the template doc.
 

Users who are viewing this thread

Back
Top Bottom