PDA

View Full Version : [WORD] How to create new document with multiple sections from source doc



richardray
09-14-2012, 03:56 AM
Hi,

First post so hopefully I will have given you enough information to assist me with this issue!

I have a source Word document which has a VBA form to gather data that is inserted into DocVariables within the doc. The document is split into sections and depending on what tick boxes are selected on the form, different sections should be pasted into a new document.

The code for the document creation is as follows - the CLIENT document part is fine as that only contains one section:


Sub CreateInstallDocs()

'This is the part of the document that creates the separate Server and Client Install Docs as DOCX and PDF files

FilePath = ActiveDocument.Path & "\"

If ChkBxStandalone.Value = True Then
ActiveDocument.Sections(1).Range.Copy

Documents.Add
ActiveDocument.SaveAs2 (FilePath & TBCompanyName.Value & " Infor10 Server Install.docx")
Documents.Open (FilePath & TBCompanyName.Value & " Infor10 Server Install.docx")

ActiveDocument.Range.PasteAndFormat (wdUseDestinationStylesRecovery)

Documents("Infor 10 Install-MultiDeployment.docm").Activate
ActiveDocument.Sections(2).Range.Copy
Documents(FilePath & TBCompanyName.Value & " Infor10 Server Install.docx").Activate
ActiveDocument.Range.PasteAndFormat (wdUseDestinationStylesRecovery)


Documents("Infor 10 Install-MultiDeployment.docm").Activate
ActiveDocument.Sections(4).Range.Copy
Documents(FilePath & TBCompanyName.Value & " Infor10 Server Install.docx").Activate
ActiveDocument.Range.PasteAndFormat (wdUseDestinationStylesRecovery)

ActiveDocument.Save
ActiveDocument.Close

' CLIENT DOCUMENT

ActiveDocument.Sections(5).Range.Copy

Documents.Add
ActiveDocument.SaveAs2 (FilePath & TBCompanyName.Value & " Infor10 Client Install.docx")
Documents.Open (FilePath & TBCompanyName.Value & " Infor10 Client Install.docx")

ActiveDocument.Range.PasteAndFormat (wdUseDestinationStylesRecovery)
ActiveDocument.Save
ActiveDocument.Close

End If

At the moment, only the last section is pasted which overwrites all other sections that should be in that document. I obviously need an append method but my VBA isn't too hot and I have inherited this from someone else.

Many thanks in advance,

Richard

macropod
09-14-2012, 05:26 AM
Hi Richard,

Personally, I'd prefer to start off with the entire document, then simply delete the unwanted portions. However, if you want to add other documents, I'd suggest using the InsertFile method.

FWIW, the essential flaw in your current process is that every new document is pasted over the existing document's entire range. At the very least, you should collapse the destination document's range to, say, its end before pasting.

fumei
09-14-2012, 02:00 PM
Personally, I'd prefer to start off with the entire document, then simply delete the unwanted portions. I would strongly agree with that.

Richardray, reductive processes are - generally - easier to handle. Although there may be issues with headers and footers (if applicable).