循环遍历 Word 文档并使用 PageBreak 替换字符串

Looping through a Word Document and Replacing a String with PageBreak

我想用实际的 pagebreak 替换字符串“#PAGEBREAK#”的每一次出现。这就是我想出的:

Sub InsertPageBreak()

    Application.ScreenUpdating = False
    With ActiveDocument.Range
        With .Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = "#PAGEBREAK#"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .InsertBreak Type:=wdPageBreak
            .Execute
        End With
    End With
    Application.ScreenUpdating = True

End Sub

实际情况:字符串“#PAGEBREAK#”被替换为空字符串。 .Find 按预期工作,但我收到错误消息:

Method or Object not found

.InsertBreak Type:= wdPageBreak

这里有什么方法可以用在什么地方?

这对你有用:

Sub InsertPageBreak()


    ActiveDocument.Range.Select

        With Selection.Find
                .Text = "#PAGEBREAK#"
                .Execute
        End With

        If Selection.Find.Found Then

            Selection.GoTo What:=wdGoToBookmark, Name:="\Page"
            Selection.MoveRight Unit:=wdCharacter, Count:=1
            Selection.MoveLeft Unit:=wdCharacter, Count:=1
            Selection.InsertBreak Type:=wdPageBreak

        End If
End Sub

如果您想替换所有“#PAGEBREAK#”,请使用以下代码:

Sub InsertPageBreak()


   ActiveDocument.Range.Select

    Do

    With Selection.Find
            .Text = "#PAGEBREAK#"
            .Execute
    End With

        If Selection.Find.Found Then

            Selection.GoTo What:=wdGoToBookmark, Name:="\Page"
            Selection.MoveRight Unit:=wdCharacter, Count:=1
            Selection.MoveLeft Unit:=wdCharacter, Count:=1
            Selection.InsertBreak Type:=wdPageBreak

        Else: Exit Sub

        End If
    Loop


End Sub