Blog author:
Cc9530 | Posted on: 3/29/2011 | Category:
C# Blogs | Views: 2413 | Status:
[Member] |
Points: 75
|
Alert Moderator
Through C#, we can realize most of feature functions out of Microsoft Word.
We can create a new MS Word document, open Word document, add a new table into
Word document and even set up table cell properties at will. C# can help you do
these jobs without the necessary to install Microsoft Word on your PC.
Import COM Library: Microsoft word 11.0 Object Library.
Create New Word Document
object oMissing = System.Reflection.Missing.Value;
Word._Application
oWord;
Word._Document oDoc;
oWord = new
Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref
oMissing, ref oMissing,
ref oMissing, ref oMissing);
Open Word Document
object oMissing = System.Reflection.Missing.Value;
Word._Application
oWord;
Word._Document oDoc;
oWord = new
Word.Application();
oWord.Visible = true;
object fileName =
@"E:\CCCXCXX\TestDoc.doc";
oDoc = oWord.Documents.Open(ref fileName,
ref
oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref
oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref
oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
Import Sample
object oMissing = System.Reflection.Missing.Value;
Word._Application
oWord;
Word._Document oDoc;
oWord = new
Word.Application();
oWord.Visible = true;
object fileName =
@"E:\XXXCCX\Test.doc";
oDoc = oWord.Documents.Add(ref fileName, ref
oMissing,
ref oMissing, ref oMissing);
Add New Table
object oMissing = System.Reflection.Missing.Value;
Word._Application
oWord;
Word._Document oDoc;
oWord = new
Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref
oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
Word.Range tableLocation =
oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref
oMissing, ref oMissing);
Add New Row/Column
object oMissing = System.Reflection.Missing.Value;
Word._Application
oWord;
Word._Document oDoc;
oWord = new
Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref
oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
Word.Range tableLocation =
oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref
oMissing, ref oMissing);
Word.Table newTable = oDoc.Tables[1];
object beforeRow =
newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);
Merge Cells
object oMissing = System.Reflection.Missing.Value;
Word._Application
oWord;
Word._Document oDoc;
oWord = new
Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref
oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
Word.Range tableLocation =
oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref
oMissing, ref oMissing);
Word.Table newTable = oDoc.Tables[1];
object beforeRow =
newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);
Word.Cell cell = newTable.Cell(1, 1);
cell.Merge(newTable.Cell(1, 2));
Clip Cell
object oMissing = System.Reflection.Missing.Value;
Word._Application
oWord;
Word._Document oDoc;
oWord = new
Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(
Story From StephenPick
i could be the one