Click here to Skip to main content
15,113,873 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello,

We are trying to figure out one of two possible scenarios. We need to either run this VBA macro in C# and pass the parameters received into the C# code. Or convert the vba (using DDE) to C# so that it can be run directly in the program. We are completely lost as none of us has ever programmed in C#. Any help would be greatly appreciated.


Sub ReceiveIt_1234567()

channel = DDEInitiate("Service", "Actions")
DDEExecute channel, "[SystemEvent(""ReceiveIt"",""User Name"", ""1234567"" )]"
DDETerminate channel

End Sub
Posted

1 solution

Hi,

DDE depends on windows messaging. This means you need to use in your C# code SendMessage function that you can find in win32 api.

You can import this function to your C# code using this part of code.
C#
[DllImport("user32.dll")]
           public static extern int SendMessage(
                 int hWnd,      // handle to destination window
                 uint Msg,       // message
                 long wParam,  // first message parameter
                 long lParam   // second message parameter
                 );


But of course before use this method you need get handle of the window you want to send message to. You can do it either call FindWindow function from win32 api:
C#
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string className, string windowName);


or

you can use similar code to the following one:
C#
Process[] processes = Process.GetProcessesByName("YourProcessName");
foreach (Process process in processes)
{
    IntPtr windowHandle = p.MainWindowHandle;

    //CALL SendMessage function or do something else
}


Good luck,
Alexey
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900