通过vba复制wordheader会添加换行符
我有下面的VBA宏,它删除word文档的标题,然后打印文档,然后重新添加标题。标题基本上只是一个图像 问题是每次执行宏时,都会在标头中添加一个换行符,这会在执行一些宏后将主节向下移动 以下是我的代码:通过vba复制wordheader会添加换行符,vba,ms-word,Vba,Ms Word,我有下面的VBA宏,它删除word文档的标题,然后打印文档,然后重新添加标题。标题基本上只是一个图像 问题是每次执行宏时,都会在标头中添加一个换行符,这会在执行一些宏后将主节向下移动 以下是我的代码: Sub print() Dim oSec As Section Dim oHead As HeaderFooter For Each oSec In ActiveDocument.Sections For Each oHead In oSec.Header
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
有人能解释一下为什么每次运行宏时都会增加一行吗?我能想到的最快的办法可能不是最优雅的办法是删除粘贴图像后出现的附加段落。我假定附加的段落是最后一段。因此,第二个循环将如下所示:
'...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
我只是在页脚上遇到了类似的问题。在我的例子中,每次我添加文本时,都会添加一个回车。以下是我为解决这个问题所做的:
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