Creating a mail merge document using C#.

G

Guest

Hey, all. I'm trying to develop a C# app that creates Word 2003 mail merge
documents with an Oracle 9i database as the datasource. I used the following
as an example of how I can start out:

http://support.microsoft.com/default.aspx?scid=kb;EN-US;301659

The problem is that the code provided doesn't allow me to add more users to
the mail merge data file. When I try to add an additional user with the
following code, I get this error when I try to build:

"No overload for method 'FillRow' takes '6' arguments"

Here's the code I want to work. Please help.

<code>
private void FillRow(Word._Document oDoc, int Row, string Text1,
string Text2, string Text3, string Text4, string Text5)
{
// Insert the data into the specific cell.
oDoc.Tables[1].Cell(Row,1).Range.InsertAfter(Text1);
oDoc.Tables[1].Cell(Row,2).Range.InsertAfter(Text2);
oDoc.Tables[1].Cell(Row,3).Range.InsertAfter(Text3);
oDoc.Tables[1].Cell(Row,4).Range.InsertAfter(Text4);
oDoc.Tables[1].Cell(Row,5).Range.InsertAfter(Text5);
}

private void CreateMailMergeDataFile()
{
Word._Document oDataDoc;
int iCount;

Object oName = "C:\\DataDoc.doc";
Object oHeader = "FirstName, LastName, Address, CityStateZip";
wrdDoc.MailMerge.CreateDataSource(ref oName,ref oMissing,
ref oMissing,ref oHeader, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing);

// Open the file to insert data.
oDataDoc = wrdApp.Documents.Open(ref oName,ref oMissing,
ref oMissing, ref oMissing,ref oMissing,ref oMissing,
ref oMissing,ref oMissing,ref oMissing,ref oMissing,
ref oMissing,ref oMissing,ref oMissing,ref oMissing,
ref oMissing, ref oMissing);

for (iCount=1; iCount<=2; iCount++)
{
oDataDoc.Tables[1].Rows.Add(ref oMissing);
}
// Fill in the data.
FillRow(oDataDoc, 2, "Steve", "DeBroux",
"4567 Main Street", "Buffalo, NY 98052");
FillRow(oDataDoc, 3, "Jan", "Miksovsky",
"1234 5th Street", "Charlotte, NC 98765");
FillRow(oDataDoc, 4, "Brian", "Valentine",
"12348 78th Street Apt. 214",
"Lubbock, TX 25874");
FillRow(oDataDoc, 5, "FifthFirst", "FifthLast",
"6666 Phoney Street",
"Cowtown, CA 90218");
// Save and close the file.
oDataDoc.Save();
oDataDoc.Close(ref oFalse, ref oMissing, ref oMissing);
}
</code>
 
B

Ben Lucas

It's telling you that there the FillRow method that you are calling does not
match the signature.

Your signature for the method is:
private void FillRow(Word._Document oDoc, int Row, string Text1,
string Text2, string Text3, string Text4, string Text5)

Note that there are 7 arguments here.

However, when you are calling it here:
FillRow(oDataDoc, 4, "Brian", "Valentine",
"12348 78th Street Apt. 214",
"Lubbock, TX 25874");

You have only provided 6 parameters.

--
Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com


campwes said:
Hey, all. I'm trying to develop a C# app that creates Word 2003 mail
merge
documents with an Oracle 9i database as the datasource. I used the
following
as an example of how I can start out:

http://support.microsoft.com/default.aspx?scid=kb;EN-US;301659

The problem is that the code provided doesn't allow me to add more users
to
the mail merge data file. When I try to add an additional user with the
following code, I get this error when I try to build:

"No overload for method 'FillRow' takes '6' arguments"

Here's the code I want to work. Please help.

<code>
private void FillRow(Word._Document oDoc, int Row, string Text1,
string Text2, string Text3, string Text4, string Text5)
{
// Insert the data into the specific cell.
oDoc.Tables[1].Cell(Row,1).Range.InsertAfter(Text1);
oDoc.Tables[1].Cell(Row,2).Range.InsertAfter(Text2);
oDoc.Tables[1].Cell(Row,3).Range.InsertAfter(Text3);
oDoc.Tables[1].Cell(Row,4).Range.InsertAfter(Text4);
oDoc.Tables[1].Cell(Row,5).Range.InsertAfter(Text5);
}

private void CreateMailMergeDataFile()
{
Word._Document oDataDoc;
int iCount;

Object oName = "C:\\DataDoc.doc";
Object oHeader = "FirstName, LastName, Address, CityStateZip";
wrdDoc.MailMerge.CreateDataSource(ref oName,ref oMissing,
ref oMissing,ref oHeader, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing);

// Open the file to insert data.
oDataDoc = wrdApp.Documents.Open(ref oName,ref oMissing,
ref oMissing, ref oMissing,ref oMissing,ref oMissing,
ref oMissing,ref oMissing,ref oMissing,ref oMissing,
ref oMissing,ref oMissing,ref oMissing,ref oMissing,
ref oMissing, ref oMissing);

for (iCount=1; iCount<=2; iCount++)
{
oDataDoc.Tables[1].Rows.Add(ref oMissing);
}
// Fill in the data.
FillRow(oDataDoc, 2, "Steve", "DeBroux",
"4567 Main Street", "Buffalo, NY 98052");
FillRow(oDataDoc, 3, "Jan", "Miksovsky",
"1234 5th Street", "Charlotte, NC 98765");
FillRow(oDataDoc, 4, "Brian", "Valentine",
"12348 78th Street Apt. 214",
"Lubbock, TX 25874");
FillRow(oDataDoc, 5, "FifthFirst", "FifthLast",
"6666 Phoney Street",
"Cowtown, CA 90218");
// Save and close the file.
oDataDoc.Save();
oDataDoc.Close(ref oFalse, ref oMissing, ref oMissing);
}
</code>
 
G

Guest

Ben,

That makes sense - thanks! What I don't understand now is that when I leave
the as:

private void FillRow(Word._Document oDoc, int Row, string Text1,
string Text2, string Text3, string Text4)
{
// Insert the data into the specific cell.
oDoc.Tables[1].Cell(Row,1).Range.InsertAfter(Text1);
oDoc.Tables[1].Cell(Row,2).Range.InsertAfter(Text2);
oDoc.Tables[1].Cell(Row,3).Range.InsertAfter(Text3);
oDoc.Tables[1].Cell(Row,4).Range.InsertAfter(Text4);
}

and add a new name to the bottom of the name/address list, the last two
names get smushed together like so:

BrianFifthFirst ValentineFifthLast
12348 78th Street Apt. 2146666 Phoney Street
Lubbock, TX 25874Cowtown, CA 90218

I just need to add additional rows to the merge data file. Thanks again.
 
G

Guest

"Ben Lucas" wrote:
No, because when I add a fifth line:

oDoc.Tables[1].Cell(Row,4).Range.InsertAfter(Text4);

I get an error. It's almost as if this code won't take more than 4
names/addresses. Any other ideas?

Thanks.

-campwes
 
G

Guest

All,

I figured it out - stupid newbie mistake. That's what I get for trying to
learn C# for a new project.

I needed to increment the Boolean value in the "for" clause:

for (iCount=1; iCount<=2; iCount++)

to:

for (iCount=1; iCount<=4; iCount++).

Doing this allows me to add as many contacts to the list as I want.

campwes said:
:
No, because when I add a fifth line:

oDoc.Tables[1].Cell(Row,4).Range.InsertAfter(Text4);

I get an error. It's almost as if this code won't take more than 4
names/addresses. Any other ideas?

Thanks.

-campwes
Are you perhaps using the same Row number for the 4th and 5th rows?

--
Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com
 
G

Guest

I forget where I saw that FillRow method before, but I know I downloaded it
from someplace. I was using it but found some limitations and it was slow.
In my search for the solution to the problem I was having, I found this
method and it works much better. (Sorry it's in VB .NET) I tried to strip
anything out that was custom to my application.

Private Function CreateDataSource(ByVal voWordApp As Word.Application)
Dim oWrdDataDoc As Word.DocumentClass
Dim i As Integer
Dim oDS As DataSet

oDS = 'Open your Dataset here
Dim sColumns As String
Dim sFldName As String
Dim sValues As String
Dim oDR As DataRow

For i = 0 To nColCnt - 1
'Create a tab delimited string of your column names
sColumns &= oDS.Tables(0).Columns(i).ColumnName & vbTab
Next i

'strip off the last tab
sColumns = sColumns .Substring(0, sColumns.LastIndexOf(vbTab)) &
vbcrlf

'Create our data document
oWrdDataDoc = voWordApp.Documents.Add()


'Create a table with a header row.
Dim oWrdRange As Word.Range = oWrdDataDoc.Content


With oWrdRange
.Collapse(WdCollapseDirection.wdCollapseEnd)
.InsertAfter(Text:=sColumns)
.Collapse(WdCollapseDirection.wdCollapseEnd)

'Now load our data into our table
For Each oDR In oDS.Tables(0).Rows
sValues = ""

'Get the values for our table
For Each sFldName In sColumns.Split(vbTab)
'Read each VALUE out of the datatable
sValues &= Trim(oDR.Item(sFldName)) & vbTab
Next sFldName

'Strip the last vbTab character
sValues = sValues.Substring(0, sValues.LastIndexOf(vbTab))

.InsertAfter(Text:=sValues & vbCrLf) 'Insert the new row of
data
.Collapse(WdCollapseDirection.wdCollapseEnd)
Next oDR

.Start = oWrdDataDoc.Range.Start

'Here is where the table is created
.ConvertToTable(vbTab)

Dim oWrdSelection As Word.Selection = voWordApp.Selection
oWrdSelection.Tables.Item(1).AllowAutoFit = False
oWrdSelection = Nothing
End With

oWrdDataDoc.SaveAs("c:\temp\datadoc.doc")

oWrdDataDoc.Close(False)
End Function
#End Region


campwes said:
Hey, all. I'm trying to develop a C# app that creates Word 2003 mail merge
documents with an Oracle 9i database as the datasource. I used the following
as an example of how I can start out:

http://support.microsoft.com/default.aspx?scid=kb;EN-US;301659

The problem is that the code provided doesn't allow me to add more users to
the mail merge data file. When I try to add an additional user with the
following code, I get this error when I try to build:

"No overload for method 'FillRow' takes '6' arguments"

Here's the code I want to work. Please help.

<code>
private void FillRow(Word._Document oDoc, int Row, string Text1,
string Text2, string Text3, string Text4, string Text5)
{
// Insert the data into the specific cell.
oDoc.Tables[1].Cell(Row,1).Range.InsertAfter(Text1);
oDoc.Tables[1].Cell(Row,2).Range.InsertAfter(Text2);
oDoc.Tables[1].Cell(Row,3).Range.InsertAfter(Text3);
oDoc.Tables[1].Cell(Row,4).Range.InsertAfter(Text4);
oDoc.Tables[1].Cell(Row,5).Range.InsertAfter(Text5);
}

private void CreateMailMergeDataFile()
{
Word._Document oDataDoc;
int iCount;

Object oName = "C:\\DataDoc.doc";
Object oHeader = "FirstName, LastName, Address, CityStateZip";
wrdDoc.MailMerge.CreateDataSource(ref oName,ref oMissing,
ref oMissing,ref oHeader, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing);

// Open the file to insert data.
oDataDoc = wrdApp.Documents.Open(ref oName,ref oMissing,
ref oMissing, ref oMissing,ref oMissing,ref oMissing,
ref oMissing,ref oMissing,ref oMissing,ref oMissing,
ref oMissing,ref oMissing,ref oMissing,ref oMissing,
ref oMissing, ref oMissing);

for (iCount=1; iCount<=2; iCount++)
{
oDataDoc.Tables[1].Rows.Add(ref oMissing);
}
// Fill in the data.
FillRow(oDataDoc, 2, "Steve", "DeBroux",
"4567 Main Street", "Buffalo, NY 98052");
FillRow(oDataDoc, 3, "Jan", "Miksovsky",
"1234 5th Street", "Charlotte, NC 98765");
FillRow(oDataDoc, 4, "Brian", "Valentine",
"12348 78th Street Apt. 214",
"Lubbock, TX 25874");
FillRow(oDataDoc, 5, "FifthFirst", "FifthLast",
"6666 Phoney Street",
"Cowtown, CA 90218");
// Save and close the file.
oDataDoc.Save();
oDataDoc.Close(ref oFalse, ref oMissing, ref oMissing);
}
</code>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top