#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 |
#2
|
||||
|
||||
It looks like that code pastes BEFORE anything has been copied - so it assumes you copied something before the code was run. If not, where did the clipboard contents come from? Assuming you wanted the selection from openDoc then you could have used instead
newDoc.Range.FormattedText = aRng.FormattedText Looping through bookmarks from the start and removing+adding bookmarks while you are doing it is problematic - however in this case it appears you are getting away with it. Can you explain why you want the original text put in there twice with bookmarks named with two different series? I would envisage that if there are any Cross-Refs outside the original selection in openDoc, they will be broken after the _Ref bookmarks are removed. Wouldn't it be better to leave that one set there and just introduce the _New section after or before it?
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Sorry for my delay in responding. I figured out what was wrong, via a lot of time. When I rebooted, I could not get this code to work. But when I manually showed bookmarks (ctrl-shift-F5, choose "hidden bookmarks"), then the code worked fine. I had to add this code before any bookmark manipulation:
Code:
ActiveDocument.Bookmarks.ShowHidden = True Then the code works fine. Andrew, thank you for the comment. I am doing something probably no one does. I have some numbered paragraphs with cross references in them. I copy this text, put the cursor below it, then I want to put a modified version of this code there, with updated bookmarks/cross-references, then put yet another modified version of this code there, with yet further updated bookmarks/cross-references. What I do is copy the text to a new document, change the bookmarks/cross-references, change the text, go to the bottom of the new document, paste the original text in again, change the bookmarks/cross-reference, manipulate it in a different way, then paste all the text in the new document back to the insertion point. If you have suggestions about a better way to loop through bookmarks from the start and removing+adding bookmarks while you are doing it, I'm open to suggestions. This code does work, and in my debugging, I went through it line-by-line to see exactly what it did. But I'm always open for suggestions of something better/less likely to cause error/better programming techniques, etc. |
#4
|
|||
|
|||
After thinking about this, what I probably did was the following. Rebooted my computer, determined the code didn't work at all (because hidden bookmarks weren't being shown). Incredulous (since I had been working on other parts of the code for hours), I then put a break after the first set of bookmark manipulations. Those didn't work, but to check, I had to show the hidden bookmarks. I then finished running the code, and it appeared the second set of bookmark manipulations worked...because they did because hidden bookmarks were no longer hidden.
And I did something similar at home and at work, never realizing that every time I went to debug this, I showed hidden bookmarks, which allowed the code (or part of it, depending on where I put a break) to work. I never realized it until I stumbled upon it today, then I tested it by turning on showing hidden bookmarks manually after a reboot before running the code. Then the code worked. I asked ChatGPT how to enable bookmarks via VBA, and got two wrong answers. I did a search and found Microsoft's discussion of this, and used their code (the code I show above). Sometimes, ChatGPT is helpful, but many times, it's not. |
#5
|
|||
|
|||
It looks like the problem may be due to bookmark/link processing starting before the text is completely inserted into the new document (newDoc). There may be some asynchronous delay or process occurring when the code first runs that prevents the text from being fully inserted before it is processed.
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Is there a way to summarise all bookmarks and cross references used in a Word document? | 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 |
Bookmarks & cross-references | Suchoklates | Word | 1 | 09-19-2013 02:32 AM |
Bookmarks with different figure references | Dan_68 | Word | 0 | 02-15-2010 08:29 PM |