المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Problem creating Multiple tables in word using c#



C# Programming
08-26-2009, 10:33 AM
Hello All,
I am trying to create 2 separate tables and print them in word, however I end up with two tables which are attached to each other..how do i differentiate between the two tables?or how do i seperate the two tables?is there a way in which i can insert a paragraph/break between the tables..can anyone please help?Also i am not able to add a second table to the tables collection.i get an error saying" The requested member of the collection does not exist."
Here is my code. I have put it in the button click event:

private void button1_Click(object sender, EventArgs e)
{

object missing = System.Reflection.Missing.Value;

object Visible=true;

object start1 = 0;

object end1 = 0;
object styleTypeTable = WdStyleType.wdStyleTypeTable;
ApplicationClass WordApp = new ApplicationClass();

Document adoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
//
Tables tb = adoc.Tables;
Object defaultTableBehavior = Type.Missing;
Object autoFitBehavior = Type.Missing;
Range rng = adoc.Range(ref start1, ref missing);
tb.Add(rng, 2,2, ref missing, ref missing);

Table tbl =adoc.Tables[1];
tbl.Borders.Enable = 1;
tbl.Cell(1, 1).Range.Text = "Hi";
tbl.Cell(1, 2).Range.Text = "Hi";
tbl.Cell(2, 1).Range.Text = "Hi";
tbl.Cell(2, 2).Range.Text = "Hi";
object strt =tbl.Range.End;

Range ran = adoc.Range(ref strt, ref strt); rng.Copy();
Table tbl2= tb.Add(ran, 2, 2, ref missing, ref missing);
//tbl2 = adoc.Tables[1];
tbl2.Borders.Enable = 1;
//tbl2.Cell(1, 1).Range.Text = "Second table";
tbl2.Cell(1, 1).Range.Text = "Second table";
tbl2.Cell(1, 2).Range.Text = "Second table";
tbl2.Cell(2, 1).Range.Text = "Second table";
tbl2.Cell(2, 2).Range.Text = "Second table";
try

{


object filename = @"D:\MyWord.doc";

adoc.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing,

ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

WordApp.Visible = true;

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}
}

Thanks in advance http://barakasoft.com/script/Forums/Images/smiley_smile.gif