[HELP] My macro doen's work at first try!

A

Andr? Palmela

Hello
I have one Excel macro that doesn't work very well the first time i
execute it next to switch on the PC. The second, third, etc.. time i
run it, it works fine!
My macro first runs a program called "Windmill" that will comunicate
with Excel with DDE.
I give a pause in my macro to give time to Windmill to load. The
problem is that the first time i execute the macro it only gives the
command to execute windmill after the time passed.
I already tried to increase the pause time but the problem remains. I
already tried two diferent tipes of pause and nothing...
what can i do ?
The second time it works fine.... :(
thank's in avvance
regards
André Palmela

-------------------------------------------------------------------------------
Public ExitLoop As Boolean
Public linha As Integer
Public linha_inicial As Integer
Public linha_final As Integer

Private Declare Function Beep Lib "kernel32" (ByVal dwFreq As Long,
ByVal dwDuration As Long) As Long

Public Declare Function GetTickCount Lib "kernel32" () As Long

Public Function Pause(Value As Long)
'Value = Valor da pausa em milisegundos
Dim PreTicks As Long
PreTicks = GetTickCount
Do
DoEvents
If GetTickCount >= PreTicks + Value Then Exit Do
Loop
End Function

Public Function COUNTER()
Dim count As Integer
count = 0
linha = linha_inicial
Do While linha <= linha_final
DoEvents
If Cells(linha, 3).Value <> "" Then count = count + 1
linha = linha + 1
Loop
If count = 10 Then
COUNTER = True
Exit Function
Else
COUNTER = False
Exit Function
End If
End Function

Sub START()

linha_inicial = 34
linha_final = 43

'**********************************************************
On Error Resume Next
Application.StatusBar = "System Loading ..."

' the problem is here! :)
Shell "C:\WINDMILL\WMDDE.EXE AUTO.wdp", vbMinimizedNoFocus
Pause 2000
'DoEvents
'Application.Wait Now + TimeValue("00:00:2")

ExitLoop = False
linha = linha_inicial
count = 0

Beep 400, 200
Beep 500, 200
Beep 600, 200

Application.StatusBar = "System Running ..."

Do While ExitLoop = False

Do While Cells(linha, 2).Value = "" And ExitLoop = False
Pause 500
ddechan = Excel.DDEInitiate("Windmill", "data")
myData = Excel.DDERequest(ddechan, "Massa")
Cells(33, 9).Value = myData
If Cells(33, 9).Value <> "READ failed" Then
Cells(linha, 2).Value = Cells(33, 9).Value
Beep 100, 500
End If
Loop

Do While Cells(linha, 3).Value = "" And ExitLoop = False
Pause 500
ddechan = Excel.DDEInitiate("Windmill", "data")
myData = Excel.DDERequest(ddechan, "Massa")
Cells(33, 9).Value = myData
If Cells(33, 9).Value <> "READ failed" Then
Cells(linha, 3).Value = Cells(33, 9).Value
Beep 2000, 500
End If
Loop

If linha = linha_final Then
Pause 500
COUNTER
If COUNTER = True Then
Beep 1500, 1000
Beep 1000, 1000
Do While COUNTER = True And ExitLoop = False
DoEvents
COUNTER
Loop
End If
linha = linha_inicial - 1
End If
linha = linha + 1
Loop

'**********************************************************
Application.StatusBar = "System Closing ..."

ddechan = DDEInitiate("Windmill", "Commands")
DDEExecute ddechan, "Destroy"
Excel.DDETerminate (ddechan)
Cells(33, 9).Value = ""

Pause 100

AppActivate "IML Device 0", False
SendKeys "%{F4}~", False
DoEvents

Pause 100

AppActivate "IML Device 1", False
SendKeys "%{F4}~", False
DoEvents

Beep 600, 200
Beep 500, 200
Beep 400, 200

Application.StatusBar = "System Terminated"

End Sub

Sub Destroy()
ExitLoop = True
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top