Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mail merge - Object reference not set to an instance of an object.

Status
Not open for further replies.

honeypot

Technical User
Mar 6, 2001
147
GB
An unhandled exception of type 'System.NullReferenceException' occurred in PostBook Planning Dept.exe

Additional information: Object reference not set to an instance of an object.

The above error occurs when trying to run mail merge in my program. My code is as follows:


Private Sub InsertLines(ByVal LineNum As Integer)
Dim iCount As Integer
Dim wrdApp As Word.Application
Dim wrdDoc As Word._Document
' Insert "LineNum" blank lines.
For iCount = 1 To LineNum
wrdApp.Selection.TypeParagraph()
Next iCount
End Sub

Private Sub FillRow(ByVal Doc As Word.Document, ByVal Row As Integer, _
ByVal Text1 As String, ByVal Text2 As String, _
ByVal Text3 As String, ByVal Text4 As String)

With Doc.Tables.Item(1)
' Insert the data in the specific cell.
.Cell(Row, 1).Range.InsertAfter(Text1)
.Cell(Row, 2).Range.InsertAfter(Text2)
.Cell(Row, 3).Range.InsertAfter(Text3)
.Cell(Row, 4).Range.InsertAfter(Text4)
End With
End Sub

Private Sub CreateMailMergeDataFile()
Dim wrdDataDoc As Word._Document
Dim iCount As Integer
Dim wrdApp As Word.Application
Dim wrdDoc As Word._Document
' Create a data source at C:\DataDoc.doc containing the field data.
wrdDoc.MailMerge.CreateDataSource(Name:="C:\DataDoc.doc", _
HeaderRecord:="Name, Address_1, Address_2, Address_3")
' Open the file to insert data.
wrdDataDoc = wrdApp.Documents.Open("C:\DataDoc.doc")
For iCount = 1 To 2
wrdDataDoc.Tables.Item(1).Rows.Add()
Next iCount
' Fill in the data.
FillRow(wrdDataDoc, 2, "", "", _
"", "")
FillRow(wrdDataDoc, 3, "", "", _
"", "")
FillRow(wrdDataDoc, 4, "", "", _
"", "")
' Save and close the file.
wrdDataDoc.Save()
wrdDataDoc.Close(False)
End Sub

Any ideas on why this error occurs???
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top