使用VBA删除word文件的第二页中的页眉和页脚

时间:2016-09-21 10:29:50

标签: vba excel-vba word-vba excel

我有一个Word文档文件,我想编写一个VBA宏,它在两个现有页面之间的文档中输入一个新页面。 Word文档的页面有一个页眉和一个页脚,它是一个图像文件。

在新页面中,我将添加一个TextBox,用于读取第三页中的内容,图像和空白表。

我希望从新页面中删除页眉和页脚(这将成为整个Word文档中的第二页)。我希望将现有的页眉和页脚保留在文档的所有其他页面上。 所以它会是:

  • 文档的第1页:封面页没有页眉和页脚(已解决)
  • 文档的第2页:没有页眉和页脚(寻找解决方案)
  • 文档的第3页和后页:所有内容都应保留初始页眉和页脚。

这是我的程序代码:

Sub processSecondPage()

Dim imgPathSecondPageBackground As String
Dim secondPageBackground As Shape
Dim tableNew As Table

imgPathSecondPageBackground = "C:\solutionProposals\Pictures\Second_Page_Background_Cables.jpg"

'Moves selection to the 2nd Page
Selection.GoTo what:=wdGoToPage, which:=wdGoToNext, Name:="2"

'inserts a new Page before the 2nd Page
Selection.InsertBreak Type:=wdPageBreak     'wdSectionBreakNextPage
'Selection.InsertBreak Type:=wdSectionBreakNextPage

Selection.GoTo what:=wdGoToPage, which:=wdGoToNext, Name:="2"

'inserts the image on the 2nd page that will look like the background
Set secondPageBackground = 

ActiveDocument.Shapes.AddPicture(FileName:=imgPathSecondPageBackground, LinkToFile:=False, SaveWithDocument:=True, Left:=CentimetersToPoints(0.1), Top:=CentimetersToPoints(-3))
    With secondPageBackground.WrapFormat
    .Type = wdWrapBehind
End With
Selection.GoTo what:=wdGoToPage, which:=wdGoToNext, Name:="2"
Selection.InsertAfter Text:="processing 2nd page"


'enter a table on the second page at a certain position
Set tableNew = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=6, NumColumns:=4, DefaultTableBehavior:=wdWord9TableBehavior)
With tableNew.Rows
    .HorizontalPosition = CentimetersToPoints(0.2)
    .VerticalPosition = CentimetersToPoints(20)
    .Height = 30 'sets the height of the cells
End With

'apply zebra stripes to table cells
tableNew.Cell(2, 1).Shading.BackgroundPatternColorIndex = wdGray25
tableNew.Cell(4, 1).Shading.BackgroundPatternColorIndex = wdGray25
tableNew.Cell(6, 1).Shading.BackgroundPatternColorIndex = wdGray25
tableNew.Cell(2, 2).Shading.BackgroundPatternColorIndex = wdGray25
tableNew.Cell(4, 2).Shading.BackgroundPatternColorIndex = wdGray25
tableNew.Cell(6, 2).Shading.BackgroundPatternColorIndex = wdGray25
tableNew.Cell(2, 3).Shading.BackgroundPatternColorIndex = wdGray25
tableNew.Cell(4, 3).Shading.BackgroundPatternColorIndex = wdGray25
tableNew.Cell(6, 3).Shading.BackgroundPatternColorIndex = wdGray25
tableNew.Cell(2, 4).Shading.BackgroundPatternColorIndex = wdGray25
tableNew.Cell(4, 4).Shading.BackgroundPatternColorIndex = wdGray25
tableNew.Cell(6, 4).Shading.BackgroundPatternColorIndex = wdGray25


'Hide the header and footer on the second page
Selection.Sections(1).Headers(wdHeaderFooterPrimary).LinkToPrevious = False
Selection.Sections(1).Headers(wdHeaderFooterPrimary).Range.Delete
Selection.Sections(1).Footers(wdHeaderFooterPrimary).LinkToPrevious = False
Selection.Sections(1).Footers(wdHeaderFooterPrimary).Range.Delete

End Sub

我能找到的解决方法最接近的匹配是:Insert a new page in word without header,但它删除了文档所有页面上的页眉和页脚。我无法调整它。

如何从第二页删除页眉和页脚,同时将其保留在其他页面上,我该怎么办?

谢谢。

0 个答案:

没有答案
相关问题