0 votes
in VBA by Skilled (673 points)

Hello everyone

I am trying to load the calculator using such a code

Public vPID As Variant

Public Sub OpenApplication()
Dim i As Long

    'Launch application if not already open
    If vPID = 0 Then 'Application not already open
101:
        vPID = Shell("C:\Windows\system32\Calc1.exe", vbNormalFocus)
    Else 'Application already open so reactivate
        On Error GoTo 101
        AppActivate (vPID)
        

    End If
    
            For i = 1 To 100    ' Set up counting loop.
    SendKeys i & "{+}", True    ' Send keystrokes to Calculator
Next i    ' to add each value of I.
SendKeys "=", True    ' Get grand total.
SendKeys "%{F4}", True    ' Send ALT+F4 to close Calculator.

End Sub

SendKeys doesn't work on the calc app .. 

When run the code and the worksheet is active it is written to cell .. and when in VBE it is written inside the code

Any help in this topic please?

 

1 Answer

+1 vote
by Super Expert (2.7k points)
selected by
 
Best answer

The macro is writing to the cell because it takes a while for the calculator app to activate and the macro is progressing too fast. Try adding a delay after activating the calculator app using VBA Shell or AppActivate and before sending the keystrokes using SendKeys. The VBA Sleep function and the Application.Wait method are both great tools for adding the delay. Here's an amended macro demonstrating use of the Sleep function after activating the calculator application:

#If VBA7 Then
    Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64-Bit versions of Excel
#Else
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'For 32-Bit versions of Excel
#End If
Public vPID As Variant

Public Sub OpenApplication()
Dim i As Long

    'Launch application if not already open
    If vPID = 0 Then 'Application not already open
101:
        vPID = Shell("C:\Windows\system32\Calc.exe", vbNormalFocus) 'change to Calc1.exe if you want
        Sleep 3000
    Else 'Application already open so reactivate
        On Error GoTo 101
        AppActivate (vPID)
        Sleep 3000
    End If
    
            For i = 1 To 100    ' Set up counting loop.
    SendKeys i & "{+}", True    ' Send keystrokes to Calculator
Next i    ' to add each value of I.
SendKeys "=", True    ' Get grand total.
Sleep 1000
SendKeys "%{F4}", True    ' Send ALT+F4 to close Calculator.

End Sub

 

by Skilled (673 points)
+1
Thank you very much my tutor. You are really awesome

Thanks a lot for everything.

Welcome to wellsr Q&A
wellsr Q&A is the VBA and Python programming community that rewards you for learning how to code.

Getting Started
Register
VBA Cheat Sheets (On Sale Now)

Earn free prizes for asking VBA and Python questions and for answering questions asked by others in our community.

What makes us different?
Our points system rewards you with a chance for free gifts based on the quality of your questions and answers. All you have to do is post and you could get rewarded, like these members:

Hightree $10 Amazon Gift Card
Thales1 $10 Amazon Gift Card
runfunke $10 Amazon Gift Card
coolag $10 Amazon Gift Card
Siew Hun $10 Amazon Gift Card

So, why don't you join us? It really is an encouraging way to motivate members in our VBA and Python community.

Register

For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.

...