First of all thank you very much for your help !
You would need to be more explicit regarding the overall situation.
Are you building the Main document from scratch?
Yes I am. The idea of the macro is to create a document. The Macro run
a Dialog Box where the user is able to select the Block he want to
have in his document. I am using Section to divide my document in a
way I will be able to access the different blocks easily. Here is a
simple example of the mechanism :
dialog Box :
Choose the parts you want in the document :
X 1. First Part
2. Second Part
X 3. Third Part
4. Fourth Part
The user who selects Part 1 and 3 will then have a document like
this :
Section 1 :
Title : First Part
Section 2 :
Title : Third Part
My issue is the next step. When I have created those parts I want to
be able to access them and paste a whole Word file which contains
paragraphs of text that the user will need in his parts. For example
if he wants the First Part he will then ask to put in the First Part a
block "first block" and a block "third block" from a range of
available blocks.
Does it already contain the said three sections?
No the sections are created before trying to paste the text in the
sections
Is it always three sections?
It depends of what I need and what I select in the DialogBox
Where are those other documents from?
The other documents are already existing and contain part of document.
To be more specific, I am trying to create a cutomized contract. I
have a range of articles and the idea is to be able to select what
section I want in my contract and what article are needed in it.
Meanwhile, you can look at the Sections collection in the Word VBA model.
You can iterate each section, set a range from the section and manipulate
that range in order to insert whatever text you want.
For instance:
Sub test()
Dim secDoc As Sections
Dim rgeSec As Range
Dim i As Long
Set secDoc = ActiveDocument.Sections
For i = 1 To secDoc.Count
Set rgeSec = secDoc.Item(i).Range
With rgeSec
'Remove Section break or last para in document from range
.MoveEnd wdCharacter, -1
.InsertAfter "New Text after." & Chr(13)
End With
Next
End Sub
My main problem is to place my text with the appropriate style after a
title I created in a section.
Your example helped me to place my text in the appropriate section
without erasing the next one (what I did before ...). But I am still
having some issues with pasting my document in the appropriate style.
To illustrate I would like to obtain this result :
Section I :
Article 10 : Intellectual Property /with style Title 1
10.1 blablablablabla / with style article
At the moment when I paste my text either I get both in style Title 1
or in style article.
Here the code I am using :
With rgeSec
'Remove Section break or last para in document from range
.MoveEnd wdCharacter, -1
.InsertAfter Chr(13) 'Moving to a new line
.InsertAfter "New Text after." & Chr(13)
.Style = "Corps"
End With
Regards,
Xavier