Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I pace a macro? 2

Status
Not open for further replies.

Pimafix

Technical User
Sep 6, 2006
14
US
I am running a macro from excel that goes into Extra. I tried to use

Do while Sess0.Screen.OIA.Xstatus <> 0
Doevents
Loop

But Extra runs the macro so fast it won't recognize a wait string.
I tried using Do While and Do Unitl loops but then the macro will sometimes skip a step and screw up. Most recently I have started to use If <> variable then goto Start. Clunky and still doesn't catch everything.

For example there is one screen where the macro will not enter a new screen name unless a pause is built in. Without the pause it skips the new screen name and just hits enter which goes to the wrong screen.

In thread 1-1261727 there is discussion on how to run code based off of Do While Trim(Sess0.Screen.GetString(24,2,7))= "" or Do While Trim(Sess0.Screen.GetString(24,2,7))<> "DESIRED". Given the discussion that works when the system is slower, but how do I get the macro to function properly when it runs faster then Extra will recognize?

I am really grasping at straws here and haven't been able to find anything to help out. A friend of mine suggested using the host settle time but that doesn't always work because A) I can't get it to hook into my excel and B) sometimes the screens change slower then the 30 second time out.

Right now the only method I have found that works to slow the macro down enough for it to function in Extra is the following:

Private Sub Timeout()
On Error Resume Next

Sess0.WindowState = 0
'minimizes Extra so Excel can be used

Application.Wait Now + TimeValue("00:00:02")
'Should make macro pause for 2 seconds


Sess0.WindowState = 2
'Maximizes Extra and continues

End Sub

This is called as a pause is needed but it won't and doesn't slow the macro down by 2 seconds. I used to use window states 4 and 1 however there are constant error codes when I run that and the resume next doesn't do anything.

Any ideas on how to solve this items?



 
When you do something in Extra you should know what is coming. You then should tell Extra to wait for that to happen.

This code:
Do while Sess0.Screen.OIA.Xstatus <> 0
Doevents
Loop
Will wait for the "X Clock" to disappear from Extra. Obviously in your system it's possible for the XClock to disappear before the data comes back.

For each screen change or query you need to determine how the user knows that Extra is done, then tell Extra the same thing.

In the thread you quoted, the user knew that when Extra was done he could see "DESIRED" at 24,2. So you put the system in a loop to wait for that string like this:

Do While Trim(Sess0.Screen.GetString(24,2,7))<> "DESIRED"
doevents 'Frees up the system for a split second for other programs to operate
Loop

Now Extra will not leave this loop until it sees "DESIRED" at 24,2, then it will continue.

You must do this same thing everytime you change the screen.

Hope this helps,
calculus

 
The g_HostSettleTime is only considered when it's called by:
waithostquiet(g_HostSettleTime).

FYI, it's a terible way to wait for screen changes.

calculus
 
Hi calculus,

Sometimes when I copy data from Extra and paste to Excel with a macro, the data gets lost.
I usually do a PF1 to find my record, then continuously PF2 to page foward for additional data until there are no more pages.
As the data is copied from each page to Excel, sometimes the PF2 is evoked before all the data from the prior page is copied into Excel.
If I put WaitHostQuiet(500) before the PF2 is sent, that usually gives the system just enough time to complete the paste.
At the office, I have an XP running Excel 2000 and NT with Excel 97. If I omit WaitHostQuiet (500) when I use the NT/Excel 97, I would experience loss of data. I have less of a problem with XP.

but you are correct that it's a terrible way to wait for screen changes

just my experience...
zach
 
The g_HostSettleTime is only considered when it's called by:
waithostquiet(g_HostSettleTime).

True, but you can change the system's TimeOutValue to make any WaitFor take longer.

WaitForString Method

Description

Waits until the specified text appears on the screen. The Screen object will wait for the amount of time set in System.TimeoutValue.

Code:
Dim Sys As Object, Sess As Object, MyScreen As Object
Set Sys = CreateObject("EXTRA.System")
Sys.TimeoutValue = 120000
Set Sess = Sys.ActiveSession
Set MyScreen = Sess.Screen
WaitingForIt = MyScreen.WaitForString("WHATEVER")

Also, if you're going to use a Do While loop it's a good idea to either use a counter or the system time to prevent an infinite loop.

Code:
Function WaitForMyString(strString, intRow, intCol, intMin)
  dtmHour = Hour(Now)
  dtmMin = Minute(Now) + intMin
  While dtmMin > 59 Then
    dtmHour = dtmHour + 1
    dtmMin = dtmMin - 60
  Wend
  dtmWaitUntil = TimeSerial(dtmHour, dtmMin, Second(Now))

  Do While Sess0.Screen.GetString(intRow, intCol, Len(strString)) <> strString
  DoEvents
    If dtmWaitUntil <= TimeSerial(Hour(Now), Minute(Now), Second(Now)) Then
      MsgBox "Wait Time Elapsed"
      WaitForMyString = 0
      Exit Function
    End If
  Loop
  WaitForMyString = -1
End Function

You'd need to make a couple changes in the function if the people using the macro work later than midnight.
 
So far I haven't been able to get host settle time to work from Excel. It won't recognize the library or the code depending on what going on.

While I understand that the way I go about changing screens is scary, I have only found two ways to get the macro to work for screen changes.

First - I do know what will appear where on my screen and can use wait strings, however that still doesn't solve the problem of the macro going faster then the system. I used Do while and Loop until to get screen names and data to get in place.

Second - most recently I have changed the code to repeat a putstring 3 times before a screen change so the macro won't go over the new screen/file and use the Do While after a wait string populates on the bottom right of the new screen. This has increased speed and removed pauses, but it still means extra code because I can't get the new screen names to stick without a loop or several repeats of the code before the sendkey command hits enter.

Skie,
Thanks for pointing out the system time out info. I think if that gets bumped up it will solve my problems of timing out.

My program doesn't use human interaction once it starts in Excel. The macro fills in screens and collects data from Extra based on whats already in the system.

 
Oh ya, one more thing.

Calculus,
I tried the Do While Trim(Sess0.Screen.GetString(24,2,7))<> "DESIRED" way of doing things but the system would sit and do nothing for about 30 seconds at a time when ever the program went in and out of the main menu. Since this happens 4 or 5 times a file it really eats up time. Especially when you consider that it only takes 3-5 minutes to manually do the job.

This is why I am now putting in three identical put strings in a row. It solves the time problem and ensures accuracy at the same time. When I let the Do While run unchecked in the main menu, it won't do anything but sit and wait. When I used send keys to erase the field before starting the loop it still would sit blank for about 30 seconds. Before going on.

Still stumped on this one.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top