View Single Post
 
Old 02-28-2024, 07:18 AM
ctviggen ctviggen is offline Windows 10 Office 2016
Advanced Beginner
 
Join Date: Feb 2021
Posts: 54
ctviggen is on a distinguished road
Default Addressing timing issues (doing things too fast causes a problem)

I have some timing issues in different subroutines.


Timing issue #1. This subroutine (1) adds text, pastes copied text, manipulates the pasted text; (2) performs (1) with different added text and manipulation; (3) performs (1) with different added text and manipulation... When I tested this manually by stepping through the code, it worked fine. When I ran it normally, it threw errors. So, before I pasted the copied text, I added a subroutine that did nothing but delay for a second. I run the delay right before every instance of pasting text. This corrects the error but creates a "choppy" UI, as this is seen by a user: something happens; it stops; something happens; it stops; etc.



Timing issue #2. For this subroutine, I copy text from a currently open document, set an insertion point in that document, open a new document, paste the copied text there, then immediately begin manipulating hidden bookmarks and their corresponding cross-references. After other manipulation, the manipulated text gets copied back to the insertion point in the currently open document and the new document gets closed. I tested this on different computers with the same Word file, and sometimes the manipulation of bookmarks/cross-references works, and sometimes it doesn't. Again, I think there's a timing issue. I think adding a delay before beginning the manipulation of bookmarks/cross-references would work. However, this would make this code run longer (the user does not see the manipulation here, though).



This is the code I'm using for a delay (not my code, someone else created it):


Code:
Private Sub TIMERUN(SEC As Single)
    PauseTime = SEC ' Set duration.
    Start = Timer ' Set start time.
    Do While Timer < Start + PauseTime
        DoEvents ' Yield to other processes.
    Loop
End Sub
But is there a better way to address these timing issues other than adding a simple delay?



Thank you.
Reply With Quote