Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-10-2023, 06:09 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 Working with an active document and new document to retain styles and cross-referencing

We have text that has the following structure. This uses styles and also cross-referencing (to paragraph numbers).

- numbered style
- non-numbered style(s)
- numbered style, but includes cross-reference to numbered style (paragraph format)
- non-numbered style(s)...


Something like this example:


1. Numbered style
some non-numbered style(s)
2. Numbered style that cross-references paragraph #1
some non-number styles(s)
3. Numbered style that cross-references paragraph(s) #1 and/or #2
....


In order to get the cross-references, someone has inserted a cross-reference with a reference type of "numbered item" and an "insert reference to" "paragraph number".

I want to:

a) have someone copy the example text above (from the "active" document)
b) have that person put an insertion point after the example text
c) open a new document and insert the example text, complete with formatting and cross-referencing intact
d) manipulate text in the text of the new document


e) copy the manipulated text and paste back into the "active" document, hopefully retaining the styles and the cross-referencing

This is the code to do (a), (b), and (c):

Code:
Sub Test()
    ' Prerequisites: copy the formatted text
    '   Put the insertion point at a paragraph after the copied text, on a blank line
        
    ' Declarations
    Dim selectedText As Range
    Set selectedText = Selection.Range
    Dim newDoc As Document
    
       
    ' 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
The document that is created has the styles from the active document and all the cross-referencing too.

How do I manipulate the text in newDoc? (All the code I have uses Selection.Find, which does not seem appropriate.) (E.g., this is a simple find and replace: FindTextA and ReplaceWithTextB.)

Once I manipulate the text, how do I copy the contents of the (revised) newDoc?

Is it possible to copy (and paste) the text complete with the styles and embedded cross-referencing?

Note: the styles in newDoc are the same as what is in the active document. So, assuming the example above is used with three numbered styles, the pasted-in text from newDoc would have 4,5,6 based on the same numbered style.
Reply With Quote
  #2  
Old 06-12-2023, 06:53 PM
Guessed's Avatar
Guessed Guessed 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
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

If the selected text includes full paragraphs then the styles will be pasted from document to document quite happily. If your selection doesn't include the trailing paragraph mark then the last selected paragraph's style won't transfer.

You haven't said how you want to 'manipulate' the text in newDoc so we can't advise on that.

When you paste back into the original doc, the same rules apply EXCEPT with the possible complication of cross-references. There are things you need to understand with cross-references:
1. Inserting a cross-reference places a hidden bookmark around the text being referred to and this is what the cross-ref field points at.
2. A bookmark can only exist in one place in one document.
3. A cross-ref only updates when you update the field so it is often out of date and needs to be refreshed if you want to see the right result.

So, if you COPY text including bookmarks AND cross-refs from a document paste it into another document then the cross-ref will still work at the new location. If you subsequently copy that range back to the original document it will try to bring the same bookmark name back to the document where that bookmark ALREADY exists.
If you REPLACE the original bookmarked text then you are also replacing that bookmark.
If you ADD the text back into another location in the document then Word will notice that the bookmark already exists and remove it from the freshly pasted text. The cross-ref will still point to the bookmark name and therefore point back to the original location of the bookmark. If that bookmarked text is subsequently removed (since you now have it in the document twice) then you will discover the cross-ref will break because the document no longer contains a bookmark with that name.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 01-01-2024, 10:54 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

Sorry, I got waylaid by other items, mainly family issues. Thank you for the input. I finally got back to looking at this.

Let's ignore the cross-referencing for now, since you point out the issues with that. (Thank you for the explanation.)

For the manipulation of text in the newDoc, this is a simple find and replace (e.g., replace the word X with the word Y or change the snippet "A B C" to "C D E F G", where the capital letters are words).

Normally, I do something like this for the original document (with a section selected):

Code:
    ' Replaces tab character with two spaces
    With Selection.Find
        .Text = "^t"
        .Replacement.Text = "  "
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    ' I think this actually causes the replacement to happen
     Selection.Find.Execute Replace:=wdReplaceAll

This is just one example where I replace a tab character with two spaces.

How do I perform the same replacement in newDoc, then copy all the (modified) text there back into the original document?
Reply With Quote
  #4  
