S
Steve Harrison
Hi,
I have a Word 2002/2003 document with four merge fields: id, name, address,
and salutation. I have a SQL Server table, T_PRINT, with four columns: id,
name, address, salutation.
I populate the table with all the information I need, and if I open the
document manually, connect to the ODC file, and run the mail merge, it works
fine. I get a merged document based on the information in the table.
However, I cannot get this working automatically from code. Once I run the
code below, I get the file "out.doc" with just two lines:
Record 1 contains too few data fields
Record 2 contains too few data fields
This is the code I'm running from VB.NET:
-----------------------------
Dim gWordApp As Word.ApplicationClass
Dim gDocClass As Word.DocumentClass
gWordApp = New Word.ApplicationClass()
gWordApp.Visible = False
gDocClass = New Word.DocumentClass()
gDocClass = gWordApp.Documents.Add()
gDocClass.MailMerge.MainDocumentType =
Word.WdMailMergeMainDocType.wdFormLetters
conn = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Initial
Catalog=" & myDB & ";"
conn &= "Data Source=" & MyServer & ";Use Procedure for Prepare=1;Auto
Translate=True;"
conn &= "Packet Size=4096;Workstation ID=STEVEH;Use Encryption for
Data=False;"
conn &= "Tag with column collation when "
gDocClass.MailMerge.OpenDataSource(Name:= " & MyODCFileLocation & "_
, ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=Word.WdOpenFormat.wdOpenFormatAuto, Connection:= " & conn & "_
, SQLStatement:="SELECT * FROM ""T_PRINT""", SQLStatement1:="", SubType:= _
Word.WdMergeSubType.wdMergeSubTypeOther)
With gDocClass.MailMerge
.Destination = Word.WdMailMergeDestination.wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = Word.WdMailMergeDefaultRecord.wdDefaultFirstRecord
.LastRecord = Word.WdMailMergeDefaultRecord.wdDefaultLastRecord
End With
.Execute(Pause:=False)
End With
gWordApp.ActiveDocument.SaveAs("c:\out.doc")
I have a Word 2002/2003 document with four merge fields: id, name, address,
and salutation. I have a SQL Server table, T_PRINT, with four columns: id,
name, address, salutation.
I populate the table with all the information I need, and if I open the
document manually, connect to the ODC file, and run the mail merge, it works
fine. I get a merged document based on the information in the table.
However, I cannot get this working automatically from code. Once I run the
code below, I get the file "out.doc" with just two lines:
Record 1 contains too few data fields
Record 2 contains too few data fields
This is the code I'm running from VB.NET:
-----------------------------
Dim gWordApp As Word.ApplicationClass
Dim gDocClass As Word.DocumentClass
gWordApp = New Word.ApplicationClass()
gWordApp.Visible = False
gDocClass = New Word.DocumentClass()
gDocClass = gWordApp.Documents.Add()
gDocClass.MailMerge.MainDocumentType =
Word.WdMailMergeMainDocType.wdFormLetters
conn = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Initial
Catalog=" & myDB & ";"
conn &= "Data Source=" & MyServer & ";Use Procedure for Prepare=1;Auto
Translate=True;"
conn &= "Packet Size=4096;Workstation ID=STEVEH;Use Encryption for
Data=False;"
conn &= "Tag with column collation when "
gDocClass.MailMerge.OpenDataSource(Name:= " & MyODCFileLocation & "_
, ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=Word.WdOpenFormat.wdOpenFormatAuto, Connection:= " & conn & "_
, SQLStatement:="SELECT * FROM ""T_PRINT""", SQLStatement1:="", SubType:= _
Word.WdMergeSubType.wdMergeSubTypeOther)
With gDocClass.MailMerge
.Destination = Word.WdMailMergeDestination.wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = Word.WdMailMergeDefaultRecord.wdDefaultFirstRecord
.LastRecord = Word.WdMailMergeDefaultRecord.wdDefaultLastRecord
End With
.Execute(Pause:=False)
End With
gWordApp.ActiveDocument.SaveAs("c:\out.doc")