aziz rasul
Active member
- Local time
- Today, 12:07
- Joined
- Jun 26, 2000
- Messages
- 1,935
I have inherited the following code: -
which fails on the line in red. The debug error 91 is 'Object variable or With block variable not set '. Any ideas why this error is happenning?
Code:
Public Sub MergeLetter(TemplateTitle)
Dim DBName As String, TemplateLocation As String
Dim DatabaseName As String, DatabaseLocation As String
Dim TemplateName As String, UserName As String, QrySource As String
Dim Response As String, SQL1 As String, TableTitle1 As String
Dim WordObj As New Word.Application
Dim NewDoc As Word.Document
On Error GoTo ErrorHandler
DoCmd.GoToRecord , , acNext
DoCmd.GoToRecord , , acPrevious
DatabaseName = "EP_Scheduling_Aug 2012.accdb"
DatabaseLocation = "\\fas3040-sata\wccmydocs$\hrlr155\My Documents\Databases\"
DBName = DatabaseLocation & DatabaseName
TemplateLocation = "L:\PerfAndDev\HR\HRJobEval\Templates\"
TemplateName = TemplateLocation & TemplateTitle
Debug.Print DBName
Debug.Print TemplateName
QrySource = "8_AutomateMailMerge"
DoCmd.SetWarnings False
SQL1 = "SELECT 8_AutomateMailMerge.* INTO " & "tbl_TempTable IN '" & DBName & "' FROM 8_AutomateMailMerge"
DoCmd.RunSQL (SQL1)
DoCmd.SetWarnings True
Call DeleteField("tbl_TempTable", "SigningDate")
Call DeleteField("tbl_TempTable", "SigningTime")
Set WordObj = CreateObject("Word.Application")
Set NewDoc = WordObj.Documents.Add(Template:=TemplateName, NewTemplate:=False)
With WordObj
.Visible = True
[COLOR="Red"]With NewDoc.MailMerge[/COLOR]
.MainDocumentType = wdFormLetters
TableTitle1 = "`" & "tbl_TempTable" & "`"
.OpenDataSource Name:=DBName, Connection:="DSN=MS ACCESS DATABASES;" & DBName & ";FIL=REDISAM;", SQLStatement:="Select * from " & TableTitle1, SQLStatement1:="", SubType:=wdMergeSubTypeAccess
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=True
End With
.Windows(1).Activate
.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
.Application.WindowState = wdWindowStateMaximize
End With
ErrorHandler:
If Err.Number > 0 Then
MsgBox Err.Number & " - " & Err.Description
End If
End Sub
which fails on the line in red. The debug error 91 is 'Object variable or With block variable not set '. Any ideas why this error is happenning?