VSTO WORD C# 备忘

这篇博客介绍了如何使用C# VSTO进行Word文档操作,包括合并与分离单元格、设置文档页面布局、调整光标位置。通过示例代码展示了如何修改页边距、页面方向、插入分页和换行,以及光标在行、段落和表格间的移动方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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          &n

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值