wpsword办公技巧页码(WPS加载项为当前节插入页码)
- 办公技巧
- 2023-08-08 18:39:42
- 0
案例需求:
在文档的当前节左边插入页码,当前节页面横向,页码编码需要随前节,且奇偶页要不同
案例可能遇到的坑:
1. 首页不同,每一个节都会有首页不同属性,需要确保当前节的首页不同是否有设置;2. 当前节原有页码影响,如果原来有页码,那么会出现无法添加页码成功,所以需要清空当前节页码;3. shape的宽度和高度以及页面中的位置,宽高不够可能会出现页码显示不全;
代码流程
1. 获取并进入当前节2. 清空当前节页码3. 设置前后节页眉页脚不同前节4. 设置首页不同和页面横向,页面宽高及页边距也可以在这里设置5. 为当前节插入页码,并调整页码位置6. 设置奇偶页不同7. 为偶数页插入页码,并调整页码位置8. 设置页码随前节
代码实现
/** * g2055 Macro * 宏由 jsdkkj 录制,时间: 2022/06/06 */function 横排表(){var selectionIndex=Selection.Sections.Item(1).Index;ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader;//进入当前页页眉var footer=ActiveDocument.Sections.Item(selectionIndex).Footers.Item(1)footer.Range.Text=""var footer1=ActiveDocument.Sections.Item(selectionIndex).Footers.Item(3);footer1.Range.Text=""if(selectionIndex!=1){//设置当前节不同前节Selection.HeaderFooter.LinkToPrevious=false;ActiveDocument.Sections.Item(selectionIndex).Headers.Item(1).LinkToPrevious=false;ActiveDocument.Sections.Item(selectionIndex).Footers.Item(1).LinkToPrevious=false;ActiveDocument.Sections.Item(selectionIndex).Headers.Item(2).LinkToPrevious=false;ActiveDocument.Sections.Item(selectionIndex).Footers.Item(2).LinkToPrevious=false;ActiveDocument.Sections.Item(selectionIndex).Headers.Item(3).LinkToPrevious=false;ActiveDocument.Sections.Item(selectionIndex).Footers.Item(3).LinkToPrevious=false;}if(selectionIndex!=ActiveDocument.Sections.Count){//设置下一节不同前节ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument;Selection.GoTo(0,1,selectionIndex 1);//光标移动到下一节ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader;Selection.HeaderFooter?Selection.HeaderFooter.LinkToPrevious=false:"";ActiveDocument.Sections.Item(selectionIndex 1).Headers.Item(1).LinkToPrevious=false;ActiveDocument.Sections.Item(selectionIndex 1).Footers.Item(1).LinkToPrevious=false;ActiveDocument.Sections.Item(selectionIndex 1).Headers.Item(2).LinkToPrevious=false;ActiveDocument.Sections.Item(selectionIndex 1).Footers.Item(2).LinkToPrevious=false;ActiveDocument.Sections.Item(selectionIndex 1).Headers.Item(3).LinkToPrevious=false;ActiveDocument.Sections.Item(selectionIndex 1).Footers.Item(3).LinkToPrevious=false;ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument;Selection.GoTo(0,1,selectionIndex);//光标回到当前节ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader;}ActiveDocument.Sections.Item(selectionIndex).PageSetup.DifferentFirstPageHeaderFooter=0;//当前节首页不同ActiveDocument.Sections.Item(selectionIndex).PageSetup.Orientation = wdOrientLandscape;//页面横向//为当前节奇数页添加页码var shape=ActiveDocument.Sections.Item(selectionIndex).Headers.Item(1).Shapes.AddTextbox(msoTextOrientationHorizontal, 92.150002, 774.299988, 78.750000, 32.250000, ActiveDocument.Sections.Item(selectionIndex).Headers.Item(1).Range);shape.TextFrame.Orientation = msoTextOrientationVertical;ActiveWindow.ActivePane.Activate();shape.Select();(obj=>{obj.Range.LanguageID = wdSimplifiedChinese;obj.Fields.Add(Selection.Range, wdFieldPage, "", true);})(Selection); //页码前后加横线Selection.MoveLeft(wdCharacter, 1, wdMove);Selection.Font.Name = "宋体";Selection.Font.Size = 10.500000;Selection.TypeText("— ");Selection.MoveRight(wdCharacter, 1, wdMove);Selection.Font.Name = "宋体";Selection.Font.Size = 10.500000;Selection.TypeText(" —");Selection.ParagraphFormat.Alignment=1;Selection.ParagraphFormat.FirstLineIndent=0;shape.Rotation = 90;shape.Select(false);shape.Line.Visible = msoFalse;shape.RelativeHorizontalPosition=wdRelativeHorizontalPositionPageshape.RelativeVerticalPosition=wdRelativeVerticalPositionPageshape.Left = 9;shape.Top = 110;//设置奇偶页不同ActiveDocument.Sections.Item(selectionIndex).PageSetup.OddAndEvenPagesHeaderFooter=1//为当前节偶数页添加页码var shape1=ActiveDocument.Sections.Item(selectionIndex).Headers.Item(1).Shapes.AddTextbox(msoTextOrientationHorizontal, 92.150002, 774.299988, 78.750000, 32.250000, ActiveDocument.Sections.Item(selectionIndex).Headers.Item(3).Range);shape1.TextFrame.Orientation = msoTextOrientationVertical;ActiveWindow.ActivePane.Activate();shape1.Select();(obj=>{obj.Range.LanguageID = wdSimplifiedChinese;obj.Fields.Add(Selection.Range, wdFieldPage, "", true);})(Selection); //页码前后加横线Selection.MoveLeft(wdCharacter, 1, wdMove);Selection.Font.Name = "宋体";Selection.Font.Size = 10.500000;Selection.TypeText("— ");Selection.MoveRight(wdCharacter, 1, wdMove);Selection.Font.Name = "宋体";Selection.Font.Size = 10.500000;Selection.TypeText(" —");Selection.ParagraphFormat.Alignment=1;Selection.ParagraphFormat.FirstLineIndent=0;shape1.Rotation = 90;shape1.Select(false);shape1.Line.Visible = msoFalse;shape1.RelativeHorizontalPosition=wdRelativeHorizontalPositionPageshape1.RelativeVerticalPosition=wdRelativeVerticalPositionPageshape1.Left = 9;shape1.Top = 500;(obj=>{//单独为本节设置页码obj.RestartNumberingAtSection = true;obj.StartingNumber = Selection.Information(wdActiveEndPageNumber);})(ActiveDocument.Sections.Item(selectionIndex).Headers.Item(wdHeaderFooterPrimary).PageNumbers);//页码续前节//(obj=>{//obj.RestartNumberingAtSection = false;//obj.StartingNumber = 1;//})(ActiveDocument.Sections.Item(selectionIndex).Headers.Item(wdHeaderFooterPrimary).PageNumbers);//回到主文档ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument;Selection.SetRange(0, 0);}
本文由 京廊文化根据互联网搜索查询后整理发布,旨在分享有价值的内容,本站为非营利性网站,不参与任何商业性质行为,文章如有侵权请联系删除,部分文章如未署名作者来源请联系我们及时备注,感谢您的支持。
本文链接: /bangong/11829.html