View Single Post
 
Old 03-03-2024, 08:19 AM
ctviggen ctviggen is offline Windows 10 Office 2016
Advanced Beginner
 
Join Date: Feb 2021
Posts: 54
ctviggen is on a distinguished road
Default

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
One note that may or may not mean anything. The file with this code, a dotm, is stored on OneDrive so that I can modify it and test it both at home and work. I have not tried to move the file locally.
Reply With Quote