Launch the .Net Win Forms from Excel spreadsheet
Actually first I am mentioning some details about my application:
1. It is a Windows Application written in C#.
2. It is an Trading application that has one Main screen (Its a normal
Window Form not MDI Form) from where we can launch Trade Specific screens by
clicking on New and Edit button.
3. We can launch these Trade specific screens from Excel sheet as well but
the precondition for this is that Main screen of Trading application must be
open by that time.
Now the Problem is that:
If any Trade specific Screen is already open then it doesn't open the new
Trade specific Screen from Excel sheet. Actually I have written the following
Snippet of code to open the screen:
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam,
public bool UploadToTradeScreen( string tradeID, bool saveDebugFile )
Process myProcesses = Process.GetProcessesByName("TradeApp");
if(myProcesses.Length == 0)
XmlDocument xmlDoc = FetchXMLDoc( tradeID );
//Other Logic ...
IntPtr ret = SendMessage(myProcesses.MainWindowHandle,
WM_USER_TradeApp_OPENTRADE, iNow, (IntPtr)0 );
int iRet = ret.ToInt32( );
if( saveDebugFile == false )
File.Delete( fileName );
return iRet == 0 ? false : true;
and I am trapping this Window Message in the following code snippet to open
protected override void WndProc(ref Message m)
case ((int) WindowsMessages.WM_USER_TradeApp_OPENTRADE):
StaticHelperClass.FrameWorkHelper.AppHelper.Displa yTradeData( fileName );
case ((int) WindowsMessages.WM_USER_TradeApp_SAVETRADE):
StaticHelperClass.FrameWorkHelper.AppHelper.SaveTr adeData( fileName );
When the Main screen opens and very first time we launch Trade Specific Screen from Excelsheet then it opens the Trade Specific Screen. But next time it doesn't open the new Trade Specific screen
from Excel sheet.
My guess about this problem is that when we open any Trade Specific Screen then the MainWindowHandle transfers to the Trade Specific Screen from the Main Screen.
Could you please suggest me some solutions to overcome this problem, so that I can open the multiple Trade specific screens from the Excel sheet.