Old 01-03-2024, 03:07 AM
Guessed's Avatar
Guessed Guessed 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
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Assuming you have already defined the original document and newDoc then you could try this
Code:
Sub aTest()
  Dim aRng As Range, aDoc As Document, newDoc As Document
  'I'll assume you have already set aDoc and newDoc to specific documents
  
  With newDoc.Range.Find
    .Text = "^t"
    .Replacement.Text = "  "
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
  End With
  Set aRng = aDoc.Range
  aRng.Collapse Direction:=wdCollapseEnd
  aRng.FormattedText = newDoc.Range.FormattedText
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 01-20-2024, 01:22 PM
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

Sorry for my delay. That is great, thank you. I'm working on this now.
Reply With Quote
  #6  
Old 01-20-2024, 03:24 PM
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

This code works well, except that it adds the material pasted into the currently open document at the end of the document:
Code:
  
Set aRng = aDoc.Range   
 aRng.Collapse Direction:=wdCollapseEnd   
 aRng.FormattedText = newDoc.Range.FormattedText
What I would like is either
(1) select and copy all the text, but leave the text selected, and the material would be pasted in at the end of the selection (and I could add a carriage return before pasting); or

(2) after selection and copying of the text, I move the cursor to a blank line after the text I just selected.

I modified your code slightly to try this, where only the first line is modified:
Code:
  
Set aRng = Selection.Range   
 aRng.Collapse Direction:=wdCollapseEnd   
  aRng.FormattedText = newDoc.Range.FormattedText
This SEEMS to me like it should work, but it doesn't. Nothing gets pasted in regardless of whether I try (1) or (2). I'm stumped as to why.

(Also, as you said, the cross-referencing uses the "old" cross-referencing for the material that is pasted in, but I'll work on that later. Thanks for the information about this.)
Reply With Quote
  #7  
Old 01-21-2024, 08:05 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

I've done more research. What happens is I can manipulate the text in the newDoc fine, but the "Set aRng = Selection.Range" selects the end of the newDoc, so the modified text gets copied there.



I've tried (where "openDoc" is the "aDoc" as suggeted) "Set aRng = openDoc.Application.Selection.Range". This also inserts the text into newDoc instead of openDoc/aDoc. This one doesn't make sense to me, as it seems correct.


I've tried "Set aRng = openDoc.Selection.Range", but this gives an error.



I've done a search to find how to paste text into an open Word document at a cursor location, but what I find doesn't use two open documents.


I can't find how to select a cursor location in an open document to be able to paste in text from another (also open) document. I'm stumped.
Reply With Quote
  #8  
Old 01-21-2024, 03:53 PM
Guessed's Avatar
Guessed Guessed 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
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

You haven't provided your full code but it sounds like you have defined both aDoc and newDoc as the same document.

When your code starts up, you should define the activedocument before you start getting confused with a second document eg
Code:
Set aDoc = ActiveDocument
Then you can create and define the new document in a single line
Code:
Set newDoc = Documents.Add
This allows you to be very clear that these are two different documents, both open at the same time.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #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
  #10  
Old 01-27-2024, 07:55 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

So, if I enable "Set aRng = openDoc.Range" and disable "Set aRng = openDoc.Application.Selection.Range", the modified material goes into the open document (openDoc), instead of into the new document (newDoc), but at the end of the open document.



I just want that insertion point to either go where the cursor currently is in the open document or at the end of the selected text that is being modified from the open document.


I thought "Set aRng = openDoc.Application.Selection.Range" and "aRng.Collapse Direction:=wdCollapseEnd" would do that, but these do not.
Reply With Quote
  #11  
Old 01-28-2024, 08:46 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

I figured it out. If you put "Set aRng = Selection.Range" after "Set openDoc = ActiveDocument" but before you paste into the new document (newDoc), the insertion point comes at the cursor location in the open document. If you move that same statement later, I believe the "Selection" is in newDoc, and then the paste will occur in newDoc. Like this, but I've left all the other stuff in that did not work:


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
    'This should set the insertion point where the cursor currently is
    ' in the open document (openDoc)
    Set aRng = Selection.Range ' Set here before you open the new document
        
    ' 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
Reply



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 03:40 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