天天看點

word程式設計 vsto2005 (轉)

  word程式設計 vsto2005 (轉) 2008-08-07 15:18

word文檔工程變量的

     //合并單元格

   table.Cell(2, 2).Merge(table.Cell(2, 3));

//單元格分離

    object Rownum = 2;

    object Columnnum = 2;

    table.Cell(2, 2).Split(ref Rownum, ref Columnnum);

//單元格對齊方式

     WApp.Selection.Cells.VerticalAlignment =Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

//插入表行

     table.Rows.Add(ref missing);

//分頁 object ib = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;

    WApp.Selection.InsertBreak(ref ib);

//換行

     WApp.Selection.TypeParagraph();

二、word文檔設定

WApp.ActiveDocument.PageSetup.LineNumbering.Active =0;//行編号

            WApp.ActiveDocument.PageSetup.Orientation =Microsoft.Office.Interop.Word.WdOrientation.wdOrientPortrait;//頁面方向

            WApp.ActiveDocument.PageSetup.TopMargin =WApp.CentimetersToPoints(float.Parse("2.54"));//上頁邊距

            WApp.ActiveDocument.PageSetup.BottomMargin = WApp.CentimetersToPoints(float.Parse("2.54"));//下頁邊距

            WApp.ActiveDocument.PageSetup.LeftMargin = WApp.CentimetersToPoints(float.Parse("3.17"));//左頁邊距

            WApp.ActiveDocument.PageSetup.RightMargin = WApp.CentimetersToPoints(float.Parse("3.17"));//右頁邊距

            WApp.ActiveDocument.PageSetup.Gutter = WApp.CentimetersToPoints(float.Parse("0"));//裝訂線位置

            WApp.ActiveDocument.PageSetup.HeaderDistance = WApp.CentimetersToPoints(float.Parse("1.5"));//頁眉

            WApp.ActiveDocument.PageSetup.FooterDistance = WApp.CentimetersToPoints(float.Parse("1.75"));//頁腳

            WApp.ActiveDocument.PageSetup.PageWidth = WApp.CentimetersToPoints(float.Parse("21"));//紙張寬度

            WApp.ActiveDocument.PageSetup.PageHeight = WApp.CentimetersToPoints(float.Parse("29.7"));//紙張高度

            WApp.ActiveDocument.PageSetup.FirstPageTray = Microsoft.Office.Interop.Word.WdPaperTray.wdPrinterDefaultBin;//紙張來源

            WApp.ActiveDocument.PageSetup.OtherPagesTray = Microsoft.Office.Interop.Word.WdPaperTray.wdPrinterDefaultBin;//紙張來源

            WApp.ActiveDocument.PageSetup.SectionStart = Microsoft.Office.Interop.Word.WdSectionStart.wdSectionNewPage;//節的起始位置:新 建頁

            WApp.ActiveDocument.PageSetup.OddAndEvenPagesHeaderFooter = 0;//頁眉頁腳-奇偶頁不同

            WApp.ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = 0;//頁眉頁腳-首頁不同

            WApp.ActiveDocument.PageSetup.VerticalAlignment = Microsoft.Office.Interop.Word.WdVerticalAlignment.wdAlignVerticalTop;//頁 面垂直對齊方式

            WApp.ActiveDocument.PageSetup.SuppressEndnotes =0;//不隐藏尾注

            WApp.ActiveDocument.PageSetup.MirrorMargins = 0;//不設定首頁的内外邊距

            WApp.ActiveDocument.PageSetup.TwoPagesOnOne = false;//不雙面列印

            WApp.ActiveDocument.PageSetup.BookFoldPrinting =false;//不設定手動雙面正面列印

            WApp.ActiveDocument.PageSetup.BookFoldRevPrinting =false;//不設定手動雙面背面列印

            WApp.ActiveDocument.PageSetup.BookFoldPrintingSheets = 1;//列印預設份數

            WApp.ActiveDocument.PageSetup.GutterPos = Microsoft.Office.Interop.Word.WdGutterStyle.wdGutterPosLeft;//裝訂線位于左側

            WApp.ActiveDocument.PageSetup.LinesPage = 40;//預設頁行數量

            WApp.ActiveDocument.PageSetup.LayoutMode = Microsoft.Office.Interop.Word.WdLayoutMode.wdLayoutModeLineGrid;//版式模式為“ 隻指定行網格”

   三、光标移動

