![]() |
|
#1
|
|||
|
|||
![]()
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? |
#2
|
||||
|
||||
![]() Quote:
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] |
#3
|
||||
|
||||
![]() Quote:
That being the case remove the line to save the document before it is closed Code:
'oDoc.SaveAs2 strDocName oDoc.Close wdDoNotSaveChanges 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 |
![]() |
Tags |
macro, page break, split |
|
![]() |
||||
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 |
![]() |
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 |