Copy all paragraphs formatted with a certain style and paste them atthe end of the document

A

andreas

Dear Experts:

I got a document (Word 2003) in which I should perform the following
tasks:

- copy all paragraphs formatted with a certain user-defined paragraph
style
- paste them at the end of the current document

Help is appreciated. Thank you very much in advance. Regards, Andreas
 
L

Lene Fredborg

The following macro should do what you want:


Sub CopyParagraphsToEndOfDoc()

Dim oRange As Range
Dim oPara As Paragraph
Dim oRangePaste As Range
Dim strStyle As String

'Define the style name
'Replace the style name below with the relevant style
strStyle = "MyStyle"

'Start in an empty paragraph at end of doc
ActiveDocument.Range.InsertParagraphAfter

'Keep track of the original content without last paragraph
Set oRange = ActiveDocument.Range
With oRange
.End = .End - 1

'Check all paragraphs and copy to end of document if strStyle
For Each oPara In .Paragraphs
'Replace the style name below with the relevant style
If oPara.Style = strStyle Then
'Copy paragraph
oPara.Range.Copy
'Go to end of document and paste
Selection.EndKey (wdStory)
Selection.Paste
End If
Next oPara
End With

'Delete last empty paragraph
ActiveDocument.Paragraphs.Last.Range.Delete

'Clean up
Set oRange = Nothing
Set oRangePaste = Nothing

MsgBox "Finished."
End Sub

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
A

andreas

The following macro should do what you want:

Sub CopyParagraphsToEndOfDoc()

    Dim oRange As Range
    Dim oPara As Paragraph
    Dim oRangePaste As Range
    Dim strStyle As String

    'Define the style name
    'Replace the style name below with the relevant style
    strStyle = "MyStyle"

    'Start in an empty paragraph at end of doc
    ActiveDocument.Range.InsertParagraphAfter

    'Keep track of the original content without last paragraph
    Set oRange = ActiveDocument.Range
    With oRange
        .End = .End - 1

        'Check all paragraphs and copy to end of document if strStyle
        For Each oPara In .Paragraphs
            'Replace the style name below with the relevant style
            If oPara.Style = strStyle Then
                'Copy paragraph
                oPara.Range.Copy
                'Go to end of document and paste
                Selection.EndKey (wdStory)
                Selection.Paste
            End If
        Next oPara
    End With

    'Delete last empty paragraph
    ActiveDocument.Paragraphs.Last.Range.Delete

    'Clean up
    Set oRange = Nothing
    Set oRangePaste = Nothing

    MsgBox "Finished."
End Sub

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmarkwww.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word








- Zitierten Text anzeigen -

Dear Lene,

as always from your side, professional, concise coding. Thank you very
much for your terrific help. I really appreciate it.

Have a nice day. Regards, Andreas
 
L

Lene Fredborg

You are welcome.

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
L

Lene Fredborg

In the macro i posted, I can see that I forgot to delete the lines including
oRangePaste (Dim oRangePaste As Range, Set oRangePaste = Nothing). The
variable oRangePaste is not used in the final version so you can delete the
two lines if you wish.

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
K

Klaus Linke

Another way to achieve pretty much the same thing would be to put a "table
of contents" field that just references that style (no page numbers, no
hyperlinks) at the end of the document:
{ TOC \n \t "myStyle" }

Regards,
Klaus
 
L

Lene Fredborg

Hi Klaus,

Good idea. I did not think of that solution. I imagined it was important to
retain the original formatting (even if I did not ask). If that is correct,
the TOC solution may not be well suited.

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
A

andreas

Another way to achieve pretty much the same thing would be to put a "table
of contents" field that just references that style (no page numbers, no
hyperlinks) at the end of the document:
{ TOC \n \t "myStyle" }

Regards,
Klaus

Hi Klaus,

thank you very much for this valuable tip. Both codes/tips from you
and Lene are valuable to me. Regards, Andreas
 

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