Hi Jaafar.
Opens calculator, removes its titlebar, makes it a child window of a userform, and therefore, offers you control over how it closes. The results of the calculator will be posted into cell a1 in the attached example.
It needs a bit more work but I am out of time. Please post any improvements that you make as I would like to see them. I could not get the LockWindowUpdate to control the screen drawing. See if you can solve that. The example works.
ShellCalcGetResults.zip
In a userform named "CustomCalc"
<table width="100%" border="1" bgcolor="White" style="filter
rogid:DXImageTransform.Microsoft.Gradient(endColorstr='#C0CFE2', startColorstr='#FFFFFF', gradientType='0');"><tr><TD><font size="2" face=Courier New> <font color="#0000A0">Option</font> <font color="#0000A0">Explicit</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> GetDesktopWindow <font color="#0000A0">Lib</font> "user32" () <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> SetParent <font color="#0000A0">Lib</font> "user32" (ByVal hWndChild <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> hWndNewParent <font color="#0000A0">As</font> Long) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> MoveWindow <font color="#0000A0">Lib</font> "user32" (ByVal hwnd <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> x <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> y <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> nWidth <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> nHeight <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> bRepaint <font color="#0000A0">As</font> Long) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> FindWindow <font color="#0000A0">Lib</font> "user32" <font color="#0000A0">Alias</font> "FindWindowA" (ByVal lpClassName <font color="#0000A0">As</font> String, <font color="#0000A0">ByVal</font> lpWindowName <font color="#0000A0">As</font> String) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> SetWindowText <font color="#0000A0">Lib</font> "user32" <font color="#0000A0">Alias</font> "SetWindowTextA" (ByVal hwnd <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> lpString <font color="#0000A0">As</font> String) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> SendMessage <font color="#0000A0">Lib</font> "user32" <font color="#0000A0">Alias</font> "SendMessageA" (ByVal hwnd <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> wMsg <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> wParam <font color="#0000A0">As</font> Long, lParam <font color="#0000A0">As</font> Any) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> SetWindowLong <font color="#0000A0">Lib</font> "user32" <font color="#0000A0">Alias</font> "SetWindowLongA" (ByVal hwnd <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> nIndex <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> dwNewLong <font color="#0000A0">As</font> Long) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> SetForegroundWindow <font color="#0000A0">Lib</font> "user32" (ByVal hwnd <font color="#0000A0">As</font> Long) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> LockWindowUpdate <font color="#0000A0">Lib</font> "user32" (ByVal hwndLock <font color="#0000A0">As</font> Long) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> GetWindowLong <font color="#0000A0">Lib</font> "user32" <font color="#0000A0">Alias</font> "GetWindowLongA" (ByVal hwnd <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> nIndex <font color="#0000A0">As</font> Long) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> SetWindowPos <font color="#0000A0">Lib</font> "user32" (ByVal hwnd <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> hWndInsertAfter <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> x <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> y <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> cx <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> cy <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> wFlags <font color="#0000A0">As</font> Long) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> GetWindowRect <font color="#0000A0">Lib</font> "user32" (ByVal hwnd <font color="#0000A0">As</font> Long, lpRect <font color="#0000A0">As</font> RECT) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> FindWindowEx <font color="#0000A0">Lib</font> "user32" <font color="#0000A0">Alias</font> "FindWindowExA" (ByVal hWnd1 <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> hWnd2 <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> lpsz1 <font color="#0000A0">As</font> String, <font color="#0000A0">ByVal</font> lpsz2 <font color="#0000A0">As</font> String) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> GetWindowTextLength <font color="#0000A0">Lib</font> "user32" <font color="#0000A0">Alias</font> "GetWindowTextLengthA" (ByVal hwnd <font color="#0000A0">As</font> Long) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Const</font> WM_GETTEXT = &HD
<font color="#0000A0">Private</font> <font color="#0000A0">Const</font> WM_GETTEXTLENGTH = &HE
<font color="#0000A0">Private</font> <font color="#0000A0">Const</font> GWL_STYLE = (-16)
<font color="#0000A0">Private</font> <font color="#0000A0">Const</font> WS_CAPTION = &HC00000
<font color="#0000A0">Private</font> <font color="#0000A0">Const</font> WS_MAXIMIZEBOX = &H10000
<font color="#0000A0">Private</font> <font color="#0000A0">Const</font> WS_MINIMIZEBOX = &H20000
<font color="#0000A0">Private</font> <font color="#0000A0">Const</font> WS_SYSMENU = &H80000
<font color="#0000A0">Private</font> <font color="#0000A0">Const</font> WM_CLOSE = &H10
<font color="#0000A0">Private</font> <font color="#0000A0">Enum</font> ESetWindowPosStyles
SWP_SHOWWINDOW = &H40
SWP_HIDEWINDOW = &H80
SWP_FRAMECHANGED = &H20
SWP_NOACTIVATE = &H10
SWP_NOCOPYBITS = &H100
SWP_NOMOVE = &H2
SWP_NOOWNERZORDER = &H200
SWP_NOREDRAW = &H8
SWP_NOREPOSITION = SWP_NOOWNERZORDER
SWP_NOSIZE = &H1
SWP_NOZORDER = &H4
SWP_DRAWFRAME = SWP_FRAMECHANGED
HWND_NOTOPMOST = -2
<font color="#0000A0">End</font> <font color="#0000A0">Enum</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Type</font> RECT
Left <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
Top <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
Right <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
Bottom <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
<font color="#0000A0">End</font> <font color="#0000A0">Type</font>
<font color="#0000A0">Private</font> CalcHwnd <font color="#0000A0">As</font> Long, PID <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
<font color="#0000A0">Private</font> pResult <font color="#0000A0">As</font> <font color="#0000A0">String</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> UserForm_Initialize()
<font color="#0000A0">Dim</font> UfHwnd <font color="#0000A0">As</font> Long, r(1) <font color="#0000A0">As</font> RECT, Style <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
LockWindowUpdate GetDesktopWindow
Shell "calc"
CalcHwnd = FindWindow(vbNullString, "Calculator")
GetWindowRect CalcHwnd, r(0)
Style = GetWindowLong(CalcHwnd, GWL_STYLE)
SetWindowText CalcHwnd, vbNullString
Style = Style <font color="#0000A0">And</font> <font color="#0000A0">Not</font> WS_SYSMENU
Style = Style <font color="#0000A0">And</font> <font color="#0000A0">Not</font> WS_MAXIMIZEBOX
Style = Style <font color="#0000A0">And</font> <font color="#0000A0">Not</font> WS_MINIMIZEBOX
Style = Style <font color="#0000A0">And</font> <font color="#0000A0">Not</font> WS_CAPTION
SetWindowLong CalcHwnd, GWL_STYLE, Style
UfHwnd = FindWindow(vbNullString, Me.Caption)
SetParent CalcHwnd, UfHwnd
MoveWindow CalcHwnd, 0, 0, r(0).Right - r(0).Left, r(0).Bottom - r(0).Top, <font color="#0000A0">True</font>
GetWindowRect UfHwnd, r(1)
MoveWindow UfHwnd, r(1).Left, r(1).Top, r(0).Right - r(0).Left, r(0).Bottom - r(0).Top, <font color="#0000A0">True</font>
LockWindowUpdate <font color="#0000A0">False</font>
Me.Repaint
<font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
<font color="#0000A0">Friend</font> <font color="#0000A0">Property</font> <font color="#0000A0">Get</font> Result() <font color="#0000A0">As</font> <font color="#0000A0">String</font>
Result = pResult
<font color="#0000A0">End</font> <font color="#0000A0">Property</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> UserForm_QueryClose(Cancel <font color="#0000A0">As</font> Integer, CloseMode <font color="#0000A0">As</font> Integer)
<font color="#0000A0">Dim</font> EditText <font color="#0000A0">As</font> String, EditHwnd <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
<font color="#0000A0">If</font> CloseMode = vbFormControlMenu <font color="#0000A0">Then</font>
Cancel = <font color="#0000A0">True</font>
EditHwnd = FindWindowEx(CalcHwnd, 0, "Edit", vbNullString)
EditText = Space(SendMessage(EditHwnd, WM_GETTEXTLENGTH, <font color="#0000A0">ByVal</font> 0, <font color="#0000A0">ByVal</font> 0))
SendMessage EditHwnd, WM_GETTEXT, <font color="#0000A0">ByVal</font> Len(EditText) + 1, <font color="#0000A0">ByVal</font> EditText
pResult = EditText
SetParent CalcHwnd, GetDesktopWindow
SetForegroundWindow CalcHwnd
SendMessage CalcHwnd, WM_CLOSE, 0&, 0&
Me.Hide
<font color="#0000A0">End</font> <font color="#0000A0">If</font>
<font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
</FONT></td></tr></table><button onclick='document.all("98200623444296").value=document.all("98200623444296").value.replace(/<br \/>\s\s/g,"");document.all("98200623444296").value=document.all("98200623444296").value.replace(/<br \/>/g,"");window.clipboardData.setData("Text",document.all("98200623444296").value);'>Copy to Clipboard</BUTTON><textarea style="position:absolute;visibility:hidden" name="98200623444296" wrap="virtual">
Option Explicit
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE
Private Const GWL_STYLE = (-16)
Private Const WS_CAPTION = &HC00000
Private Const WS_MAXIMIZEBOX = &H10000
Private Const WS_MINIMIZEBOX = &H20000
Private Const WS_SYSMENU = &H80000
Private Const WM_CLOSE = &H10
Private Enum ESetWindowPosStyles
SWP_SHOWWINDOW = &H40
SWP_HIDEWINDOW = &H80
SWP_FRAMECHANGED = &H20
SWP_NOACTIVATE = &H10
SWP_NOCOPYBITS = &H100
SWP_NOMOVE = &H2
SWP_NOOWNERZORDER = &H200
SWP_NOREDRAW = &H8
SWP_NOREPOSITION = SWP_NOOWNERZORDER
SWP_NOSIZE = &H1
SWP_NOZORDER = &H4
SWP_DRAWFRAME = SWP_FRAMECHANGED
HWND_NOTOPMOST = -2
End Enum
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private CalcHwnd As Long, PID As Long
Private pResult As String
Private Sub UserForm_Initialize()
Dim UfHwnd As Long, r(1) As RECT, Style As Long
LockWindowUpdate GetDesktopWindow
Shell "calc"
CalcHwnd = FindWindow(vbNullString, "Calculator")
GetWindowRect CalcHwnd, r(0)
Style = GetWindowLong(CalcHwnd, GWL_STYLE)
SetWindowText CalcHwnd, vbNullString
Style = Style And Not WS_SYSMENU
Style = Style And Not WS_MAXIMIZEBOX
Style = Style And Not WS_MINIMIZEBOX
Style = Style And Not WS_CAPTION
SetWindowLong CalcHwnd, GWL_STYLE, Style
UfHwnd = FindWindow(vbNullString, Me.Caption)
SetParent CalcHwnd, UfHwnd
MoveWindow CalcHwnd, 0, 0, r(0).Right - r(0).Left, r(0).Bottom - r(0).Top, True
GetWindowRect UfHwnd, r(1)
MoveWindow UfHwnd, r(1).Left, r(1).Top, r(0).Right - r(0).Left, r(0).Bottom - r(0).Top, True
LockWindowUpdate False
Me.Repaint
End Sub
Friend Property Get Result() As String
Result = pResult
End Property
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Dim EditText As String, EditHwnd As Long
If CloseMode = vbFormControlMenu Then
Cancel = True
EditHwnd = FindWindowEx(CalcHwnd, 0, "Edit", vbNullString)
EditText = Space(SendMessage(EditHwnd, WM_GETTEXTLENGTH, ByVal 0, ByVal 0))
SendMessage EditHwnd, WM_GETTEXT, ByVal Len(EditText) + 1, ByVal EditText
pResult = EditText
SetParent CalcHwnd, GetDesktopWindow
SetForegroundWindow CalcHwnd
SendMessage CalcHwnd, WM_CLOSE, 0&, 0&
Me.Hide
End If
End Sub
</textarea>
Example usage:
<table width="100%" border="1" bgcolor="White" style="filter
rogid:DXImageTransform.Microsoft.Gradient(endColorstr='#C0CFE2', startColorstr='#FFFFFF', gradientType='0');"><tr><TD><font size="2" face=Courier New> <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> CommandButton1_Click()
<font color="#0000A0">Dim</font> cc <font color="#0000A0">As</font> <font color="#0000A0">New</font> CustomCalc
cc.Show vbModal
<font color="#008000"> 'here is your result</font>
Range("a1") = cc.Result
Unload cc
<font color="#0000A0">Set</font> cc = <font color="#0000A0">Nothing</font>
<font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
</FONT></td></tr></table><button onclick='document.all("98200623629750").value=document.all("98200623629750").value.replace(/<br \/>\s\s/g,"");document.all("98200623629750").value=document.all("98200623629750").value.replace(/<br \/>/g,"");window.clipboardData.setData("Text",document.all("98200623629750").value);'>Copy to Clipboard</BUTTON><textarea style="position:absolute;visibility:hidden" name="98200623629750" wrap="virtual">
Private Sub CommandButton1_Click()
Dim cc As New CustomCalc
cc.Show vbModal
'here is your result
Range("a1") = cc.Result
Unload cc
Set cc = Nothing
End Sub</textarea>
ShellCalcGetResults.zip