![]() |
#1
|
|||
|
|||
![]()
In my code, I copy text from a currently open document (openDoc) and paste it into a new document (newDoc). This text has hidden bookmarks and corresponding cross-references. I process newDoc to change those so that, when I paste them back into openDoc, the original referencing structure is updated with new numbering but the old structure is retained. I copy the text into openDoc twice and perform processing of the bookmarks/references twice. The result is two different sets of text with two sets of new numbering, but retaining the old structure.
When this works, it works wonderfully. Everything is updated correctly. My problem: it doesn't always work. The first time I run the code, the two sets of code for processing the bookmarks/references do not work. After that, sometimes, the first set of code does not work, but the second set of code does work. At some point, both of sets of code start to work. Once both start to work, they seem to work all the time (I was correcting other errors for hours yesterday, and they both seemed to work). I thought this was a timing issue, because I paste text into newDoc then immediately begin processing using a set of code for processing the bookmarks/references. I added a one-second time delay (TIMERUN) after pasting into newDoc but before beginning processing. This delay, however, does not correct the problem. Anyone have an idea as to why this code works...sometimes? Here is a small portion of the code. The code that starts "For Each bookmark in newDoc.Bookmarks" is the code that runs sometimes. Code:
Sub Test() ' Declarations Dim selectedText As Range Set selectedText = Selection.Range ' newDoc is a new document where claims are copied and manipulated Dim newDoc As Document ' openDoc is the currently opened document Dim openDoc As Document ' aRng is used as the current insertion point at the cursor in the open document Dim aRng As Range ' Will be used to paste into new document Dim nRng As Range ' Stores the new name for a bookmark Dim newName As String ' Stores a bookmark range for a bookmark Dim bookmarkRange 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 ' 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 ' Add a delay before working on bookmarks and cross-references - this doesn't prevent the problem TIMERUN (1) ' adds one second delay ' Change the bookmarks and cross-references -- first time For Each bookmark In newDoc.Bookmarks ' Check if the bookmark name starts with "_Ref" (assuming this pattern) If Left(bookmark.Name, 4) = "_Ref" Then ' Generate new name newName = "New_" & Mid(bookmark.Name, 5) ' Save bookmark range Set bookmarkRange = bookmark.Range ' Add a new bookmark with the updated name newDoc.Bookmarks.Add newName, bookmarkRange ' Update the cross-references to point to the new bookmark name UpdateCrossReferences newDoc, bookmark.Name, newName ' Delete the old bookmark with the _Ref name bookmark.Delete End If Next bookmark ' Perform processing plus add text ' Paste again at the end of the material in newDoc Set nRng = newDoc.Content nRng.Collapse Direction:=wdCollapseEnd nRng.Paste ' Add a delay before working on bookmarks and cross-references - this doesn't prevent the problem TIMERUN (1) ' adds one second delay ' Change the bookmarks and cross-references -- second time For Each bookmark In newDoc.Bookmarks ' Check if the bookmark name starts with "_Ref" (assuming this pattern) If Left(bookmark.Name, 4) = "_Ref" Then ' Generate new name newName = "Ne1_" & Mid(bookmark.Name, 5) ' Save bookmark range Set bookmarkRange = bookmark.Range ' Add a new bookmark with the updated name newDoc.Bookmarks.Add newName, bookmarkRange ' Update the cross-references to point to the new bookmark name UpdateCrossReferences newDoc, bookmark.Name, newName ' Delete the old bookmark with the _Ref name bookmark.Delete End If Next bookmark ' More processing and adding text ' Paste all the revised text from newDoc back into the original document, openDoc ' Insert the manipulated text at the current cursor location aRng.FormattedText = newDoc.Range.FormattedText 'Close that document (without nasty questions) Documents(newDoc).Close SaveChanges:=wdDoNotSaveChanges End Sub |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
Training | Word VBA | 1 | 11-09-2017 12:06 AM |
Where to put Subroutine to make sure bookmarks aren't deleted so cross-references work | mrsjetset | Word VBA | 5 | 06-29-2016 05:06 PM |
Convert manual cross references in footnotes to other footnotes to automatic cross references | ghumdinger | Word VBA | 7 | 11-20-2014 11:47 PM |
![]() |
Suchoklates | Word | 1 | 09-19-2013 02:32 AM |
Bookmarks with different figure references | Dan_68 | Word | 0 | 02-15-2010 08:29 PM |