谈word2003编程
 

 
 
近期因为项目需要,写了许多word2003编程的东东.有时候遇到难题想查sdk说明,很难找到中文解释,对于e文不好的我来说,简直是天书.想必很多人多有感慨.
    下面列出内容是一些常用的内容说明,希望对大家有帮助.
    那就开始吧注意,下文的WAPP是我定义的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

  • 发表评论
  • 用户:  验证码: 点击更换  

设为首页 | 联系我们    小雨科技有限公司版权所有 Copyright © 2008- 2009  备案/许可证编号为:黔ICP备17001234号-1

  
 

Keywords: 考试系统 考试系统
Powered by PageAdmin CMS