Basically i have a Microsoft document in word in this format called biodata.docx:
| Name | Linda |
| DOB | 13 jan 2010 |
| Address | 1 nowhere street |
Basically, i want tobe able to upload this document in word, and display the contents in an asp.net form where each text box reads data from a diffent row.
i need help
Posted May 12, 2012, 2:25 AM
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
object WordFile = @"C:\Users\muralidharand\Documents\Name.docx";
object RdOnly = false;
object Visible = true;
object Missing = System.Reflection.Missing.Value;
Document Doc = WordApp.Documents.Open(ref WordFile, ref Missing, ref RdOnly, ref Missing, ref Missing,
ref Missing, ref Missing, ref Missing, ref Missing, ref Missing,
ref Missing, ref Visible, ref Missing, ref Missing, ref Missing,
ref Missing);
Microsoft.Office.Interop.Word.Table mytable = Doc.Tables[1];
foreach (Microsoft.Office.Interop.Word.Row item in mytable.Rows)
{
TableRow row = new TableRow();
for(int i =1;i
TableCell tablecell = new TableCell();
Microsoft.Office.Interop.Word.Cell cell = item.Cells[i];
if (i == 1)
{
tablecell.Text = cell.Range.Text;
}
else
{
TextBox text = new TextBox();
text.Text = cell.Range.Text;
tablecell.Controls.Add(text);
}
row.Cells.Add(tablecell);
}
Table1.Rows.Add(row);
}
WordApp.Documents.Close(ref Missing, ref Missing, ref Missing);
Doc = null;
WordApp = null;
Hope this helps you.
Rafael MonteroPosted Jun 21, 2013, 9:30 PM
venk yPosted Oct 31, 2012, 6:37 AM
thanks for your posts,
its realy very useful to me.
keep it up,
thanks,
venkat.
Posted May 15, 2012, 12:13 PM
There is a small change in the code that is used to determine to display the textbox or label.
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
object WordFile = @"C:\Users\muralidharand\Documents\Name.docx";
object RdOnly = false;
object Visible = true;
object Missing = System.Reflection.Missing.Value;
Document Doc = WordApp.Documents.Open(ref WordFile, ref Missing, ref RdOnly, ref Missing, ref Missing,
ref Missing, ref Missing, ref Missing, ref Missing, ref Missing,
ref Missing, ref Visible, ref Missing, ref Missing, ref Missing,
ref Missing);
Microsoft.Office.Interop.Word.Table mytable = Doc.Tables[1];
foreach (Microsoft.Office.Interop.Word.Row item in mytable.Rows)
{
TableRow row = new TableRow();
for(int i =1;i
{
TableCell tablecell = new TableCell();
Microsoft.Office.Interop.Word.Cell cell = item.Cells[i];
if (i == 1 || i==2)
{
tablecell.Text = cell.Range.Text;
}
else
{
TextBox text = new TextBox();
text.Text = cell.Range.Text;
tablecell.Controls.Add(text);
}
row.Cells.Add(tablecell);
}
Table1.Rows.Add(row);
}
WordApp.Documents.Close(ref Missing, ref Missing, ref Missing);
Doc = null;
WordApp = null;
uyoyo EdosioPosted May 15, 2012, 11:54 AM
Posted May 15, 2012, 11:30 AM
uyoyo EdosioPosted May 15, 2012, 10:15 AM
Thank you very much this code worked.
I noticed that when i upload a word document with alot of text large number of words in tables it throws an error "index out of range" but if the data in the table is few the code works.