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方法進行復制、剪切、粘貼等操作
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)如何設置標題樣式,“標題一”,“標題二”等 參考
- public void AddTitle(string s)
- {
- //Word段落
- Word.Paragraph p;
- p = oDoc.Content.Paragraphs.Add(ref missing);
- //設置段落中的內容文本
- p.Range.Text = s;
- //設置爲一號標題
- object style = Word.WdBuiltinStyle.wdStyleHeading1;
- p.set_Style(ref style);
- //添加到末尾
- p.Range.InsertParagraphAfter(); //在應用 InsertParagraphAfter 方法之後,所選內容將擴展至包括新段落。
- }
- /// <summary>
- /// 添加普通段落
- /// </summary>
- /// <param name="s"></param>
- public void AddParagraph(string s)
- {
- Word.Paragraph p;
- p = oDoc.Content.Paragraphs.Add(ref missing);
- p.Range.Text = s;
- object style = Word.WdBuiltinStyle.wdStyleBodyText;
- p.set_Style(ref style);
- p.Range.InsertParagraphAfter();
- }
(2)如何插入表格
使用Word的Table類,有人還使用DataTable類進行輔助
(3)如何插入圖片
InlineShapes是Word中內嵌的圖形等資源
- public void InsertImage(string strPicPath, float picWidth, float picHeight)
- {
- string FileName = strPicPath;
- object LinkToFile = false;
- object SaveWithDocument = true;
- object Anchor = oWord.Selection.Range;
- oWord.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor).Select();
- oWord.Selection.InlineShapes[1].Width = picWidth; // 圖片寬度
- oWord.Selection.InlineShapes[1].Height = picHeight; // 圖片高度
- }
? 插入圖片後爲什麼又沒了?
這可能是由於你在插入圖片後,又插入東西,但是你沒有移動光標,所以把圖片給覆蓋掉了。
解決方法:光標移動
(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);
- // Go to a predefined bookmark, if the bookmark doesn't exists the application will raise an error
- public void GotoBookMark(string strBookMarkName)
- {
- // VB : Selection.GoTo What:=wdGoToBookmark, Name:="nome"
- object Bookmark = (int)Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
- object NameBookMark = strBookMarkName;
- oWord.Selection.GoTo(ref Bookmark, ref missing, ref missing, ref NameBookMark);
- }
- public void GoToTheEnd()
- {
- // VB : Selection.EndKey Unit:=wdStory
- object unit;
- unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
- oWord.Selection.EndKey(ref unit, ref missing);
- }
- public void GoToTheBeginning()
- {
- // VB : Selection.HomeKey Unit:=wdStory
- object unit;
- unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
- oWord.Selection.HomeKey(ref unit, ref missing);
- }
(5)生成目錄
- public void insertContent() //利用標題樣式生成目錄
- {
- GoToTheBeginning();
- object start = 0;
- object end = 0;
- Word.Range myRange = oWord.ActiveDocument.Range(ref start, ref end); //位置區域
- object useHeadingStyle = true; //使用Head樣式
- object upperHeadingLevel = 1; //最大一級
- object lowerHeadingLevel = 3; //最小三級
- object useHypeLinks = true;
- //TablesOfContents的Add方法添加目錄
- oDoc.TablesOfContents.Add(myRange, ref useHeadingStyle,
- ref upperHeadingLevel, ref lowerHeadingLevel,
- ref missing, ref missing, ref missing, ref missing,
- ref missing, ref useHypeLinks, ref missing, ref missing);
- oDoc.TablesOfContents[1].UpdatePageNumbers(); //更新頁碼
- }
- #endregion
(6)目錄格式怎麼設置?比如加粗、傾斜等
利用段落格式設置
- public void formatContent() {
- Word.TableOfContents myContent = oDoc.TablesOfContents[1]; //目錄
- Word.Paragraphs myParagraphs = myContent.Range.Paragraphs; //目錄裏的所有段,一行一段
- int[] FirstParaArray = new int[3]{ 1, 8, 9 }; //一級標題,直接指定
- foreach (int i in FirstParaArray) {
- myParagraphs[i].Range.Font.Bold = 1; //加粗
- myParagraphs[i].Range.Font.Name = "黑體"; //字體
- myParagraphs[i].Range.Font.Size = 12; //小四
- myParagraphs[i].Range.ParagraphFormat.SpaceBefore = 6; //段前
- myParagraphs[i].Range.ParagraphFormat.SpaceAfter = 6; //段後間距
- }
- }