PDA

View Full Version : UNMERGE CODE MODIFY



stevembe
03-17-2015, 01:23 AM
I am hoping somebody can help modify a bit of script I have used in the past but it is now not funtioning correctly. Basically the following unmerged word letters and saved them to a specified path and named them automatically. However, the format of the document I am using has changed. There are now 4 paragraph breaks then a table and after that a Section Break (Continuous) and at the bottom of the page a Section Break (Next Page) which is where I want the unmerge to stop and save



Sub Demo()
Application.ScreenUpdating = False
Dim i As Long, StrTxt As String, Rng As Range, Doc As Document, HdFt As HeaderFooter
With ActiveDocument
'Process each Section
For i = 2 To .Sections.Count - 2
With .Sections(i)
'Get the 6th paragraph
Set Rng = .Range.Paragraphs(6).Range
With Rng
.MoveEnd wdCharacter, -1
'Construct the destination file path & first part of name
StrTxt = "H:\Terms & Conditions\Final\" & .Text & "_"
End With
'Get the 5th paragraph
Set Rng = .Range.Paragraphs(5).Range
With Rng
.MoveEnd wdCharacter, -1
'Construct balance of file name
StrTxt = StrTxt & .Text & ".docx"
End With
'Get the whole Section
Set Rng = .Range
With Rng
'Contract the range to exclude the Section break
.MoveEnd wdCharacter, -1
'Copy the range
.Copy
End With
End With
'Create the output document
Set Doc = Documents.Add(Template:=ActiveDocument.AttachedTemplate.FullName, Visible:=False)
With Doc
'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 Or .Characters.Last.Previous = Chr(12)
.Characters.Last.Previous = vbNullString
Wend
'Replicate the headers & footers
For Each HdFt In Rng.Sections(1).Headers
HdFt.Range.Copy
.Sections(1).Headers(HdFt.Index).Range.PasteAndFormat (wdFormatOriginalFormatting)
Next
For Each HdFt In Rng.Sections(1).Footers
HdFt.Range.Copy
.Sections(1).Footers(HdFt.Index).Range.PasteAndFormat (wdFormatOriginalFormatting)
Next
'Save & close the output document
.SaveAs FileName:=StrTxt, AddToRecentFiles:=False
.Close SaveChanges:=True
End With
Next
End With
Set Rng = Nothing: Set Doc = Nothing
Application.ScreenUpdating = True
End Sub

Most grateful for any help

stevembe
03-17-2015, 01:30 AM
13021 Hopefully this explains it a little better

gmayor
03-17-2015, 05:30 AM
You don't make things easy by using merged table cells. You might find it easier to simply re-merge using http://www.gmayor.com/individual_merge_letters.htm or http://www.gmayor.com/individual_merge_letters.htm (the first of those links also has some code examples).

stevembe
03-18-2015, 12:52 AM
Thanks for the reply but unfortunately I am unable to load add ons and use what you recommend. I know it is not easy with the merged table cells but the letters were produced by somebody else.

gmayor
03-18-2015, 01:52 AM
I despair at companies that won't provide the tools that will allow you to work, and then wonder why you struggle to achieve your aims. Get your IT department to test the add-in, which is used around the world by thousands of users, including Government and Military users.

stevembe
03-18-2015, 03:05 AM
I too despair, makes life so much more difficult!