Clone variable in Word VBA
Given the range with revisions in it, I need to reconstruct the original text and the modified text. The first solution was to:
Sub OriginalText (ByVal Rng as Range)
Rng.Revisions.RejectAll
OriginalText = Rng.Text
End sub
Yet it turned out that ByVal is not really ByVal. the moment RejectAll is called, all changes are rejected in original document as well, and there is no way to apply them - they're gone from ThisDocument.Revisions.
Is there a (preferably handy) way to copy the variable Rng to any (say,Rng2) in the sub so I can work开发者_开发技巧 with the copy of the range without affecting the source?
Is there a way to serialized the range and bring it together, maybe?
Upd: Let me put it this way. Is there a chance to copy the object (Range in my case) so the changes made to the copy won't affect the source? I think that still is the fastest and the most elegant solution.
Range is a marker, even though it returns the content of the range. If you want the text, you need to say so.
Set r = Selection.Range
TextNow = Selection.Range.Text
TextOrig = OriginalText(r)
.RejectAll
seems to be clearing all of the revisions (including when the text was first entered). It would be convenient if we could access .Revisions(0)
, but it doesn't look like this is possible.
Your best bet may be to see if you can store the original document before the changes are made. If that is not possible, you could go through with something like this script and find all of the changes by hand.
Try Range.Duplicate, which seems to create a clone of the range.
精彩评论