|
how do i slow a macro down
hello all.
i have a macro that loops through all the segments in a sketch using
intitemcount = 0
do while not strsegid(intitemcount) = ""
.................
loop
this works fine when i step the macro through using f8 but when i run the macro properly the macro alarms out with a "subscript out range" error. if i put a macro break in at "intitemcount = 0", then resume the macro, every thing works fine. it appears to me that the only explanation is vba is running too fast for solidworks to keep up (though i am sure that this is fanciful!). what can i do to get my macro running properly? is there a command in vba to force the process to wait for x milliseconds? has anyone come up against this before?
many thanks
bob
robert luck
production engineer ~ cam programmer
for icount = 1 to 1000
next icount
this doesn't have a specific time it takes to run this though, so it may not work as expected.
i haven't tried this, but found it through by googling "vba delay function"
method 2: use an api call to suspend the execution of word
use an api call to suspend the execution of word for a fixed amount of time. the kernel32 contains a function that pauses a program's execution for a specified amount of time, specified in milliseconds.
to use the function, it must first be declared in the general declarations section of the module in which it will be used:
declare sub sleep lib "kernel32" alias "sleep" (byval dwmilliseconds as long)
use the following syntax to call the sleep function:
sub sleep() sleep 1000 'implements a 1 second delay end sub
drc inc.
minneapolis, mn
.designreadycontrols.com
sw2007 sp5.0
core2 quad
3gb ram
xp pro sp2
ati firegl v3600
i think "subscript out of range" means that you are exceding the limits of the datatype of your variable.
are you sure you ever exit your loop? or try changing intitemcount to a long instead of an interger.
unless you step through your process 32k times you would never see this.
if im wrong here is a timer example
from the solidworks vba help:
timer function example
this example uses the timer function to pause the application. the example also uses doevents to yield to other processes during the pause.
dim pausetime, start, finish, totaltime
if (msgbox("press yes to pause for 5 seconds", 4)) = vbyes then
pausetime = 5 ' set duration.
start = timer ' set start time.
do while timer < start + pausetime
doevents ' yield to other processes.
loop
finish = timer ' set end time.
totaltime = finish - start ' calculate total time.
msgbox "paused for " & totaltime & " seconds"
else
end
end if
thank you for your guidance, i shall be trying the methods you descride over the weekend. i think dominic might be on the money though as after i posted my plea for help i tried erasing the array immediately before exiting the macro and so far it has worked (but i have only tested it about 10 times and with sketches up to 150 segments). but that raises another question in that do arrays exist in memory after a macro closes? i thought that for simple software like vba macros all objects created by the macro were destroyed on exit.
anyway, my thanks to you all again for helping an apprentice macro-maker.
bob luck
robert luck
production engineer ~ cam programmer
quick |
|