用VBA删除标题的最后一段标记

时间:2019-01-29 18:49:34

标签: vba ms-word header

我要删除标题的最后一行。我目前在标题中有两行:第一行是我复制粘贴,第二行是在复制粘贴之前(复制粘贴的起点)。

我尝试过类似的事情:

`Selection.Sections(1).Headers(1).Range.MoveEnd Unit:=wdCharacter, Count:=-1`

`Documents.Open (strFilename)
Selection.Sections(1).Headers(1).Range.Copy
ThisDocument.Activate
Selection.Sections(1).Headers(1).Range.Paste
Documents(strFilename).Close (0)`

期望只有一行,我从另一文档中复制粘贴 只需一行即可在标头中获得一点空间。

好的,这是一张图片,可以更好地理解我的问题:

Header with or without extra paragraph mark 希望有帮助!

2 个答案:

答案 0 :(得分:0)

我不确定我是否理解您的意思,但是我认为您正在寻找的是以下内容,请尝试

`Selection.Sections(1).Headers(1).Range.MoveEnd Unit:=wdCharacter, Count:=-1`

`Documents.Open (strFilename)
    Dim strText() As String
    Dim MyCell As String

    ThisDocument.Activate
    MyCell = Selection.Sections(1).Headers(1).Range.Text
    strText = Split(MyCell , vbCrLf)
    Selection.Sections(1).Headers(1).Range.Text=strText(0)
    Documents(strFilename).Close (0)


`

答案 1 :(得分:0)

假定带有要复制的标题的文档处于活动状态,并且该宏位于该文档中:

Sub Demo()
Application.ScreenUpdating = False
Dim DocSrc As Document, DocTgt As Document
Set DocSrc = ActiveDocument
Set DocTgt = ActiveWindow.Next.Document
With DocTgt.Sections(1).Headers(1).Range
  .FormattedText = DocSrc.Sections(1).Headers(1).Range.FormattedText
  .Characters.Last.Previous = vbNullString
End With
Application.ScreenUpdating = True
End Sub