Paste a Word file at the end of a section

X

XSterna

Hi

Does anyone know how can I paste a word file at the end of a section ?

I want to create a program that will be able to build a word document
made of different other word documents.
The idea is to be able to paste differents documents in a row.

For example, I have 3 sections in the main Document. Each of them have
a Title and I want to be able to paste Block 1 in Section 1 Block 2 in
Section 2 etc... I don't want to do it in one shot but create the
structure first with the Titles and then paste the blocks in the
appropriate Section :

Section 1
Block 1
Section 2
Block 2
Section 3
Block 3

The tricky thing is also that I want to keep the Style of the paste
paragraphs.

Thanks for any kind of help

X
 
J

Jean-Guy Marcil

XSterna was telling us:
XSterna nous racontait que :
Hi

Does anyone know how can I paste a word file at the end of a section ?

I want to create a program that will be able to build a word document
made of different other word documents.
The idea is to be able to paste differents documents in a row.

For example, I have 3 sections in the main Document. Each of them have
a Title and I want to be able to paste Block 1 in Section 1 Block 2 in
Section 2 etc... I don't want to do it in one shot but create the
structure first with the Titles and then paste the blocks in the
appropriate Section :

Section 1
Block 1
Section 2
Block 2
Section 3
Block 3

The tricky thing is also that I want to keep the Style of the paste
paragraphs.

You would need to be more explicit regarding the overall situation.

Are you building the Main document from scratch?
Does it already contain the said three sections?
Is it always three sections?
Where are those other documents from?
Etc.

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
 
X

XSterna

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
 
J

Jean-Guy Marcil

XSterna was telling us:
XSterna nous racontait que :
First of all thank you very much for your help !




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.

No the sections are created before trying to paste the text in the
sections
It depends of what I need and what I select in the DialogBox

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.



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

How are you doing the paste?

Lookup the FormattedText property in the VBA help.

You can assign a FormattedText range and then paste it using the
FormattedText property to retain al the styles from the source text.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top