I am reading word document in C# (3.5) using Microsoft.Office.Interop.Word
. Reading line by line, splitting line into array[] and processing every word of line and based on some business logic replacing some words and after the replacement of words, replacing the complete line with converted line.
Till now Every thing is working fine.
Now i have some word documents, those are having paragraph and tables. I want to read every Column of the table one by one and replace the content of the column in particular column.
Update
Using Office Automation
1. Opening word file.
2. Moving cursor to top of the document
3. Selecting first line using (`wordApp.Selection.endKey`) and processing all words
4. After processing the words replacing the selected line with the processed line.
5. Using wordApp.Selection.MoveDown(ref lineCount, ref countPage, ref MISSING);
moving next line processed further.
Problem:
1. When reading table it reads only first column when using wordApp.Selection.endKey
I want to process all column's data.
Is there any way to identify whether content is paragraph or table?
Using selection for scaning a document should be quite expensive in performance.
I would suggest the following code:
List<Word.Range> TablesRanges = new List<Word.Range>();
wordApp = new Microsoft.Office.Interop.Word.Application();
doc = wordApp.Documents.OpenNoRepairDialog(FileName: @"c:\AAAAA.docx", ConfirmConversions: false, ReadOnly: true, AddToRecentFiles: false, NoEncodingDialog: true);
for (int iCounter = 1; iCounter <= doc.Tables.Count; iCounter++)
{
Word.Range TRange = doc.Tables[iCounter].Range;
TablesRanges.Add(TRange);
}
Boolean bInTable;
for (int par = 1; par <= doc.Paragraphs.Count; par++)
{
bInTable = false;
Word.Range r = doc.Paragraphs[par].Range;
foreach (Word.Range range in TablesRanges)
{
if (r.Start >= range.Start && r.Start <= range.End)
{
Console.WriteLine("In Table - Paragraph number " + par.ToString() + ":" + r.Text);
bInTable = true;
break;
}
}
if (!bInTable)
Console.WriteLine("!!!!!! Not In Table - Paragraph number " + par.ToString() + ":" + r.Text);
}
I found a workaround for the same.
Approach is listed below.
1. Open the Word Document using WordApp.Documents.Open()
2. Using Selection.MoveDown
to traverse the Document line by line
3. Skipping the content of the Table's Cells
4. At last processing only Tables of the document
//Process all Paragraphs in the documents
while (doc.ActiveWindow.Selection.Bookmarks.Exists(@"\EndOfDoc") == false)
{
doc.ActiveWindow.Selection.MoveDown(ref wdLine, ref wdCountOne, ref wdMove);
doc.ActiveWindow.Selection.HomeKey(ref wdLine, ref wdMove);
//Skiping table content
if (doc.ActiveWindow.Selection.get_Information(WdInformation.wdEndOfRangeColumnNumber).ToString() != "-1")
{
while (doc.ActiveWindow.Selection.get_Information(WdInformation.wdEndOfRangeColumnNumber).ToString() != "-1")
{
if (doc.ActiveWindow.Selection.Bookmarks.Exists(@"\EndOfDoc"))
break;
doc.ActiveWindow.Selection.MoveDown(ref wdLine, ref wdCountOne, ref wdMove);
doc.ActiveWindow.Selection.HomeKey(ref wdLine, ref wdMove);
}
doc.ActiveWindow.Selection.HomeKey(ref wdLine, ref wdMove);
}
doc.ActiveWindow.Selection.EndKey(ref wdLine, ref wdExtend);
currLine = doc.ActiveWindow.Selection.Text;
}
//Processing all tables in the documents
for (int iCounter = 1; iCounter <= doc.Tables.Count; iCounter++)
{
foreach (Row aRow in doc.Tables[iCounter].Rows)
{
foreach (Cell aCell in aRow.Cells)
{
currLine = aCell.Range.Text;
//Process Line
}
}
}