用vba插入奇偶页不同的页面问题
时间:2011-04-15
来源:互联网
假如一个文档,要插入奇偶页不同的页码,并且用vba实现,我录制了以下代码,如果页面多于2页,且光标定位于奇数页,这个代码好是可以运行,如果光标定位在偶数页就出错,或者只有一页时代码就无法运行,你可能会说等大于1页再运行这个代码,但我怕忘记加页码,喜欢新建文档后就加入页面,而且这个贴主要是想请教下,只有一页的情况下,怎么用vba插入奇偶页不同的页码,望高人指点
Sub Macro3()
With ActiveDocument.PageSetup
.OddAndEvenPagesHeaderFooter = True
End With
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 = wdSeekCurrentPageHeader
If Selection.HeaderFooter.IsHeader = True Then
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Else
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
End If
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
With Selection.ParagraphFormat
.CharacterUnitRightIndent = 1
End With
Selection.Font.Name = "Times New Roman"
Selection.Font.Size = 14
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
ActiveWindow.ActivePane.View.NextHeaderFooter
With Selection.ParagraphFormat
.CharacterUnitLeftIndent = 1
End With
Selection.Font.Name = "Times New Roman"
Selection.Font.Size = 14
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub
Sub Macro3()
With ActiveDocument.PageSetup
.OddAndEvenPagesHeaderFooter = True
End With
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 = wdSeekCurrentPageHeader
If Selection.HeaderFooter.IsHeader = True Then
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Else
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
End If
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
With Selection.ParagraphFormat
.CharacterUnitRightIndent = 1
End With
Selection.Font.Name = "Times New Roman"
Selection.Font.Size = 14
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
ActiveWindow.ActivePane.View.NextHeaderFooter
With Selection.ParagraphFormat
.CharacterUnitLeftIndent = 1
End With
Selection.Font.Name = "Times New Roman"
Selection.Font.Size = 14
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub
作者: tongyi 发布时间: 2011-04-15
可试试如下代码:
复制内容到剪贴板
Application.ScreenUpdating = False
With ActiveDocument.Sections(1)
.PageSetup.OddAndEvenPagesHeaderFooter = True
With .Footers(wdHeaderFooterPrimary)
With .PageNumbers
.Add PageNumberAlignment:=wdAlignPageNumberRight
.NumberStyle = wdPageNumberStyleNumberInDash
End With
With .Range.Frames(1)
.HorizontalPosition = wdFrameRight
With .Range.ParagraphFormat
.Alignment = wdAlignParagraphRight
.CharacterUnitRightIndent = 1
End With
End With
End With
With .Footers(wdHeaderFooterEvenPages).Range.Frames(1)
.HorizontalPosition = wdFrameLeft
With .Range.ParagraphFormat
.Alignment = wdAlignParagraphLeft
.CharacterUnitLeftIndent = 1
End With
End With
End With
Application.ScreenUpdating = True
End Sub
代码:
Sub test()Application.ScreenUpdating = False
With ActiveDocument.Sections(1)
.PageSetup.OddAndEvenPagesHeaderFooter = True
With .Footers(wdHeaderFooterPrimary)
With .PageNumbers
.Add PageNumberAlignment:=wdAlignPageNumberRight
.NumberStyle = wdPageNumberStyleNumberInDash
End With
With .Range.Frames(1)
.HorizontalPosition = wdFrameRight
With .Range.ParagraphFormat
.Alignment = wdAlignParagraphRight
.CharacterUnitRightIndent = 1
End With
End With
End With
With .Footers(wdHeaderFooterEvenPages).Range.Frames(1)
.HorizontalPosition = wdFrameLeft
With .Range.ParagraphFormat
.Alignment = wdAlignParagraphLeft
.CharacterUnitLeftIndent = 1
End With
End With
End With
Application.ScreenUpdating = True
End Sub
作者: sylun 发布时间: 2011-04-16
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28