設(shè)置word橫向頁眉頁腳的宏腳本 做長文檔的時候難免會因?yàn)楸砀窕蛘邎D片等超長的內(nèi)容,,我們往往是利用分節(jié)符后,,把頁面設(shè)置成橫向以方便布局,。但這樣一來在設(shè)置頁眉和頁腳時word卻沒有相應(yīng)的把頁眉與頁腳相應(yīng)的進(jìn)行調(diào)整,,導(dǎo)致打印出來后,,橫向頁面的頁眉與頁腳位于紙的長邊,,與縱向頁不一致,。因此做了這個設(shè)置橫向頁眉與頁腳的宏腳本 ,。
原理就是在頁眉頁腳視圖中,,利用新加兩個文本框,一個位于橫向紙的右邊作為新的頁眉,,一個位于紙的左邊作為新的頁腳,。然后調(diào)整文本框大小與位置,使其與縱向紙的頁眉頁腳位置一致,。最后把文本框的文字內(nèi)容更改一下文字方向即可使之打印裝訂后與縱向紙一致,。 此腳本是針對A4紙?jiān)O(shè)定的,如要更改紙張需要對文本框位置與大小做相應(yīng)調(diào)整,。由于新加了一個窗口用戶對新的頁眉頁腳進(jìn)行簡單設(shè)置,,所以宏里包含一個自定義窗口。通過窗口的按鈕事件運(yùn)行宏腳本,。主要內(nèi)容如下: Private Sub CommandButton1_Click() 代碼中的txtYM為頁眉文字內(nèi)容的文本框控件,cbYMDQ與cbYJDQ為兩個控制頁眉與頁腳對齊方式的兩個下拉控件,,cbYeMeiXHX與cbDBX為設(shè)置頁眉是否有下劃線既頁腳是否有頂邊線的復(fù)選框,。'頁眉 If Trim(txtYM.Text) <> "" Then '檢查是否當(dāng)前為頁眉頁腳視圖 If ActiveWindow.View.SplitSpecial <> wdPaneNone Then ActiveWindow.Panes(2).Close End If If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _ ActivePane.View.Type = wdOutlineView Then ActiveWindow.ActivePane.View.Type = wdPrintView End If ' '去除鏈接到前一節(jié) ' ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter ' Selection.HeaderFooter.LinkToPrevious = False ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader ' Selection.HeaderFooter.LinkToPrevious = False '插入頁眉文本框 Selection.HeaderFooter.Shapes.AddTextbox(msoTextOrientationHorizontal, _ 783.15, 85.05, 35, 453.6).Select Selection.ShapeRange.TextFrame.TextRange.Select Selection.Collapse Selection.ShapeRange.Select Selection.ShapeRange.Fill.Visible = msoFalse Selection.ShapeRange.Fill.Solid Selection.ShapeRange.Fill.Transparency = 0# Selection.ShapeRange.Line.Weight = 0 Selection.ShapeRange.Line.DashStyle = msoLineSolid Selection.ShapeRange.Line.Style = msoLineSingle Selection.ShapeRange.Line.Transparency = 0# Selection.ShapeRange.Line.Visible = msoFalse Selection.ShapeRange.LockAspectRatio = msoFalse Selection.ShapeRange.Height = CentimetersToPoints(14.66) '設(shè)置文本框高度 Selection.ShapeRange.Width = 15 '設(shè)置文本框?qū)挾?/SPAN> Selection.ShapeRange.Left = 0 '設(shè)置文本框左邊距 Selection.ShapeRange.Top = 85# '設(shè)置文本框頂邊距 Selection.ShapeRange.TextFrame.MarginLeft = 0 Selection.ShapeRange.TextFrame.MarginRight = 0 Selection.ShapeRange.TextFrame.MarginTop = 0 Selection.ShapeRange.TextFrame.MarginBottom = 0 Selection.ShapeRange.RelativeHorizontalPosition = _ wdRelativeHorizontalPositionColumn Selection.ShapeRange.RelativeVerticalPosition = _ wdRelativeVerticalPositionParagraph Selection.ShapeRange.Left = CentimetersToPoints(24.8) '設(shè)置文本框左邊相對位置(厘米轉(zhuǎn)為磅) Selection.ShapeRange.Top = CentimetersToPoints(1.7) '設(shè)置文本框頂邊相對位置 Selection.ShapeRange.LockAnchor = False Selection.ShapeRange.LayoutInCell = True Selection.ShapeRange.WrapFormat.AllowOverlap = True Selection.ShapeRange.WrapFormat.Side = wdWrapBoth Selection.ShapeRange.WrapFormat.DistanceTop = CentimetersToPoints(0) Selection.ShapeRange.WrapFormat.DistanceBottom = CentimetersToPoints(0) Selection.ShapeRange.WrapFormat.DistanceLeft = CentimetersToPoints(0) Selection.ShapeRange.WrapFormat.DistanceRight = CentimetersToPoints(0) Selection.ShapeRange.WrapFormat.Type = 3 Selection.ShapeRange.ZOrder 4 Selection.ShapeRange.TextFrame.AutoSize = False Selection.ShapeRange.TextFrame.WordWrap = True Selection.ShapeRange.ScaleWidth 1.67, msoFalse, msoScaleFromTopLeft '文本框?qū)挾确糯?.67倍 Selection.ShapeRange.TextFrame.TextRange.Select '選中文本框內(nèi)容 Selection.Collapse Selection.Orientation = wdTextOrientationVerticalFarEast '頁眉文字內(nèi)容 Selection.TypeText Text:=txtYM.Text With Selection.ParagraphFormat .Borders(wdBorderLeft).LineStyle = wdLineStyleNone .Borders(wdBorderRight).LineStyle = wdLineStyleNone .Borders(wdBorderTop).LineStyle = wdLineStyleNone If cbYeMeiXHX.Value Then With .Borders(wdBorderBottom) .LineStyle = wdLineStyleSingle '設(shè)置下橫線 .LineWidth = wdLineWidth050pt '設(shè)置橫線寬 .Color = wdColorAutomatic End With Else .Borders(wdBorderBottom).LineStyle = wdLineStyleNone End If With .Borders .DistanceFromTop = 1 .DistanceFromLeft = 4 .DistanceFromBottom = 1 .DistanceFromRight = 4 .Shadow = False End With Selection.Orientation = wdTextOrientationDownward '更改頁眉文字方向 End With With Options .DefaultBorderLineStyle = wdLineStyleSingle .DefaultBorderLineWidth = wdLineWidth050pt .DefaultBorderColor = wdColorAutomatic End With With Selection.ParagraphFormat '設(shè)置段落格式 .LeftIndent = CentimetersToPoints(0) .RightIndent = CentimetersToPoints(0) .SpaceBefore = 5 .SpaceBeforeAuto = True .SpaceAfter = 5 .SpaceAfterAuto = True .LineSpacingRule = wdLineSpaceSingle .Alignment = cbYMDQ.ListIndex '設(shè)置對齊 .WidowControl = False .KeepWithNext = False .KeepTogether = False .PageBreakBefore = False .NoLineNumber = False .Hyphenation = True .FirstLineIndent = CentimetersToPoints(0) .OutlineLevel = wdOutlineLevelBodyText .CharacterUnitLeftIndent = 0 .CharacterUnitRightIndent = 0 .CharacterUnitFirstLineIndent = 0 .LineUnitBefore = 0 .LineUnitAfter = 0 .AutoAdjustRightIndent = True .DisableLineHeightGrid = False .FarEastLineBreakControl = True .WordWrap = True .HangingPunctuation = True .HalfWidthPunctuationOnTopOfLine = False .AddSpaceBetweenFarEastAndAlpha = True .AddSpaceBetweenFarEastAndDigit = True .BaseLineAlignment = wdBaselineAlignAuto End With End If If cbYeMa.Value Or cbDBX.Value Then '設(shè)置頁腳 If ActiveWindow.View.SplitSpecial <> wdPaneNone Then ActiveWindow.Panes(2).Close End If If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _ ActivePane.View.Type = wdOutlineView Then ActiveWindow.ActivePane.View.Type = wdPrintView End If ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter Selection.ShapeRange.Flip msoFlipHorizontal Selection.HeaderFooter.Shapes.AddTextbox(msoTextOrientationHorizontal, _ 19.1, 85.05, 37.3, 453.6).Select Selection.ShapeRange.TextFrame.TextRange.Select Selection.Collapse Selection.ShapeRange.Select Selection.ShapeRange.Fill.Visible = msoFalse Selection.ShapeRange.Fill.Solid Selection.ShapeRange.Fill.Transparency = 0# Selection.ShapeRange.Line.Weight = 0 Selection.ShapeRange.Line.DashStyle = msoLineSolid Selection.ShapeRange.Line.Style = msoLineSingle Selection.ShapeRange.Line.Transparency = 0# Selection.ShapeRange.Line.Visible = msoFalse Selection.ShapeRange.LockAspectRatio = msoFalse Selection.ShapeRange.Height = CentimetersToPoints(14.66) Selection.ShapeRange.Width = 25 Selection.ShapeRange.Left = 300 Selection.ShapeRange.Top = 85# Selection.ShapeRange.TextFrame.MarginLeft = 0 Selection.ShapeRange.TextFrame.MarginRight = 0 Selection.ShapeRange.TextFrame.MarginTop = 0 Selection.ShapeRange.TextFrame.MarginBottom = 0 Selection.ShapeRange.RelativeHorizontalPosition = _ wdRelativeHorizontalPositionColumn Selection.ShapeRange.RelativeVerticalPosition = _ wdRelativeVerticalPositionParagraph Selection.ShapeRange.Left = CentimetersToPoints(-1.2) Selection.ShapeRange.Top = CentimetersToPoints(1.7) Selection.ShapeRange.LockAnchor = False Selection.ShapeRange.LayoutInCell = True Selection.ShapeRange.WrapFormat.AllowOverlap = True Selection.ShapeRange.WrapFormat.Side = wdWrapBoth Selection.ShapeRange.WrapFormat.DistanceTop = CentimetersToPoints(0) Selection.ShapeRange.WrapFormat.DistanceBottom = CentimetersToPoints(0) Selection.ShapeRange.WrapFormat.DistanceLeft = CentimetersToPoints(0.32) Selection.ShapeRange.WrapFormat.DistanceRight = CentimetersToPoints(0.32) Selection.ShapeRange.WrapFormat.Type = 3 Selection.ShapeRange.ZOrder 4 Selection.ShapeRange.TextFrame.AutoSize = False Selection.ShapeRange.TextFrame.WordWrap = True Selection.ShapeRange.TextFrame.TextRange.Select Selection.Collapse Selection.Orientation = wdTextOrientationVerticalFarEast Selection.WholeStory '全選 If cbYeMa.Value Then ' Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage '插入頁碼域 Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _ "PAGE", PreserveFormatting:=True '按全文設(shè)置的頁碼格式,更改可在page域后加開關(guān) Selection.WholeStory End If Selection.Orientation = wdTextOrientationDownward ActiveWindow.ActivePane.VerticalPercentScrolled = 0 With Selection.ParagraphFormat .Borders(wdBorderLeft).LineStyle = wdLineStyleNone .Borders(wdBorderRight).LineStyle = wdLineStyleNone .Borders(wdBorderBottom).LineStyle = wdLineStyleNone If cbDBX.Value Then With .Borders(wdBorderTop) '設(shè)置頂橫線 .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = wdColorAutomatic End With Else .Borders(wdBorderTop).LineStyle = wdLineStyleNone End If With .Borders .DistanceFromTop = 1 .DistanceFromLeft = 4 .DistanceFromBottom = 1 .DistanceFromRight = 4 .Shadow = False End With End With With Selection.ParagraphFormat .LeftIndent = CentimetersToPoints(0) .RightIndent = CentimetersToPoints(0) .SpaceBefore = 5 .SpaceBeforeAuto = True .SpaceAfter = 5 .SpaceAfterAuto = True .LineSpacingRule = wdLineSpaceSingle .Alignment = cbYJDQ.ListIndex .WidowControl = False .KeepWithNext = False .KeepTogether = False .PageBreakBefore = False .NoLineNumber = False .Hyphenation = True .FirstLineIndent = CentimetersToPoints(0) .OutlineLevel = wdOutlineLevelBodyText .CharacterUnitLeftIndent = 0 .CharacterUnitRightIndent = 0 .CharacterUnitFirstLineIndent = 0 .LineUnitBefore = 0 .LineUnitAfter = 0 .AutoAdjustRightIndent = True .DisableLineHeightGrid = False .FarEastLineBreakControl = True .WordWrap = True .HangingPunctuation = True .HalfWidthPunctuationOnTopOfLine = False .AddSpaceBetweenFarEastAndAlpha = True .AddSpaceBetweenFarEastAndDigit = True .BaseLineAlignment = wdBaselineAlignAuto End With End If '回到普通視圖 ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument MsgBox "橫向頁眉頁腳設(shè)置完畢,!", vbInformation + vbOKOnly, "提示" End Sub 頁腳處頁碼的格式采用域的方式插入,此處為標(biāo)準(zhǔn)的方式,,如要換成別的樣式可以在PAGE域后面加相應(yīng)的開關(guān),。 以下為設(shè)置窗體初始代碼,用于在兩個下柆框里填充數(shù)據(jù): Private Sub UserForm_Initialize() With cbYMDQ .AddItem "左對齊", 0 .AddItem "居中對齊", 1 .AddItem "右對齊", 2 .AddItem "兩端對齊", 3 .AddItem "分散對齊", 4 .ListIndex = 1 End With With cbYJDQ .AddItem "左對齊", 0 .AddItem "居中對齊", 1 .AddItem "右對齊", 2 .AddItem "兩端對齊", 3 .AddItem "分散對齊", 4 .ListIndex = 1 End With End Sub |
|