1

我有以下 VBA 宏,它删除 word 文档的标题,然后打印文档,然后将标题添加回来。标题基本上只是一个图像。

问题是每次执行宏时都会在标题中添加一个换行符,这将在一些执行后将主要部分向下移动。

这是我的代码:

Sub print()
    Dim oSec As Section
    Dim oHead As HeaderFooter

    For Each oSec In ActiveDocument.Sections
        For Each oHead In oSec.Headers
            If oHead.Exists Then oHead.Range.CopyAsPicture
                oHead.Range.Delete
        Next oHead
    Next oSec

    ActivePrinter = "Bullzip PDF Printer"

    Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
        wdPrintDocumentWithMarkup, Copies:=1, Pages:="", PageType:= _
        wdPrintAllPages, Collate:=True, Background:=True, PrintToFile:=False, _
        PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
        PrintZoomPaperHeight:=0

    For Each oSec In ActiveDocument.Sections
        For Each oHead In oSec.Headers
            If oHead.Exists Then oHead.Range.Paste
        Next oHead
    Next oSec

End Sub

有人可以解释为什么每次运行宏时都会添加一行吗?

4

2 回答 2

0

我能想到的最快的想法(可能不是最优雅的)是删除粘贴图像后出现的附加段落。我认为附加段落是最后一段。因此,您的第二个循环将如下所示:

'...beginning of your code here
For Each oSec In ActiveDocument.Sections
    For Each oHead In oSec.Headers
        If oHead.Exists Then oHead.Range.Paste
            'new line to delete additional paragraph
            oHead.Range.Paragraphs(oHead.Range.Paragraphs.Count).Range.Delete
    Next oHead
Next oSec
'...rest of your code here
于 2013-07-30T20:47:17.087 回答
0

我刚刚遇到了与页脚类似的问题。每次我添加一些东西(在我的例子中是文本)时,都会添加一个回车。这是我要解决的问题:

currentFooter = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text
If Right(currentFooter, 1) = vbCr Then
    newFooter = Left(currentFooter, Len(currentFooter) - 1)
End If
'then you make the changes you need to the truncated footer value and apply it back
    ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = newFooter
于 2015-11-13T18:35:12.610 回答