1

Word文書のヘッダーを削除し、文書を印刷してからヘッダーを追加する次のVBAマクロがあります。ヘッダーは基本的に画像のみです。

問題は、マクロが実行されるたびにヘッダーに改行が追加され、いくつかの実行後にメイン セクションが下に移動することです。

ここに私が持っているコードがあります:

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

フッターで同様の問題に遭遇しました。何か (私の場合はテキスト) を追加するたびに、キャリッジ リターンが追加されます。これが私が対処するためにしたことです:

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 に答える
0

私が考えることができる最も簡単なアイデア (おそらく最もエレガントではない) は、画像の貼り付け後に表示される追加の段落を削除することです。追加の段落が最後の段落であると想定しました。したがって、2 番目のループは次のようになります。

'...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 に答える