Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-03-2024, 01:28 PM
ctviggen ctviggen is offline Add text to beginning of new document opened from original Word document, with styles Windows 10 Add text to beginning of new document opened from original Word document, with styles Office 2016
Advanced Beginner
Add text to beginning of new document opened from original Word document, with styles
 
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
  #2  
Old 02-03-2024, 01:45 PM
Italophile Italophile is online now Add text to beginning of new document opened from original Word document, with styles Windows 11 Add text to beginning of new document opened from original Word document, with styles Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

You need to learn to use IntelliSense, the Object Browser, and online help. You’ll get the answers you need much more quickly.

For example see the following:
Paragraphs.Add method (Word) | Microsoft Learn
Range.InsertParagraph method (Word) | Microsoft Learn
Range.InsertParagraphAfter method (Word) | Microsoft Learn
Range.InsertParagraphBefore method (Word) | Microsoft Learn

Range.Style property (Word) | Microsoft Learn
Reply With Quote
  #3  
Old 02-04-2024, 07:52 AM
ctviggen ctviggen is offline Add text to beginning of new document opened from original Word document, with styles Windows 10 Add text to beginning of new document opened from original Word document, with styles Office 2016
Advanced Beginner
Add text to beginning of new document opened from original Word document, with styles
 
Join Date: Feb 2021
Posts: 54
ctviggen is on a distinguished road
Default

Thanks, but none of that was helpful. You do realize I searched for the answer for this, correct? Not only did I do that, but I asked Chat GPT multiple questions about how to do this, and it couldn't do it. It's been quite helpful on other questions, but it could not answer this one.



For the paragraphs.add method, I'm not using a selection object, so that doesn't work.


The Range.InsertParagraph method doesn't tell my how to do this in an open document, again because it's using selection, which I'm not. Nothing is selected.



The range.style link is completely worthless.



None of those links were helpful for my particular question.
Reply With Quote
  #4  
Old 02-04-2024, 08:21 AM
Italophile Italophile is online now Add text to beginning of new document opened from original Word document, with styles Windows 11 Add text to beginning of new document opened from original Word document, with styles Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Quote:
Originally Posted by ctviggen View Post
For the paragraphs. Add method, I'm not using a selection object, so that doesn't work.
The third example in the documentation:
"This example adds a paragraph mark before the second paragraph in the active document."

Code:
ActiveDocument.Paragraphs.Add _ 
 Range:=ActiveDocument.Paragraphs(2).Range
Quote:
Originally Posted by ctviggen View Post
The Range.InsertParagraph method doesn't tell my how to do this in an open document, again because it's using selection, which I'm not. Nothing is selected.
None of the example code in the documentation for InsertParagraph, InsertParagraphAfter or InsertParagraphBefore uses Selection. For example:
Code:
Set myRange = ActiveDocument.Range(0, 0) 
With myRange 
 .InsertParagraph 
 .InsertBefore "Dear Sirs," 
End With
Code:
Set myRange = ActiveDocument.Range(0, 0) 
With myRange 
 .InsertBefore "Title" 
 .ParagraphFormat.Alignment = wdAlignParagraphCenter 
 .InsertParagraphAfter 
End With
Quote:
Originally Posted by ctviggen View Post
The range. Style link is completely worthless.
"To set this property, specify the local name of the style, an integer, a WdBuiltinStyle constant, or an object that represents the style." i.e.
Code:
nRng.Style = "styleName"
Reply With Quote
  #5  
Old 02-04-2024, 08:31 AM
ctviggen ctviggen is offline Add text to beginning of new document opened from original Word document, with styles Windows 10 Add text to beginning of new document opened from original Word document, with styles Office 2016
Advanced Beginner
Add text to beginning of new document opened from original Word document, with styles
 
Join Date: Feb 2021
Posts: 54
ctviggen is on a distinguished road
Default

I figured it out, using a brute force method. I first re-ran the code by Chat GPT, but that was wrong. So, I looked at other code of mine and just went line by line through this, changing coding as necessary and running 10+ times, until I got what I wanted. Not pretty, but works.


Code:
newDoc.paragraphs(1).Range.InsertBefore Text:="Para1" & vbCr & vbCr
    newDoc.paragraphs(2).Style = "StyleB"
    newDoc.paragraphs(2).Range.InsertAfter Text:="Para2" & vbCr & vbCr
    newDoc.paragraphs(4).Style = "StyleB"
    newDoc.paragraphs(4).Range.InsertAfter Text:="Para3" & vbCr & vbCr
    newDoc.paragraphs(6).Style = "StyleB"
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Add text to beginning of new document opened from original Word document, with styles Get path of newly opened word document MANOHAR Word 2 04-12-2017 06:07 AM
Hyperlinks that Do Not Exist in the Original Word Document Appearing in the PDF Document diarrheaofthewprocessor Word 11 01-24-2017 01:52 PM
Single step Word Styles from Source Document through entire Destination document? xbliss Word 6 08-27-2016 09:36 PM
Word 2013 - When deleting section breaks, cursor jumps to beginning of document ranholmom66 Word 3 07-01-2016 06:01 AM
Add text to beginning of new document opened from original Word document, with styles Lost word document now opened in unknown Danwordman Word 2 07-07-2015 05:03 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:29 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft