C# 操作Word知識彙總

分類: C#學習筆記
 3875人閱讀 評論(1) 收藏 舉報

1. 微軟官方實例:

段落、表格、圖表

HOW TO:利用 Visual C# .NET 使 Word 自動新建文檔

 

2. 學習資源

(1)Word in the Office 基礎知識,必讀,下面的總結裏有內容摘要

http://msdn.microsoft.com/en-us/library/Aa201330

網友翻譯版:http://blog.csdn.net/hustliangchen/archive/2011/01/05/6118459.aspx

(2)Word類的結構圖,Application、document、Range、Selection等,必讀

http://msdn.microsoft.com/en-us/library/aa164012

(3)word 2007 對象模型 :

http://msdn.microsoft.com/en-us/library/bb244515%28v=office.12%29.aspx

(4)Microsoft.Office.Interop.Word

http://msdn.microsoft.com/zh-cn/library/ms254954%28v=Office.11%29.aspx

(5)WPS 二次開發接口文檔 wpsapi.chm

中文方便閱讀,CSDN下載

(6)飛蛾 Word VBA 參考教程

全中文Word類庫,必讀

http://www.feiesoft.com/vba/word/

(7)用VBA宏提高Word操作效率——需要精研的20個經典實例

http://pcmag.hexun.com/2010-03-18/123031946.html

 

3.一些總結

(1)Document 代表一個具體的word文檔, Documents 是 Document的集合,用index索引來表示某一個document。ActiveDocument屬性是當前焦點(focus)的document。我們一般不會用索引來引用文檔,因爲索引值會隨着文檔的打開和關閉而改變;通常我們用 ActiveDocument屬性,或者 Documents 集合的Add 或 Open方法返回的document對象變量來引用。Add或Open的document會成爲 ActiveDocument,如果想使其它document成爲activeDocument,則使用 document對象的ActiveDocument方法。

用文件名指明具體Documnet,Documents("Report.doc").Activate();

 

(2) characters組成words,words組成sentences,sentences組成paragraphs,因此一個document中會包含這樣四個集合: Characters Words, Sentences ,Paragraphs collection。此外,document還可能包含sections的集合,而一個section又會有HeadersFooters 頁眉頁腳集合。

 

(3)Paragraph段落,由一個段落標誌和所有文本組成。拷貝段落時如果包含了段落標誌,則段落格式也會一同拷貝。不想拷貝格式的話就不要拷貝段落標誌。

 

(3)Range對象,代表一塊連續的由起始字符和結束字符定義的區域,可以小到只有一個插入光標或大至整個文檔內容,它也可以是但並不必須是當前selection代表的區域。可以在一個文檔中定義多個Range對象。

我們通常用Range類定義一個變量來創建Range對象,然後用Document的Range方法或其它對象的Range屬性來實例化這個Range對象。

 

(4)Selection對象

可代表光標

該對象代表窗口或窗格中的當前所選內容。所選內容代表文檔中被選定(或突出顯示的)的區域,若文檔中沒有所選內容,則代表插入點。每個文檔窗格只能有一個活動的 Selection對象,並且整個應用程序中只能有一個活動的 Selection對象。
使用 Selection對象
使用Selection屬性可返回 Selection對象。如果沒有使用 Selection屬性的對象識別符,Word 將返回活動文檔窗口的活動窗格中的所選內容。

Text屬性是其選中的內容

Copy、Cut、Paste方法進行復制、剪切、粘貼等操作

 

(5)sections

Sections.Add 方法

該方法用於返回一個 Section 對象,該對象表示添加至文檔中的新節。
Function Add([Range As Range = 0],  [Start As WpsSectionStart = 2]) As Section

參數說明
Range    Variant 類型,可選。在其之前插入分節符的區域。如果忽略本參數,則將分節符插至文檔末尾。
Start    Variant 類型,可選。要添加的分節符類型。WpsSectionStart 類型。如果忽略本參數,則添加“下一頁”類型的分節符。
    WpsSectionStart 類型可以是下列常量之一:
    值     描述
    wpsSectionContinuous     連續分節符
    wpsSectionEvenPage     偶數頁分節符
    wpsSectionNewColumn     節的結尾
    wpsSectionNewPage     下一頁分節符(默認)
    wpsSectionOddPage     奇數頁分節符

Sections 參考MSDN

Section  參考MSDN

 

4. 具體使用

(1)如何設置標題樣式,“標題一”,“標題二”等 參考

[c-sharp] view plaincopy
  1. public void AddTitle(string s)  
  2. {  
  3.     //Word段落  
  4.     Word.Paragraph p;  
  5.     p = oDoc.Content.Paragraphs.Add(ref missing);  
  6.     //設置段落中的內容文本  
  7.     p.Range.Text = s;  
  8.     //設置爲一號標題  
  9.     object style = Word.WdBuiltinStyle.wdStyleHeading1;  
  10.     p.set_Style(ref style);  
  11.     //添加到末尾  
  12.     p.Range.InsertParagraphAfter();  //在應用 InsertParagraphAfter 方法之後,所選內容將擴展至包括新段落。  
  13. }  
  14. /// <summary>  
  15. /// 添加普通段落  
  16. /// </summary>  
  17. /// <param name="s"></param>  
  18. public void AddParagraph(string s)  
  19. {  
  20.     Word.Paragraph p;  
  21.     p = oDoc.Content.Paragraphs.Add(ref missing);  
  22.     p.Range.Text = s;  
  23.     object style = Word.WdBuiltinStyle.wdStyleBodyText;  
  24.     p.set_Style(ref style);  
  25.     p.Range.InsertParagraphAfter();  
  26. }  

 

