Hi,
I have developed a desk top based application that create word file.
my code is below:
Word.Application wrdApp;
Word._Document wrdDoc;
Word.Table wrdTable;
Object oMissing = System.Reflection.Missing.Value;
Object oFalse = false;
<pre> private void button2_Click(object sender, EventArgs e)
{
wrdApp = new Word.Application();
wrdApp.Visible = false;
wrdDoc = wrdApp.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
Word.Selection wrdSelection;
Word.MailMerge wrdMailMerge;
wrdDoc.Select();
wrdSelection = wrdApp.Selection;
wrdMailMerge = wrdDoc.MailMerge;
int iCount;
Object oName = "D:\\ddd.doc";
Word.Table wrdTable = wrdDoc.Tables.Add(wrdSelection.Range, 9, 4,
ref oMissing, ref oMissing);
Object oHeader = "' ',' ',' ',' ' ";
wrdTable.Columns[1].SetWidth(300, Word.WdRulerStyle.wdAdjustSameWidth);
wrdTable.Columns[2].SetWidth(500, Word.WdRulerStyle.wdAdjustSameWidth);
wrdTable.Columns[3].SetWidth(400, Word.WdRulerStyle.wdAdjustSameWidth);
wrdTable.Columns[4].SetWidth(100, Word.WdRulerStyle.wdAdjustSameWidth);
wrdDoc.MailMerge.CreateDataSource(ref oName, ref oMissing,
ref oMissing, ref oHeader, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing);
wrdDoc = 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++)
{
wrdDoc.Tables[1].Rows.Add(ref oMissing);
}
FillRow(wrdDoc, 2, "I.D. Number : ", "" + txtReceipt.Text + "", "Date:", "" + dtDatePicker.Text + "");
FillRow(wrdDoc, 3, "Name of the patient :", "" + txtPatientName.Text + "", "Age :", "" + txtAge.Text + "");
FillRow(wrdDoc, 4, "Referred by :",""+txtRefDocter.Text+"","Sex:",""+txtSex.Text+"");
wrdDoc.Save();
wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing);
}
and
private void FillRow(Word._Document oDoc, int Row, string Text1,string Text2,string Text3,string Text4)
{
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);
}
my word file:
shows
M____ M____1 M___ M___1
I.D. Number : sfdsfdf Date: 07/15/2010
Name of the patient : sfdfds Age : sfdfd
Referred by : sfdfdf Sex: sfsfsd
Problem:
columns width in table cell isn't work and my word file header M_... like this I don't need this but if close header then a lot of header will appear and my existing document was erased but i strongly need it bec only this table will be inserted.
Please help me,I am in deep fix.
Any help thanks in advance.
Masud