Teh code below is used for doing a mail merge. It works well the 1st time but spits out an error that the database cannot be found when invoked the 2nd time.
Why would this be happening ?
Thank you in advance for the help.
Why would this be happening ?
Public Function LTRCOL_letter()
Dim oMainDoc As Word.Document
Dim oSel As Word.Selection
Dim sDBPath As String
Set oApp = CreateObject("Word.Application")
If oMainDoc Is Nothing Then
Set oMainDoc = oApp.Documents.Open("C:\Documents and Settings\Chris\Desktop\Letter_Access\Templates\LTRCOL.doc")
End If
oApp.Visible = False 'set connection as visible
With oMainDoc.MailMerge
.MainDocumentType = wdFormLetters
sDBPath = AssignDbPath
.OpenDataSource Name:=sDBPath, _
ConfirmConversions:=False, _
ReadOnly:=False, _
LinkToSource:=True, _
AddToRecentFiles:=False, _
Revert:=False, _
Format:=wdOpenFormatRTF, _
Connection:= _
"Provider=Microsoft.ACE.OLEDB.12.0; " & _
"User ID=Admin;" & _
"Password='';" & _
"Data Source=" & sDBPath & ";" & _
"Mode=Read;", _
SQLStatement:="SELECT * FROM `" & "LTRCOL" & "`", SQLStatement1:="", _
Subtype:=wdMergeSubTypeAccess
End With
If oMainDoc.MailMerge.DataSource.RecordCount > 0 Then
Call cmdGo_Click(oMainDoc.MailMerge.DataSource.RecordCount)
With oMainDoc
.MailMerge.Destination = wdSendToNewDocument
.ActiveWindow.ActivePane.View.Type = wdPrintView
.MailMerge.Execute
.Close (0)
End With
FileName = "LTRCOL" & "_" & Format(Date, "ddmmyyyy") & "_" & Format(Time, "mmhhss")
oApp.Application.Documents(1).SaveAs "C:\Documents and Settings\Chris\Desktop\Letter_Access\Output" & "\" & FileName & ".rtf"
oApp.Documents.Parent.Visible = False
oApp.Application.WindowState = wdWindowStateMinimize
oApp.ActiveWindow.WindowState = wdWindowStateMinimize
oApp.Quit (0)
Set oMainDoc = Nothing
Set oApp = Nothing
Set Rs2 = CurrentDb.OpenRecordset("AuditTrail")
Rs2.AddNew
Rs2("Filename") = TextF
Rs2("Processedby") = Environ("username")
Rs2("DateProcessed") = Format(Date, "dd/mm/yyyy")
Rs2("CountofRecords") = DCount("[Letter_code]", "LTRCOL")
Rs2("Letter_Code") = "LTRCOL"
Rs2("SavedAs") = FileName
Rs2.Update
Rs2.Close
Set Rs2 = Nothing
Else
oApp.Quit (0)
Set oMainDoc = Nothing
Set oApp = Nothing
Set Rs2 = Nothing
End If
End Function
Thank you in advance for the help.