Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #3  
Old 09-03-2018, 12:51 AM
gmayor's Avatar
gmayor gmayor is offline Divide Text Doc into Multiple Parts at .pa Delimiter Windows 10 Divide Text Doc into Multiple Parts at .pa Delimiter Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Maybe something like:


Code:
Sub SplitDoc()
Const strName As String = "Test_" 'the name of the files
Const strPath As String = "C:\Path\" 'the folder to save the files
Dim strDocName As String
Dim oDoc As Document
Dim oRng As Range, oSplit As Range
Dim oNewDoc As Document
Dim strDelim As String: strDelim = ".pa"
Dim Counter As Integer: Counter = 1
Dim strOriginalName As String

    Set oDoc = ActiveDocument
    oDoc.Save
    If oDoc.path = "" Then Beep: GoTo lbl_Exit
    strOriginalName = oDoc.FullName
    Set oRng = oDoc.Range


    Application.ScreenUpdating = False

    With oRng.Find
        Do While .Execute(FindText:=strDelim, MatchWholeWord:=True, MatchCase:=True)
            oRng.Text = ""
            Set oSplit = oRng
            oSplit.Start = ActiveDocument.Range.Start
            Set oNewDoc = Documents.Add(strOriginalName)
            oNewDoc.Range.FormattedText = oSplit.FormattedText
            strDocName = strPath & strName & _
                         LTrim$(Str$(Counter)) & ".docx"
            oNewDoc.SaveAs2 strDocName
            oNewDoc.Close
            Counter = Counter + 1
            oSplit.Text = ""
            oRng.Collapse 0
        Loop
    End With
    oDoc.SaveAs2 strDocName
    oDoc.Close wdDoNotSaveChanges
    Documents.Open strOriginalName
lbl_Exit:
    Set oDoc = Nothing
    Set oNewDoc = Nothing
    Set oRng = Nothing
    Set oSplit = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
 

Tags
macro, page break, split



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to divide a landscape page into 2 parts rtcary Word 1 01-17-2018 12:07 AM
One long string of text with ; as the delimiter Laurie B. Excel 1 02-28-2015 03:02 AM
Divide Text Doc into Multiple Parts at .pa Delimiter Split file using variable delimiter kramer74 Word VBA 7 09-01-2014 12:12 AM
How do I edit text in Quick Parts? TommyVincent Word 4 07-30-2014 01:51 PM
divide page into 4 parts aclark17 Word 4 01-17-2012 09:04 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:50 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft