Microsoft.Office.Interop.Word.Table tbl1;
Microsoft.Office.Interop.Word.Range wordRange = doc.Content;
tbl1 = doc.Content.Tables.Add(wordRange, 3, 2, ref objMiss, ref objMiss);//3 columns, 2 rows
tbl1.Borders.Enable = 1;
int cc = 1;
for (int r = 1; r <= TotalNumber; r++)
{
for (int c = 1; c <= 2; c++)
{
if (cc < UserNameOne.Length) //UserNameOne is a String Collection
{
tbl1.Cell(r, c).Range.Text = "Welcome to XYZ Company\n" + "User Name:" + UserNameOne[cc] + "\n" ;//I want the size of "Welcome to XYZ Company" to be 12 pt
tbl1.Cell(r, c).Range.Font.Bold = 0;
tbl1.Cell(r, c).Range.Font.Size = 21;
cc = cc + 1;
}
}
Sunny SharmaPosted May 28, 2013, 12:38 AM
Sorry, I couldn't get your point right the moment. Actually it was "same table" instead of "same cell". Anyway, Yes, there's a way you can achieve this purpose. For this, you need to take a Range object => assign it with the range that you want to modify => Set the Range object to denote a substring of text inside that range => Apply formatting. That's all my friend. Below is sample a sample of what I explained above:
table1.Cell(1, 1).Range.Text = "Sunny Kumar";
table1.Cell(1, 1).Range.Font.Color = WdColor.wdColorRed;
Range RangeToModify = table1.Cell(1, 1).Range;
RangeToModify.SetRange(table1.Cell(1, 1).Range.Start, table1.Cell(1, 1).Range.Words[1].End); // this will select the first word in range. SetRange() method just takes integer values as to slice a substring. You're free to put desired index number in Start and End if you're sure about.
RangeToModify.Font.Color = WdColor.wdColorGreen;
RangeToModify.Font.Size = 20;
Hope it helps!
Happy Coding :)
Prabhat KhadkaPosted Jun 7, 2013, 2:08 AM
Sunny SharmaPosted Jun 5, 2013, 2:15 PM
apologies for responding late, I didn't realize I had it in my inbox.
Interop.Word doesn't provide any Interface to restrict the number of rows in a table, however in your scenario, we can have a work around for this. How about having a table for every three rows in a document. If the number of rows exceed, add another document, add the table write another three rows and so on.
Thanks.
Prabhat KhadkaPosted Jun 2, 2013, 3:43 AM
Microsoft.Office.Interop.Word.Table tbl1;
Microsoft.Office.Interop.Word.Range wordRange = doc.Content;
tbl1 = doc.Content.Tables.Add(wordRange, TotalNumber, 2, ref objMiss, ref objMiss);//Here TotalNumber is number of rows that varies and computed beforehand.
tbl1.Borders.Enable = 1;
tbl1.AllowAutoFit = true;
int cc = 1;
for (int r = 1; r <= TotalNumber; r++)
{ for (int c = 1; c <= 2; c++)
{ if (cc < UserNameOne.Length)
{
tbl1.Cell(r, c).Range.Text = "Welcome to YZ Company" + "\n" + "User Name:" + UserNameOne[cc] + "\n" + "Password:" + UserPassword[cc] + "\n";// UserName and Password comes from string list
tbl1.Cell(r, c).Range.Font.Bold = 0;
tbl1.Cell(r, c).Range.Font.Size = 28; cc = cc + 1; } } } //////////////////////// In the code above, number of rows is determined by TotalNumber. like, if its 6 number of rows created are 6 in Word file. My problem is, I want that 6 rows in two pages, 3 rows in each page.
Thanks.
Sunny SharmaPosted May 30, 2013, 5:04 AM
Sorry for responding a little late, was busy with some other assignment. Can you please explain how the table looks after it's populated. How you've planned for the cells inside the table, are they fixed height/width, will, are they centered vertically etc..? Please send me a demo page that is system generated and also the modified one as per your preference. I'll feel happy if I could help.
Thanks.
Prabhat KhadkaPosted May 28, 2013, 6:11 AM
Sunny SharmaPosted May 28, 2013, 5:28 AM
Prabhat KhadkaPosted May 28, 2013, 4:57 AM
Sunny SharmaPosted May 28, 2013, 4:53 AM
Prabhat KhadkaPosted May 28, 2013, 4:40 AM
Sunny SharmaPosted May 28, 2013, 4:27 AM
Prabhat KhadkaPosted May 28, 2013, 4:06 AM
Prabhat KhadkaPosted May 27, 2013, 9:39 PM
Sunny SharmaPosted May 27, 2013, 5:07 AM
You can do so by manipulating the "Font" property of Table.Cell().Range object.
Below is a sample:
table1.Cell(1, 1).Range.Text = "Sunny Kumar";
table1.Cell(1, 2).Range.Font.Color = WdColor.wdColorRed;
table1.Cell(1, 2).Range.Font.Size = 15;
table1.Cell(1, 2).Range.Text = "Text in 15px Red Color";
table1.Cell(1, 3).Range.Font.Color = WdColor.wdColorBlue;
table1.Cell(1, 3).Range.Font.Size = 25;
table1.Cell(1, 3).Range.Text = "Text in 25px Blue Color";
Happy Coding :)
Please don't forget to accept this as answer if it helps!
Thanks.