Processing
 

Write a Collection of paragraphs into a new document

20/09/2016 13:58#1

Joined at: 10 months ago

Post: 2

Thank: 0

Thanked: 0

I need to write a function SaveFileWithParagraphs, that takes a path, filename and a Collection of paragraphs (which were extracted from a different word document) and simply writes the paragraphes into the new document with the same style etc.

What I have so far:

Sub SaveFileWithParagraphs(path As String, name As String, paras As Collection)   Dim wrdApp As Word.Application   Dim wrdDoc As Word.Document   Dim i As Integer   Set wrdApp = CreateObject("Word.Application")   wrdApp.Visible = True   Set wrdDoc = wrdApp.Documents.Add    With wrdDoc     For Each p In paras                     .Content.InsertAfter p.Range.Text      Next     .SaveAs (path + "\" + name)     .Close ' close the document   End With wrdApp.Quit ' close the Word application End Sub 

The problem with this is that I loose the Styling of the paragraphs, since it only copies the plain text. Any ideas on how to solve this problem?

Edit

Thanks to Cominterns answer I got this:

' ... With wrdDoc    For Each p In paras            p.Range.Copy            Selection.EndKey Unit:=wdStory            .Content.Paste    Next End With ' ... 

Which copies my paragraphs keeping all the styling. Only one small problem remains: Whenever a new paragraph is copied, it overwrites the previously copied one, so that in the end only the last paragraph remains in the document. This seems like an easy to solve problem, but I cant get it to work. Any further help is much appreciated. :)

20/09/2016 14:35Top#2

Comintern

Member

Joined at: 11 months ago

Post: 20

Thank: 0

Thanked: 0

p.Range.Text is exactly that - the text. If you want to keep the formatting, you can copy the range and then paste it:

Dim target As Range With wrdDoc     For Each p In paras         p.Range.Copy         Set target = .Range         target.Collapse wdCollapseEnd         target.PasteAndFormat wdFormatOriginalFormatting     Next     .SaveAs (path + "\" + name)     .Close ' close the document End With 
Similar articles

VBA regex matching over two lines

5 months ago - Reply: 1 - Views: 44

Table output to Word-2011 from Rstudio using knit

5 months ago - Reply: 0 - Views: 44

Write a Collection of paragraphs into a new document

5 months ago - Reply: 1 - Views: 72

how to write paragraphs without show to document

5 months ago - Reply: 1 - Views: 67

numericals in word macro

5 months ago - Reply: 1 - Views: 46

Can not find proofing tools

5 months ago - Reply: 0 - Views: 2

Possible values for <w:outlineLvl> in Word OOXML

5 months ago - Reply: 1 - Views: 8

C# restrict editing partial word paragraph

6 months ago - Reply: 2 - Views: 146

Macro translation Please (possible virus)

5 months ago - Reply: 1 - Views: 35

Doc/Docx Fastest Reading Method for Searching

5 months ago - Reply: 0 - Views: 3

Save embedded file / content from word

5 months ago - Reply: 0 - Views: 6

Counting words in Word document, including footnores

5 months ago - Reply: 1 - Views: 24

c# access extract word ole object

5 months ago - Reply: 1 - Views: 23

Using word wildcards to find unaccepted changes

5 months ago - Reply: 1 - Views: 46

vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re