//移動光标

//光标下移3行 上移3行

            object unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;

            object count = 3;

            WApp.Selection.MoveEnd(ref unit,ref count);

            WApp.Selection.MoveUp(ref unit, ref count, ref missing);

//Microsoft.Office.Interop.Word.WdUnits說明

            //wdCell                  A cell.

            //wdCharacter             A character.

            //wdCharacterFormatting   Character formatting.

            //wdColumn                A column.

            //wdItem                  The selected item.

            //wdLine                  A line. //行

            //wdParagraph             A paragraph.

            //wdParagraphFormatting   Paragraph formatting.

            //wdRow                   A row.

            //wdScreen                The screen dimensions.

            //wdSection               A section.

            //wdSentence              A sentence.

            //wdStory                 A story.

            //wdTable                 A table.

            //wdWindow                A window.

            //wdWord                  A word.

//錄制的vb宏

            //     ,移動光标至目前行首

            //    Selection.HomeKey unit:=wdLine

            //    '移動光标至目前行尾

            //    Selection.EndKey unit:=wdLine

            //    '選擇從光标至目前行首的内容

            //    Selection.HomeKey unit:=wdLine, Extend:=wdExtend

            //    '選擇從光标至目前行尾的内容

            //    Selection.EndKey unit:=wdLine, Extend:=wdExtend

            //    '選擇目前行

            //    Selection.HomeKey unit:=wdLine

            //    Selection.EndKey unit:=wdLine, Extend:=wdExtend

            //    '移動光标至文檔開始

            //    Selection.HomeKey unit:=wdStory

            //    '移動光标至文檔結尾

            //    Selection.EndKey unit:=wdStory

            //    '選擇從光标至文檔開始的内容

            //    Selection.HomeKey unit:=wdStory, Extend:=wdExtend

            //    '選擇從光标至文檔結尾的内容

            //    Selection.EndKey unit:=wdStory, Extend:=wdExtend

            //    '選擇文檔全部内容(從WholeStory可猜出Story應是目前文檔的意思)

            //    Selection.WholeStory

            //    '移動光标至目前段落的開始

            //    Selection.MoveUp unit:=wdParagraph

            //    '移動光标至目前段落的結尾

            //    Selection.MoveDown unit:=wdParagraph

            //    '選擇從光标至目前段落開始的内容

            //    Selection.MoveUp unit:=wdParagraph, Extend:=wdExtend

            //    '選擇從光标至目前段落結尾的内容

            //    Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend

            //    '選擇光标所在段落的内容

            //    Selection.MoveUp unit:=wdParagraph

            //    Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend

            //    '顯示選擇區的開始與結束的位置,注意:文檔第1個字元的位置是0

            //    MsgBox ("第" & Selection.Start & "個字元至第" & Selection.End & "個字元")

            //    '删除目前行

            //    Selection.HomeKey unit:=wdLine

            //    Selection.EndKey unit:=wdLine, Extend:=wdExtend

            //    Selection.Delete

            //    '删除目前段落

            //    Selection.MoveUp unit:=wdParagraph

            //    Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend

            //    Selection.Delete

//表格的光标移動

//光标到目前光标所在表格的地單元格

WApp.Selection.Tables[1].Cell(1, 1).Select();

//unit對象定義

