#1
|
|||
|
|||
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 Thank you. |
#2
|
||||
|
||||
Have you tried adding the doevents into your code whenever a step needs to be completed beforehand?
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Timing issues are common when VBA code invokes functions that are not under the control of the host application, especially if those functions are called repeatedly in a loop.
Copy and Paste are functions of the OS. As VBA can execute more quickly than data can be transferred to, and, especially, from, the clipboard it is necessary to use DoEvents, as Andrew suggested. If the code calls another application, a delay timer may be necessary. |
#4
|
||||
|
||||
Instead of using the clipboard which can be the source of your timing issues, try something like
NewDoc.Range.FormattedText = rngSource.FormattedText This will leave the clipboard alone (more user friendly) as a potential other benefit.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
creating a named row for dynamic addressing | trevorc | Excel Programming | 8 | 07-08-2018 03:21 AM |
Need help fast! | taavidude | Outlook | 1 | 07-30-2014 01:03 AM |
Addressing families of different sizes | ryanjanus | Mail Merge | 2 | 12-17-2012 07:05 PM |
Addressing mailing envelopes | WildBill | Outlook | 0 | 12-29-2011 03:20 PM |
Outlook 2000 addressing problem | speigel | Outlook | 0 | 11-08-2006 02:06 AM |