Edit a Word Table Using C#

A

agraashish

Hi All,
I want to open a word document and search the last table in thedocumnet.Then
I want to select a specific column and append it in the table and then save
the word document.
I have tried many times but I am alwys facing the following problems:
1)The documnent alwys opens as a "Read Only Doc".
2)When I add a .Close() method on word object I get a warning saying
"Ambiguity between method 'Microsoft.Office.Interop.Word._Document.Close(ref
object, ref object, ref object)' and non-method
'Microsoft.Office.Interop.Word.DocumentEvents2_Event.Close'. Using method
group."Hence the Word Process does not end.
3)Most importantly I am not able to Paste the selected column of the table
in the end of the table as there is no parameter in the .Paste() method.
Thanks
 
A

agraashish

This is the code that I am using :
Microsoft.Office.Interop.Word.ApplicationClass wordApp = new
ApplicationClass();
object file = @"c:\TPS.doc";
object nullobj = System.Reflection.Missing.Value;
object readOnly = false;
object isVisible = true;
Microsoft.Office.Interop.Word.Document doc =
wordApp.Documents.Open(ref file, ref nullobj, ref readOnly,
ref nullobj, ref nullobj,
ref nullobj,
ref nullobj, ref nullobj,
ref nullobj,
ref nullobj, ref nullobj,
ref nullobj,
ref nullobj, ref nullobj,
ref nullobj, ref nullobj);
doc.Activate();
int totalTables = doc.Tables.Count;
Table tab = doc.Tables[totalTables - 1];

int maxCol = tab.Columns.Count;
int selectedColumn = 3;//Hardcoded the selected column for time
being
tab.Columns[selectedColumn].Select();
doc.ActiveWindow.Selection.Copy();

object beforeColumn = tab.Columns[maxCol];//I want to add a new
column in the end and then paste the selected column
tab.Columns.Add(ref beforeColumn);//but using Columns.Add() I am
able to insert the column at the second last position
doc.ActiveWindow.Selection.Paste();//Problem:pastes the selected
column as the first column of the table
doc.Close(ref nullobj, ref nullobj, ref nullobj);//Warning:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top