object unith = Microsoft.Office.Interop.Word.WdUnits.wdRow;//表格行方式

            object extend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;/**extend對光标移動區域進行擴充選擇

            object unitu = Microsoft.Office.Interop.Word.WdUnits.wdLine;//文檔行方式,可以看成表格一行.不過和wdRow有差別

            object unitp = Microsoft.Office.Interop.Word.WdUnits.wdParagraph;//段落方式,對于表格可以選擇到表格行後的換車符,對于跨行合并的行選擇,我能找到的最簡單方式

            object count=1;//光标移動量

下面代碼示範對于存在合并單元格的選擇操作.合并單元格的選擇問題一直是word的bug.部分object對象參照上面代碼

上面這個是表格合并樣式.要如何才能選擇2行标題欄尼.看下面代碼

//定位到表格第1單元格

WApp.Selection.Tables[1].Cell(1, 1).Select();

//定位到第1個單元格第1個字元前         

WApp.Selection.HomeKey(ref unith, ref missing);

//擴充到行尾,選擇表第1行           

WApp.Selection.EndKey(ref unith, ref extend);

//定義表格标題的行數量,titlerow為參數           

object strtitlerow=titlerow-1;

//移動光标選擇第1行的末尾段落标記           

WApp.Selection.MoveDown(ref unitp, ref count, ref extend);

//選擇下一行,因為合并的原因,如表格标題最後列是合并,隻選擇了2行的部分

            WApp.Selection.MoveDown(ref unitu, ref strtitlerow, ref extend);

//擴充到該行的末端,保證合并行能全部選擇到

            WApp.Selection.EndKey(ref unith, ref extend);

//複制選擇内容到剪貼闆

            WApp.Selection.Copy();

//下面是移動光标到任何位置并粘貼内容.我程式中目的是到表格換頁的時候自動插入下一頁的表頭.

            WApp.Selection.Tables[1].Cell(System.Convert.ToInt32(strRownum), 1).Select();

            WApp.Selection.HomeKey(ref unith, ref missing);

            WApp.Selection.Paste();

四、段落格式設定

//段落格式設定

            WApp.Selection.ParagraphFormat.LeftIndent = WApp.CentimetersToPoints(float.Parse("0"));//左縮進

            WApp.Selection.ParagraphFormat.RightIndent = WApp.CentimetersToPoints(float.Parse("0"));//右縮進

            WApp.Selection.ParagraphFormat.SpaceBefore =float.Parse("0");//段前間距

            WApp.Selection.ParagraphFormat.SpaceBeforeAuto =0;//

            WApp.Selection.ParagraphFormat.SpaceAfter = float.Parse("0");//段後間距

            WApp.Selection.ParagraphFormat.SpaceAfterAuto = 0;//

            WApp.Selection.ParagraphFormat.LineSpacingRule = Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpaceSingle;//單倍行距

            WApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;// 段落2端對齊

            WApp.Selection.ParagraphFormat.WidowControl = 0;//孤行控制

            WApp.Selection.ParagraphFormat.KeepWithNext = 0;//與下段同頁

            WApp.Selection.ParagraphFormat.KeepTogether = 0;//段中不分頁

            WApp.Selection.ParagraphFormat.PageBreakBefore = 0;//段前分頁

            WApp.Selection.ParagraphFormat.NoLineNumber = 0;//取消行号

            WApp.Selection.ParagraphFormat.Hyphenation = 1;//取消段字

            WApp.Selection.ParagraphFormat.FirstLineIndent = WApp.CentimetersToPoints(float.Parse("0"));//首行縮進

            WApp.Selection.ParagraphFormat.OutlineLevel = Microsoft.Office.Interop.Word.WdOutlineLevel.wdOutlineLevelBodyText;

            WApp.Selection.ParagraphFormat.CharacterUnitLeftIndent = float.Parse("0");

            WApp.Selection.ParagraphFormat.CharacterUnitRightIndent = float.Parse("0");

            WApp.Selection.ParagraphFormat.CharacterUnitFirstLineIndent = float.Parse("0");

            WApp.Selection.ParagraphFormat.LineUnitBefore = float.Parse("0");

            WApp.Selection.ParagraphFormat.LineUnitAfter = float.Parse("0");

            WApp.Selection.ParagraphFormat.AutoAdjustRightIndent = 1;

            WApp.Selection.ParagraphFormat.DisableLineHeightGrid =0;

            WApp.Selection.ParagraphFormat.FarEastLineBreakControl =1;

            WApp.Selection.ParagraphFormat.WordWrap = 1;

            WApp.Selection.ParagraphFormat.HangingPunctuation = 1;

            WApp.Selection.ParagraphFormat.HalfWidthPunctuationOnTopOfLine = 0;

            WApp.Selection.ParagraphFormat.AddSpaceBetweenFarEastAndAlpha = 1;

            WApp.Selection.ParagraphFormat.AddSpaceBetweenFarEastAndDigit = 1;

            WApp.Selection.ParagraphFormat.BaseLineAlignment = Microsoft.Office.Interop.Word.WdBaselineAlignment.wdBaselineAlignAuto;

五、字型格式設定

//字型格式設定

            WApp.Selection.Font.NameFarEast = "華文中宋";

            WApp.Selection.Font.NameAscii = "Times New Roman";

            WApp.Selection.Font.NameOther = "Times New Roman";

            WApp.Selection.Font.Name = "宋體";

            WApp.Selection.Font.Size = float.Parse("14");

            WApp.Selection.Font.Bold = 0;

            WApp.Selection.Font.Italic = 0;

            WApp.Selection.Font.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineNone;

            WApp.Selection.Font.UnderlineColor = Microsoft.Office.Interop.Word.WdColor.wdColorAutomatic;

            WApp.Selection.Font.StrikeThrough =0;//删除線

            WApp.Selection.Font.DoubleStrikeThrough = 0;//雙删除線

            WApp.Selection.Font.Outline =0;//空心

            WApp.Selection.Font.Emboss = 0;//陽文

            WApp.Selection.Font.Shadow = 0;//陰影

            WApp.Selection.Font.Hidden = 0;//隐藏文字

            WApp.Selection.Font.SmallCaps = 0;//小型大寫字母

            WApp.Selection.Font.AllCaps = 0;//全部大寫字母

            WApp.Selection.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorAutomatic;

            WApp.Selection.Font.Engrave = 0;//陰文

            WApp.Selection.Font.Superscript = 0;//上标

            WApp.Selection.Font.Subscript = 0;//下标

            WApp.Selection.Font.Spacing = float.Parse("0");//字元間距

            WApp.Selection.Font.Scaling = 100;//字元縮放

            WApp.Selection.Font.Position = 0;//位置

            WApp.Selection.Font.Kerning = float.Parse("1");//字型間距調整

            WApp.Selection.Font.Animation = Microsoft.Office.Interop.Word.WdAnimation.wdAnimationNone;//文字效果

            WApp.Selection.Font.DisableCharacterSpaceGrid =false;

            WApp.Selection.Font.EmphasisMark = Microsoft.Office.Interop.Word.WdEmphasisMark.wdEmphasisMarkNone;

六、終于找到了擷取光标位置的東東。那裡找到的忘了,感謝提供的老大。放到這裡供大家參考。

有了這個和上面内容,相信大家對word文檔的控制應該到了随心所欲的地步,爽啊

擷取的c#文法 //get_Information

Selection.get_Information(WdInformation.wdActiveEndPageNumber)

//關于行号-頁号-列号-位置

            //information 屬性

            //傳回有關指定的所選内容或區域的資訊。variant 類型,隻讀。

            //expression.information(type)

            //expression 必需。該表達式傳回一個 range 或 selection 對象。

            //type long 類型,必需。需要傳回的資訊。可取下列 wdinformation 常量之一:

            //wdactiveendadjustedpagenumber 傳回頁碼,在該頁中包含指定的所選内容或區域的活動結尾。如果設定了一個起始頁碼,并對頁碼進行了手工調整,則傳回調整過的頁碼。

            //wdactiveendpagenumber 傳回頁碼,在該頁中包含指定的所選内容或區域的活動結尾,頁碼從文檔的開頭開始計算而不考慮對頁碼的任何手工調整。

            //wdactiveendsectionnumber 傳回節号,在該節中包含了指定的所選内容或區域的活動結尾。

            //wdatendofrowmarker 如果指定的所選内容或區域位于表格的行結尾标記處,則本參數傳回 true。

            //wdcapslock 如果大寫字母鎖定模式有效,則本參數傳回 true。

            //wdendofrangecolumnnumber 傳回表格列号,在該表格列中包含了指定的所選内容或區域的活動結尾。

            //wdendofrangerownumber 傳回表格行号,在該表格行包含了指定的所選内容或區域的活動結尾。

            //wdfirstcharactercolumnnumber 傳回指定的所選内容或區域中第一個字元的位置。如果所選内容或區域是折疊的,則傳回所選内容或區域右側緊接着的字元編号。

            //wdfirstcharacterlinenumber 傳回所選内容中第一個字元的行号。如果 pagination 屬性為 false,或 draft 屬性為 true,則傳回 - 1。

            //wdframeisselected 如果所選内容或區域是一個完整的圖文框文本框,則本參數傳回 true。

            //wdheaderfootertype 傳回一個值,該值表明包含了指定的所選内容或區域的頁眉或頁腳的類型,如下表所示。 值 頁眉或頁腳的類型

            //- 1 無

            //0 偶數頁頁眉

            //1 奇數頁頁眉

            //2 偶數頁頁腳

            //3 奇數頁頁腳

            //4 第一個頁眉

            //5 第一個頁腳

            //wdhorizontalpositionrelativetopage 傳回指定的所選内容或區域的水準位置。該位置是所選内容或區域的左邊與頁面的左邊之間的距離,以磅為機關。如果所選内容或區域不可見,則傳回 - 1。

            //wdhorizontalpositionrelativetotextboundary 傳回指定的所選内容或區域相對于周圍最近的正文邊界的左邊的水準位置,以磅為機關。如果所選内容或區域沒有顯示在目前螢幕,則本參數傳回 - 1。

            //wdinclipboard 有關此常量的詳細内容,請參閱 microsoft office 98 macintosh 版的語言參考幫助。

            //wdincommentpane 如果指定的所選内容或區域位于批注窗格,則傳回 true。

            //wdinendnote 如果指定的所選内容或區域位于頁面視圖的尾注區内,或者位于普通視圖的尾注窗格中,則本參數傳回 true。

            //wdinfootnote 如果指定的所選内容或區域位于頁面視圖的腳注區内,或者位于普通視圖的腳注窗格中,則本參數傳回 true。

            //wdinfootnoteendnotepane 如果指定的所選内容或區域位于頁面視圖的腳注或尾注區内,或者位于普通視圖的腳注或尾注窗格中,則本參數傳回 true。詳細内容,請參閱前面的 wdinfootnote 和 wdinendnote 的說明。

            //wdinheaderfooter 如果指定的所選内容或區域位于頁眉或頁腳窗格中,或者位于頁面視圖的頁眉或頁腳中,則本參數傳回 true。

            //wdinmasterdocument 如果指定的所選内容或區域位于主要文檔中,則本參數傳回 true。

            //wdinwordmail 傳回一個值,該值表明了所選内容或區域的的位置,如下表所示。值 位置

            //0 所選内容或區域不在一條電子郵件消息中。

            //1 所選内容或區域位于正在發送的電子郵件中。

            //2 所選内容或區域位于正在閱讀的電子郵件中。

            //wdmaximumnumberofcolumns 傳回所選内容或區域中任何行的最大表格列數。

            //wdmaximumnumberofrows 傳回指定的所選内容或區域中表格的最大行數。

            //wdnumberofpagesindocument 傳回與所選内容或區域相關聯的文檔的頁數。

            //wdnumlock 如果 num lock 有效,則本參數傳回 true。

            //wdovertype 如果改寫模式有效,則本參數傳回 true。可用 overtype 屬性改變改寫模式的狀态。

            //wdreferenceoftype 傳回一個值,該值表明所選内容相對于腳注、尾注或批注引用的位置,如下表所示。 值 描述

            //— 1 所選内容或區域包含、但不隻限定于腳注、尾注或批注引用中。

            //0 所選内容或區域不在腳注、尾注或批注引用之前。

            //1 所選内容或區域位于腳注引用之前。

            //2 所選内容或區域位于尾注引用之前。

            //3 所選内容或區域位于批注引用之前。

            //wdrevisionmarking 如果修訂功能處于活動狀态,則本參數傳回 true。

            //wdselectionmode 傳回一個值,該值表明目前的標明模式,如下表所示。 值 標明模式

            //0 正常標明

            //1 擴充標明

            //2 列標明

            //wdstartofrangecolumnnumber 傳回所選内容或區域的起點所在的表格的列号。

            //wdstartofrangerownumber 傳回所選内容或區域的起點所在的表格的行号。

            //wdverticalpositionrelativetopage 傳回所選内容或區域的垂直位置,即所選内容的上邊與頁面的上邊之間的距離,以磅為機關。如果所選内容或區域沒有顯示在螢幕上,則本參數傳回 - 1。

            //wdverticalpositionrelativetotextboundary 傳回所選内容或區域相對于周圍最近的正文邊界的上邊的垂直位置,以磅為機關。如果所選内容或區域沒有顯示在螢幕上,則本參數傳回 - 1。

            //wdwithintable 如果所選内容位于一個表格中,則本參數傳回 true。

            //wdzoompercentage 傳回由 percentage 屬性設定的目前的放大百分比。

繼續閱讀