View Single Post
 
Old 02-03-2024, 01:28 PM
ctviggen ctviggen is offline Windows 10 Office 2016
Advanced Beginner
 
Join Date: Feb 2021
Posts: 54
ctviggen is on a distinguished road
Default Add text to beginning of new document opened from original Word document, with styles

Hi all, I have a program where I open one (original) document, copy text, paste that text into a new document (newDoc), manipulate that text, then paste the manipulated text at a cursor location in the original document. This works well, and retains styles, which is what I wanted.


Before I paste the manipulated text into the original document, I want to add a few paragraphs at the beginning of newDoc. Note: newDoc has both StyleA and StyleB. The following describes it.

Beginning of newDoc:

Original starting paragraph that is StyleA

What I want:

Para1 that is StyleA
"Blank" paragraph that is Style B
Para2 that is StyleA
"Blank" paragraph that is Style B
Para3 that is StyleA
"Blank" paragraph that is Style B
Original starting paragraph that is StyleA


I have tried the following code (snippet):


Code:
    Set nRng = newDoc.Content
    ' nRng.Collapse Direction:=wdCollapseStart
    ' I think you could start the next line(s) with nRng or newDoc
    newDoc.paragraphs(1).Range.InsertBefore Text:="Test for para1" & vbCr
    ' newDoc.paragraphs.Add
    newDoc.paragraphs(2).Range.InsertBefore Text:="Test for para2" & vbCr
     newDoc.paragraphs(3).Range.InsertBefore Text:="Test for para3" & vbCr
This inserts the 3 paragraphs I want (all with StyleA, since this is what the original paragraph in newDoc was), but I cannot figure out how to add "blank" paragraphs that are StyleB, and I cannot figure out even if I added those paragraphs how to set them to be a style.


The "newDoc.paragraphs.Add" works, but it adds the new paragraph at the end of the newDoc. That's why it is commented out. The nRng was a test to set the range to the beginning of newDoc, then use this to add paragraphs, but I couldn't get it to work. I had to "brute force" the issue instead, by using actual paragraph numbers.


Is there an easier way to do this? Or at least add "blank" paragraphs with StyleB?
Reply With Quote