Word中幾個重要的對象介紹:
Application對象表示 Microsoft Office Word 2003應用程序本身。每次編寫代碼時,都應從Application對象開始。可以從Application對象訪問Word公開的所有其他對象和集合,以及Application對象自身的屬性和方法。
在Word中處理某個特定文檔時,這個文檔就稱為活動文檔,並且可通過Application對象的ActiveDocument屬性引用。所有的Word Document對象同時也是Application對象的Documents集合的成員,該集合由所有打開的文檔組成。使用Document對象時,允許使用單個文檔,而Documents集合則允許使用所有打開的文檔。由於在Application和Document級別都可以進行文檔操作,所以Application和Document類共享許多成員。
Selection對象表示 Microsoft Office Word 2003 文檔中當前選定的區域。在Word用戶界面中執行某項操作(例如,對文本進行加粗)時,應首先選擇或突出顯示目標文本,然後應用格式設置。可在代碼中以相同的方式使用Selection對象:先定義Selection,然後執行操作。可以使用Selection對象選擇、操作和打印文檔中的文本以及設置文本的格式。Selection對象始終存在於文檔中。如果未選中任何對象,它表示插入點。因此,在嘗試使用Selection對象執行任何操作之前,知道該對象包含哪些內容是很重要的。
Range對象與Selection對象共享著很多成員。二者之間的主要區別在於:Selection對象始終在用戶界面中返回對所選內容的引用,而Range對象允許在用戶界面中不顯示范圍的情況下處理文本。Range對象的主要優勢有:
Range 對象通常只需要較少行數的代碼就能完成給定任務。
Range 對象不會引起與 Word 必須移動或改變活動文檔中的突出顯示相關的系統開銷。
Range 對象比 Selection 對象的功能更強。
Bookmark對象與Range和Selection對象類似,因為它表示文檔中的連續區域,既有起始位置也有結束位置。書簽用於在文檔中標記一個位置,或者用作文檔中的文本容器。Bookmark對象可以小到只有一個插入點,也可以大到整篇文檔。您還可以在文檔中定義多個書簽。可以將Bookmark看作是保存在文檔中的一個指定位置。
打開Word:
Word.Application wapp = new Microsoft.Office.Interop.Word.Application();
wapp.Visible = true;
wapp = null;
打開一個現有的Word文檔:
Word.Application wapp = new Microsoft.Office.Interop.Word.Application();
wapp.Visible = true;
object filename = "E:\\Task.doc";
object isread = false;
object isvisible = true;
object miss = System.Reflection.Missing.Value;
wapp.Documents.Open(ref filename, ref miss, ref isread, ref miss, ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref isvisible, ref miss, ref miss, ref miss, ref miss);
wapp = null;
打開一個新文檔:
Word.Application wapp = new Microsoft.Office.Interop.Word.Application();
Word.Document adoc = new Microsoft.Office.Interop.Word.Document();
wapp.Visible = true;
object miss = System.Reflection.Missing.Value;
adoc = wapp.Documents.Add(ref miss, ref miss, ref miss, ref miss);
wapp = null;
第二篇是具體的實例
[csharp]
OpenFileDialog opd = new OpenFileDialog();
opd.InitialDirectory = "G:\\項目\\福沃德\\Project\\文本相似度檢測\\data\\";;
opd.Filter = "Word文檔(*.doc)|*.doc|文本文檔(*.txt)| *.txt|RTF文檔(*.rtf)|*.rtf|所有文檔(*.*)|*.*";
opd.FilterIndex = 1;
if (opd.ShowDialog() == DialogResult.OK && opd.FileName.Length > 0)
{
//建立Word類的實例,缺點:不能正確讀取表格,圖片等等的顯示
//Word.ApplicationClass app = new Word.ApplicationClass();
Word.Application app = new Microsoft.Office.Interop.Word.Application();
Word.Document doc = null;
object missing = System.Reflection.Missing.Value;
object FileName = opd.FileName;
object readOnly = false;
object isVisible = true;
object index = 0;
try {
doc = app.Documents.Open( ref FileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
doc.ActiveWindow.Selection.WholeStory();
doc.ActiveWindow.Selection.Copy(); //從剪切板獲取數據
IDataObject data=Clipboard.GetDataObject();
this.richTextBox1.Text= data.GetData(DataFormats.Text).ToString();
}
finally {
if (doc != null)
{
doc.Close(ref missing, ref missing, ref missing);
doc = null;
}
if (app != null)
{
app.Quit(ref missing, ref missing, ref missing);
app = null;
}
}
} //end if
OpenFileDialog opd = new OpenFileDialog();
opd.InitialDirectory = "G:\\項目\\福沃德\\Project\\文本相似度檢測\\data\\";;
opd.Filter = "Word文檔(*.doc)|*.doc|文本文檔(*.txt)| *.txt|RTF文檔(*.rtf)|*.rtf|所有文檔(*.*)|*.*";
opd.FilterIndex = 1;
if (opd.ShowDialog() == DialogResult.OK && opd.FileName.Length > 0)
{
//建立Word類的實例,缺點:不能正確讀取表格,圖片等等的顯示
//Word.ApplicationClass app = new Word.ApplicationClass();
Word.Application app = new Microsoft.Office.Interop.Word.Application();
Word.Document doc = null;
object missing = System.Reflection.Missing.Value;
object FileName = opd.FileName;
object readOnly = false;
object isVisible = true;
object index = 0;
try {
doc = app.Documents.Open( ref FileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
doc.ActiveWindow.Selection.WholeStory();
doc.ActiveWindow.Selection.Copy(); //從剪切板獲取數據
IDataObject data=Clipboard.GetDataObject();
this.richTextBox1.Text= data.GetData(DataFormats.Text).ToString();
}
finally {
if (doc != null)
{
doc.Close(ref missing, ref missing, ref missing);
doc = null;
}
if (app != null)
{
app.Quit(ref missing, ref missing, ref missing);
app = null;
}
}
} //end if
要注意引用
using Word = Microsoft.Office.Interop.Word;
三 相關的函數拓展,本人未親測
創建新Word:
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 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);
導入模板
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);
.添加新表
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);
.表插入行
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);
.單元格合並
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));
.單元格分離
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);
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));
object Rownum = 2;
object Columnnum = 2;
cell.Split(ref Rownum, ref Columnnum);
通過段落控制插入
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = ""; /**//* \endofdoc is a predefined bookmark */
//Start Word and create a new document.
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);
//Insert a paragraph at the beginning of the document.
Word.Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text = "Heading 1";
oPara1.Range.Font.Bold = 1;
oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter();