0 votes
in VBA by Expert (916 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 (3.2k 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 Expert (916 points)
+2
Thank you very much my tutor. You are really awesome

Thanks a lot for everything.

Welcome to wellsr Q&A
Ask any questions you have about VBA and Python and our community will help answer them. wellsr Q&A is the standalone question and answer platform for wellsr.com. If you have a question about one of our specific tutorials, please include a link back to the tutorial.

Getting Started
Register
VBA Cheat Sheets (On Sale Now)

Looking for something else? Hire our team directly through ourVBA Help page, instead.

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

...