You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何阻止MS Word中信封打印时带出页眉?

问题:阻止Word信封打印区段打印页眉

我的任务是批量更新大量MS Word的doc和docx文件。原有文件通过插入链接Word文档的OLE对象,让所有文件首页显示机构信头,修改源信头文件后打开文档会自动更新。

客户联合供应商重新设计了新信头文件,该文件包含第二页及后续页面需使用的信头,因此不能再在文档正文插入OLE对象,必须改用页眉功能。

我参考他人代码编写了处理流程:

  • 清理现有文件中的OLE对象形状和所有页眉
  • 设置文档为「不同首页页眉」模式
  • 将新的第一页信头文档插入首页页眉并置于文字下方
  • 为后续页面设置主页眉

该流程需保存关闭文档才能生效,目前已正常运行。但现在遇到问题:许多文件设有信封打印区段,信封打印时会连带打印主页眉,而我不能删除主页眉(因为后续页面需要)。请问如何阻止信封打印页眉?


现有处理代码

Sub moveLetterheadToHeader()
'Created: 1/4/2025  By: Mike Devlin
'Need a routine to move the letterhead shape to the documents header
'First delete the shape, then re-add it to the document's header
'Then try and add the second page letterhead to the header for pages that are not the first page
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oShape As Word.Shape
Dim oShape2 As Word.Shape
Dim oShape3 As Word.Shape
Dim strPathAndFile As String

strPathAndFile = ""
'    strPathAndFile = "C:\Users\mdevlin\Documents\LH\Appeal Letter_NoPage2.doc"
strPathAndFile = "C:\Users\mdevlin\Documents\LH\Appeal Letter_Pg2.doc"
'    strPathAndFile = "C:\Users\mdevlin\Documents\LH\Appeal Letter_Pg2Env.doc"

Set oWord = New Word.Application

'******************************

'Get rid of any OLE Object shapes
'open the file with Word
Set oDoc = oWord.Documents.Open(strPathAndFile)

'Find the shape and then delete it
For Each oShape In oDoc.Shapes
    If oShape.Type = msoLinkedOLEObject Then
        oShape.Delete
        Exit For
    End If
Next oShape

oDoc.Save

oDoc.Close False

'*************************

'Get rid of any existing headers or footers
'and set up the dioc file to have a different first page header
Set oDoc = oWord.Documents.Open(strPathAndFile)

'Make sure we are viewing the file in print mode
If oDoc.ActiveWindow.ActivePane.View.Type = wdNormalView Or oDoc.ActiveWindow.ActivePane.View.Type = wdOutlineView Then
    oDoc.ActiveWindow.ActivePane.View.Type = wdPrintView
End If

'When I checked the file, somehow the old letterhead file had been linked into the primary header
'So I need to make sure there is no header in the primary header before adding the new page2 header
Dim Sctn As Word.Section
Dim HdFt As Word.HeaderFooter

For Each Sctn In oDoc.Sections
  For Each HdFt In Sctn.Headers
    If HdFt.Exists Then
        HdFt.Range.Text = vbNullString
    End If
  Next
Next

oDoc.PageSetup.HeaderDistance = 0
oDoc.PageSetup.FooterDistance = 0
oDoc.PageSetup.OddAndEvenPagesHeaderFooter = False
'When set to false, there is only the primary header and footer
'    oDoc.PageSetup.DifferentFirstPageHeaderFooter = False
'When set to true, the primary header and footer are not available and the first page and primary header and footer are available
oDoc.PageSetup.DifferentFirstPageHeaderFooter = True

oDoc.Save

oDoc.Close False
 '***************************

'Set up the first page header
Set oDoc = oWord.Documents.Open(strPathAndFile)

oDoc.Sections(1).Headers(wdHeaderFooterFirstPage).Range.InlineShapes.AddOLEObject ClassType:="Word.Document.12", _
    FILENAME:="S:\apps\Templates\Letterhead_pg1.docx", _
    LinkToFile:=True, _
    DisplayAsIcon:=False
Set oShape2 = oDoc.Sections(1).Headers(wdHeaderFooterFirstPage).Range.InlineShapes(1).ConvertToShape

oShape2.Top = 0
oShape2.Left = 0

oShape2.WrapFormat.Type = wdWrapBehind

'Gonna try saving and closing the doc, then re-opening the doc and setting the primary header after the re-open since that seemed to work when I did it manually

'Close the header
If oDoc.ActiveWindow.View.SplitSpecial <> wdPaneNone Then
    oDoc.ActiveWindow.Panes(2).Close
End If

oDoc.Save

oDoc.Close False



'****************************

'Set up the primary header which will be used for all subsequent pages/sections
Set oDoc = oWord.Documents.Open(strPathAndFile)

'Make sure we are viewing the file in print mode
If oDoc.ActiveWindow.ActivePane.View.Type = wdNormalView Or oDoc.ActiveWindow.ActivePane.View.Type = wdOutlineView Then
    oDoc.ActiveWindow.ActivePane.View.Type = wdPrintView
End If

oDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.InlineShapes.AddOLEObject ClassType:="Word.Document.12", _
    FILENAME:="S:\apps\Templates\Letterhead_pg2.docx", _
    LinkToFile:=True, _
    DisplayAsIcon:=False
Set oShape3 = oDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.InlineShapes(1).ConvertToShape

oShape3.Top = 0
oShape3.Left = 0

oShape3.WrapFormat.Type = wdWrapBehind

'Close the header
If oDoc.ActiveWindow.View.SplitSpecial <> wdPaneNone Then
    oDoc.ActiveWindow.Panes(2).Close
End If

oDoc.Save

oDoc.Close False

'************************

Set oDoc = Nothing

Set oShape = Nothing
Set oShape2 = Nothing
Set oShape3 = Nothing

If IsAppRunning("Word.Application") = True Then
    'Kill Word
    oWord.Quit
End If

Set oWord = Nothing
End Sub

内容的提问来源于stack exchange,提问作者mdevman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 13:54:50