Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-08-2018, 06:14 PM
raywood raywood is offline Divide Text Doc into Multiple Parts at .pa Delimiter Windows 10 Divide Text Doc into Multiple Parts at .pa Delimiter Office 2010 64bit
Novice
Divide Text Doc into Multiple Parts at .pa Delimiter
 
Join Date: Sep 2018
Posts: 10
raywood is on a distinguished road
Default

gmayor -- yes, you're right, silly oversight on my part. Changing the path name fixed that. And this macro has the advantage of not requiring manual approval of each letter.



One remaining issue: in two tests, for the last .pa-delimited document, I get only a blank page. In one, for instance, there are eight pieces of correspondence ending with .pa; each of the first seven get copied into separate .docx files; there is an eighth .docx file, but it is empty; the eighth letter ending with .pa does not get copied into it.

Just in the spirit of trying to learn a bit from this, maybe the counter is incrementing at the wrong time?
Reply With Quote
  #2  
Old 09-08-2018, 08:43 PM
macropod's Avatar
macropod macropod is offline Divide Text Doc into Multiple Parts at .pa Delimiter Windows 7 64bit Divide Text Doc into Multiple Parts at .pa Delimiter Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by raywood View Post
And this macro has the advantage of not requiring manual approval of each letter.
Actually, that was just an oversight, in that I left a MsgBox in the code from testing.

The following version of the previous macro will process a whole folder, so you don't have to manually open each document. As coded, the macro assumes your XyWrite documents have a .txt extension; if not, change the two .txt references in the code to the correct extension.
Code:
Sub SplitDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String
Dim DocSrc As Document, DocTgt As Document, i As Long
Dim Rng As Range, HdFt As HeaderFooter, j As Long
strDocNm = ActiveDocument.FullName
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.txt", vbNormal)
While strFile <> ""
  If strFolder & "\" & strFile <> strDocNm Then
    Set DocSrc = Documents.Open(FileName:=strFolder & "\" & strFile, _
      ConfirmConversions:=False, AddToRecentFiles:=False, Visible:=False)
    With DocSrc
      ' Create, copy & delete a temporary Section break.
      With .Range
        Set Rng = .Characters.First
        With Rng
          .Collapse wdCollapseStart
          .InsertBreak Type:=wdSectionBreakNextPage
          .Start = .Start - 1
          .Copy
          .Delete
        End With
        ' Replace all instances of .pa with the copied Section break
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Text = ".pa"
          .Replacement.Text = "^c"
          .Forward = True
          .Format = False
          .Wrap = wdFindContinue
          .Execute Replace:=wdReplaceAll
        End With
      End With
      ' Process each Section
      j = 0
      For i = 1 To .Sections.Count
        ' Get the whole Section
        Set Rng = .Sections(i).Range
        ' Contract the range to exclude the Section break
        With Rng
          .MoveEnd wdCharacter, -1
          Do While .Characters.Last.Previous.Text = vbCr
            .Characters.Last.Previous.Text = vbNullString
          Loop
          If Len(.Text) > 1 Then
            j = j + 1
            ' Copy the range
            .Copy
            ' Create the output document
            Set DocTgt = Documents.Add(Template:=DocSrc.AttachedTemplate.FullName, Visible:=False)
            With DocTgt
              ' Paste contents into the output document, preserving the formatting
              .Range.PasteAndFormat (wdFormatOriginalFormatting)
              ' Delete trailing paragraph breaks & page breaks at the end
              While .Characters.Last.Previous = vbCr
                .Characters.Last.Previous = vbNullString
              Wend
              ' Replicate the headers & footers
              For Each HdFt In DocSrc.Sections(i).Headers
                .Sections(1).Headers(HdFt.Index).Range.FormattedText = HdFt.Range.FormattedText
              Next
              For Each HdFt In DocSrc.Sections(i).Footers
                .Sections(1).Footers(HdFt.Index).Range.FormattedText = HdFt.Range.FormattedText
              Next
              ' Save & close the output document
              .SaveAs FileName:=Split(DocSrc.FullName, ".txt")(0) & "_" & j & ".docx", _
                FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
              .Close SaveChanges:=False
            End With
          End If
        End With
      Next
      .Close SaveChanges:=False
    End With
  End If
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 09-08-2018, 11:04 PM
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,142
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

Quote:
Originally Posted by raywood View Post
gmayor --
One remaining issue: in two tests, for the last .pa-delimited document, I get only a blank page. In one, for instance, there are eight pieces of correspondence ending with .pa; each of the first seven get copied into separate .docx files; there is an eighth .docx file, but it is empty; the eighth letter ending with .pa does not get copied into it.

Just in the spirit of trying to learn a bit from this, maybe the counter is incrementing at the wrong time?
The blank page is caused by the fact that you have .pa at the end.

That being the case remove the line to save the document before it is closed

Code:
'oDoc.SaveAs2 strDocName
    oDoc.Close wdDoNotSaveChanges
Incidentally this also demonstrated another issue related to that blank page in that there was a missing line in the original code that would have correctly named the last document. This would still have given you a blank page, (the page after the last .pa) but the last true page would have been correct also.
Code:
End With
    strDocName = strPath & strName & _
                 LTrim$(Str$(Counter)) & ".docx"
    oDoc.SaveAs2 strDocName
    oDoc.Close wdDoNotSaveChanges
    Documents.Open strOriginalName
__________________
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
Reply

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 02:00 PM.


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