(2)如何插入表格

使用Word的Table類,有人還使用DataTable類進行輔助

 

(3)如何插入圖片

InlineShapes是Word中內嵌的圖形等資源

[c-sharp] view plaincopy
  1. public void InsertImage(string strPicPath, float picWidth, float picHeight)  
  2.         {  
  3.             string FileName = strPicPath;  
  4.             object LinkToFile = false;  
  5.             object SaveWithDocument = true;  
  6.             object Anchor = oWord.Selection.Range;  
  7.             oWord.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor).Select();  
  8.             oWord.Selection.InlineShapes[1].Width = picWidth; // 圖片寬度   
  9.             oWord.Selection.InlineShapes[1].Height = picHeight; // 圖片高度  
  10.         }  

 

? 插入圖片後爲什麼又沒了?

這可能是由於你在插入圖片後,又插入東西,但是你沒有移動光標,所以把圖片給覆蓋掉了。

解決方法:光標移動

 

(4)光標移動

A:標籤:

系統預定義標籤:object oEndOfDoc = "//endofdoc"; /* /endofdoc is a predefined bookmark 系統預定義的書籤?*/

自定義標籤:

B:利用標籤獲取位置

Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;

插入段落、表格時都會用到這個位置:

oPara3 = oDoc.Content.Paragraphs.Add(ref oRng);

oTable = oDoc.Tables.Add(wrdRng, 3, 5, ref oMissing, ref oMissing);

[c-sharp] view plaincopy
  1. // Go to a predefined bookmark, if the bookmark doesn't exists the application will raise an error  
  2.         public void GotoBookMark(string strBookMarkName)  
  3.         {  
  4.             // VB :  Selection.GoTo What:=wdGoToBookmark, Name:="nome"  
  5.             object Bookmark = (int)Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;  
  6.             object NameBookMark = strBookMarkName;  
  7.             oWord.Selection.GoTo(ref Bookmark, ref missing, ref missing, ref NameBookMark);  
  8.         }          
  9.         public void GoToTheEnd()  
  10.         {  
  11.             // VB :  Selection.EndKey Unit:=wdStory  
  12.             object unit;  
  13.             unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;  
  14.             oWord.Selection.EndKey(ref unit, ref missing);  
  15.         }  
  16.         public void GoToTheBeginning()  
  17.         {  
  18.             // VB : Selection.HomeKey Unit:=wdStory  
  19.             object unit;  
  20.             unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;  
  21.             oWord.Selection.HomeKey(ref unit, ref missing);  
  22.         }  

 

(5)生成目錄

[c-sharp] view plaincopy
  1. public void insertContent() //利用標題樣式生成目錄  
  2.         {  
  3.             GoToTheBeginning();  
  4.             object start = 0;  
  5.             object end = 0;  
  6.             Word.Range myRange = oWord.ActiveDocument.Range(ref start, ref end); //位置區域  
  7.             object useHeadingStyle = true//使用Head樣式  
  8.             object upperHeadingLevel = 1;  //最大一級  
  9.             object lowerHeadingLevel = 3;  //最小三級  
  10.             object useHypeLinks = true;  
  11.             //TablesOfContents的Add方法添加目錄  
  12.             oDoc.TablesOfContents.Add(myRange, ref useHeadingStyle,  
  13.                 ref upperHeadingLevel, ref lowerHeadingLevel,  
  14.                 ref missing, ref missing, ref missing, ref missing,  
  15.                 ref missing, ref useHypeLinks, ref missing, ref missing);  
  16.             oDoc.TablesOfContents[1].UpdatePageNumbers(); //更新頁碼  
  17.         }  
  18.         #endregion  

 

(6)目錄格式怎麼設置?比如加粗、傾斜等

利用段落格式設置

[c-sharp] view plaincopy
  1. public void formatContent() {  
  2.             Word.TableOfContents myContent = oDoc.TablesOfContents[1]; //目錄  
  3.             Word.Paragraphs myParagraphs = myContent.Range.Paragraphs; //目錄裏的所有段,一行一段  
  4.             int[] FirstParaArray = new int[3]{ 1, 8, 9 }; //一級標題,直接指定  
  5.             foreach (int i in FirstParaArray) {  
  6.                 myParagraphs[i].Range.Font.Bold = 1;  //加粗  
  7.                 myParagraphs[i].Range.Font.Name = "黑體"//字體  
  8.                 myParagraphs[i].Range.Font.Size = 12; //小四  
  9.                 myParagraphs[i].Range.ParagraphFormat.SpaceBefore = 6; //段前  
  10.                 myParagraphs[i].Range.ParagraphFormat.SpaceAfter = 6; //段後間距  
  11.             }  
  12. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章