Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-28-2024, 07:18 AM
ctviggen ctviggen is offline Addressing timing issues (doing things too fast causes a problem) Windows 10 Addressing timing issues (doing things too fast causes a problem) Office 2016
Advanced Beginner
Addressing timing issues (doing things too fast causes a problem)
 
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
  #2  
Old 03-02-2024, 09:21 PM
Guessed's Avatar
Guessed Guessed is offline Addressing timing issues (doing things too fast causes a problem) Windows 10 Addressing timing issues (doing things too fast causes a problem) Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Have you tried adding the doevents into your code whenever a step needs to be completed beforehand?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 03-03-2024, 03:16 AM
Italophile Italophile is offline Addressing timing issues (doing things too fast causes a problem) Windows 11 Addressing timing issues (doing things too fast causes a problem) Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

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.
Reply With Quote
  #4  
Old 03-03-2024, 03:35 AM
Guessed's Avatar
Guessed Guessed is offline Addressing timing issues (doing things too fast causes a problem) Windows 10 Addressing timing issues (doing things too fast causes a problem) Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Addressing timing issues (doing things too fast causes a problem) 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 timing issues (doing things too fast causes a problem) 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

Other Forums: Access Forums

All times are GMT -7. The time now is 09:52 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft