Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #9  
Old 01-27-2024, 07:39 AM
ctviggen ctviggen is offline Working with an active document and new document to retain styles and cross-referencing Windows 10 Working with an active document and new document to retain styles and cross-referencing Office 2016
Advanced Beginner
Working with an active document and new document to retain styles and cross-referencing
 
Join Date: Feb 2021
Posts: 54
ctviggen is on a distinguished road
Default

Thank you. I _think_ I'm doing that. Here is the code. I cut the code down, because it's doing a lot of search/replace, so the part where there is finding X and replacing with Y is done multiple times. There's other code, too, but I cut that out. This code is what I just tried, with a break point at the statement that closes the new document. Everything works, but the pasted in/revised material gets pasted to the new document (newDoc) and not into the open document (openDoc), which is where I want it.


I just tried it using the "aRng.Collapse Direction:=wdCollapseEnd", and the same thing happens: the revised material gets pasted to the end of the new document (newDoc).



To me, this seems like aRng is set to the open document, and the statement "aRng.FormattedText = newDoc.Range.FormattedText" should put the revised material _somewhere_ in the open document, but it gets put into the new document instead.



Code:
Sub Example()
    
    Dim newDoc As Document
    Dim openDoc As Document
    Dim aRng As Range
    
    ' See if this sets the open document to openDoc
    Set openDoc = ActiveDocument
        
    ' Add a new document to store the copied text
    Set newDoc = Documents.Add
    ' This adds with all the formatting, including cross-referencing
    newDoc.Content.Paste
                
    ' Change "X" to "Y"
    With newDoc.Range.Find
        .Text = "X"
        .Replacement.Text = "Y"
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll ' This performs the replacement
    End With
        
    ' Set aRng = openDoc.Range
    ' Set aRng to the current selection in the open document
    ' The following does not work for the currently open document and copies into newDoc
    ' Set aRng = Selection.Range
    ' The following seems like it should work to copy into the current open document (openDoc),
    '    but the text goes into newDoc
    Set aRng = openDoc.Application.Selection.Range
    ' The following causes an error
    ' Set aRng = openDoc.Selection.Range
    
    ' Insert the manipulated text at the current cursor location
    ' aRng.Collapse Direction:=wdCollapseEnd
    aRng.FormattedText = newDoc.Range.FormattedText

    'Close that document (without nasty questions)
    Documents(newDoc).Close SaveChanges:=wdDoNotSaveChanges
      
    
End Sub
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Code on a button to attach a document to my Word Auto fill document is not working on a Mac reynoldsrjr Word 0 03-30-2020 04:00 AM
Single step Word Styles from Source Document through entire Destination document? xbliss Word 6 08-27-2016 09:36 PM
How to save active document to SharePoint document library Rose roon Word VBA 9 09-22-2015 10:53 PM
document styles and some formatting doesn't appear where should when different people view document Dilbert fan Word 1 08-23-2015 05:07 AM
Working with an active document and new document to retain styles and cross-referencing Cross Referencing w/Track Changes Active Liza Word 1 11-06-2014 12:44 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:55 PM.


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