Forum

Files split but nee...
 
Notifications
Clear all

Files split but need to retain source files formatting and layout

9 Posts
3 Users
0 Reactions
175 Views
(@david_ng)
Posts: 310
Reputable Member
Topic starter
 

I want to split a  document preserving the format try using the following VB scripts but failed to keep the format after file split into 110 files .

Can someone help to rectify the errors

 

Sub Split()

Dim i As Long, Source As Document, Target As Document, Letter As Range

Set Source = ActiveDocument
For i = 1 To Source.Sections.Count
Set Letter = Source.Sections(i).Range
Set Target = Documents.Add
Target.Range = Letter
Target.Sections(1).PageSetup.SectionStart = wdSectionContinuous
Source.Range.FormattedText = Target.Range.FormattedText
Target.SaveAs FileName:="C:DATA" & "Letter to Creditor inviting views - " & i
Target.Close
Next i
End Sub

Wink

 
Posted : 26/09/2024 11:08 am
(@david_ng)
Posts: 310
Reputable Member
Topic starter
 

Pls respond with better codes

 

Thanks

 
Posted : 27/09/2024 3:55 am
(@keebellah)
Posts: 373
Reputable Member
 

What if you tell us more first?

You don't give any version information and I doubt very much that this is an Excel question

 
Posted : 28/09/2024 1:57 am
(@david_ng)
Posts: 310
Reputable Member
Topic starter
 

Sorry this is  Word document macro request.

 
Posted : 28/09/2024 3:39 am
(@keebellah)
Posts: 373
Reputable Member
 

I'm out then 

 
Posted : 29/09/2024 2:05 am
(@david_ng)
Posts: 310
Reputable Member
Topic starter
 

Thanks

 
Posted : 30/09/2024 8:13 pm
Anders Sehlstedt
(@sehlsan)
Posts: 972
Prominent Member
 

Hello,

I asked Copilot. Don't know if it works though.

Copilot
Genererar svar åt dig…

Skickades av Copilot:
It looks like your script is almost there, but there are a few adjustments needed to ensure the formatting is preserved when splitting the document. Here’s a revised version of your script:

Sub Split()

    Dim i As Long, Source As Document, Target As Document, Letter As Range

    Set Source = ActiveDocument
    For i = 1 To Source.Sections.Count
        Set Letter = Source.Sections(i).Range
        Set Target = Documents.Add
        ' Copy the formatted text from the source to the target
        Target.Range.FormattedText = Letter.FormattedText
        ' Ensure the section start type is continuous
        Target.Sections(1).PageSetup.SectionStart = wdSectionContinuous
        ' Save the new document with a unique name
        Target.SaveAs FileName:="C:DATA" & "Letter to Creditor inviting views - " & i & ".docx"
        ' Close the new document
        Target.Close
    Next i

End Sub

Key Changes:

  1. FormattedText Property: Use 
    Letter.FormattedText

     instead of 

    Source.Range.FormattedText

     to ensure the formatting of each section is preserved.

  2. File Extension: Added 
    .docx

     to the 

    SaveAs

     filename to ensure the files are saved in the correct format.

Give this a try and see if it resolves the formatting issues. If you encounter any further problems, feel free to ask!

Br,
Anders

 
Posted : 01/10/2024 5:08 pm
(@david_ng)
Posts: 310
Reputable Member
Topic starter
 

Thanks millions Anders, will give it a go with live data and file. 

 
Posted : 03/10/2024 1:57 am
(@david_ng)
Posts: 310
Reputable Member
Topic starter
 

I try the macro but still failed

 
Posted : 03/10/2024 2:28 am
Share: