K
kiln
From Access, I need to open Word, merge a document, and leave the merged
doc open. All works fine (pseudo code) except that I am not able to
release the objWord variable at the end of the routine, thus the error
the next time I try to run the merge. Calling Quit closes Word, I need
it kept open.
Public Function Merge()
Dim objWord As Object ' or Word.Application
Dim docResult As Word.Document
Dim docMerge As Word.Document
Dim strPath As String
Dim strDocName As String
Dim strDotName As String
Dim strDBName As String
Dim strPathDocName As String
Dim strPathDotName As String
Dim strPathDBName As String
Dim strPhaseSet As String
Dim strMergeTable As String
Set objWord = New Word.Application
objWord.Documents.Open strPathDotName, False
Set docMerge = ActiveDocument
With docMerge.MailMerge
.OpenDataSource _
Name:=strPathDBName, _
LinkToSource:=True, _
Connection:="TABLE " & strMergeTable, _
SQLStatement:="SELECT * FROM [" & strMergeTable & "]"
.Destination = wdSendToNewDocument
.Execute
End With
' Close the template file
docMerge.Close
' Show word
objWord.Visible = True
Set docResult = ActiveDocument
' Save the merged document to the project file name.
Set docMerge = Nothing
Set docResult = Nothing
Set objWord = Nothing
End Function
doc open. All works fine (pseudo code) except that I am not able to
release the objWord variable at the end of the routine, thus the error
the next time I try to run the merge. Calling Quit closes Word, I need
it kept open.
Public Function Merge()
Dim objWord As Object ' or Word.Application
Dim docResult As Word.Document
Dim docMerge As Word.Document
Dim strPath As String
Dim strDocName As String
Dim strDotName As String
Dim strDBName As String
Dim strPathDocName As String
Dim strPathDotName As String
Dim strPathDBName As String
Dim strPhaseSet As String
Dim strMergeTable As String
Set objWord = New Word.Application
objWord.Documents.Open strPathDotName, False
Set docMerge = ActiveDocument
With docMerge.MailMerge
.OpenDataSource _
Name:=strPathDBName, _
LinkToSource:=True, _
Connection:="TABLE " & strMergeTable, _
SQLStatement:="SELECT * FROM [" & strMergeTable & "]"
.Destination = wdSendToNewDocument
.Execute
End With
' Close the template file
docMerge.Close
' Show word
objWord.Visible = True
Set docResult = ActiveDocument
' Save the merged document to the project file name.
Set docMerge = Nothing
Set docResult = Nothing
Set objWord = Nothing
End Function