简体   繁体   中英

How To Insert Multiple Page Break In Microsoft Word After Specific Line Using VBA Macros?

I have a Word Document with about 100 pages and I want to print it page by page as I copied in in Word from TXT File. So the concept in TXT file page break was a line as shared below...

--------------------------------------------Continue on next page------------------------------------------------

Now I want to do some automation about this Page-Break in word because I can't make it manually. SO I want some VBA Script as MACROS to search this specific line and then add Page-Break after this.

在此处输入图像描述

So Is this possible via VBA as I tried and got no way in Word Toolbar Features and tried to use upper screenshot feature but it is only adding page-break after highlighted line.

Here is the MACRO that I tried to RUn and I wrote it by the help of other queries over Stack Overflow but it's not working...

Option Explicit
Public Sub FindAndAddPageBreak()
    Dim WordApp As Word.Application
    Dim MyWordDocument As Word.Document
    Dim Counter As Long
    Set WordApp = New Word.Application
    Set MyWordDocument = Word.Application.Documents.Open("C:\test.docx")
    For Counter = 1 To ActiveDocument.Sentences.Count
        With MyWordDocument.Sentences(Counter)
            If Left$(.Text, 11) = "--------------------------------------------Continue on next page------------------------------------------------" Then
                Selection.InsertBreak Type:=wdSectionBreakContinuous
            End If
        End With
    Next
End Sub

Alternative Solution: Thanks to macropod for the hint via GUI. Search & Replace is the option but it is giving me only BEFORE not AFTER. SO what about AFTER? 在此处输入图像描述 在此处输入图像描述

All you need is a wildcard Find/Replace with:

Find = Continue on next page[!^13]@^13
Replace = ^&^12

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM