Interacting with document which called the exe file

Hi,

I have two CorelDRAW 2017 files in separate windows. I have following VBA in GMS format:


#If VBA7 Then
Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As LongPtr, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As LongPtr) As LongPtr

#Else
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

#End If
Sub corel_interact()
Pth = "C:\test\"
fleEXE= "corelInteract.exe"

ShellExecute 0, "Open", fleEXE, "", Pth, 1
End Sub

This code runs an executable file created by VisualStudio.  It is a windows form application with a button on it that creates a rectangle around the selected shape.


namespace corelInteract
{
public partial class Form1 : Form
{
private corel.Application corelApp;
public Form1()
{
Type cdrType = Type.GetTypeFromProgID("CorelDRAW.Application.19");
corelApp = (corel.Application)Activator.CreateInstance(cdrType);
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
if (corelApp.ActiveDocument.SelectionRange.Count > 0)
{
double xx, yy, width, height;
corelApp.ActiveDocument.ActiveShape.GetBoundingBox(out xx, out yy, out width, out height);
corelApp.ActiveDocument.ActiveLayer.CreateRectangle2(xx,yy,width,height);
corelApp.ActiveDocument.ClearSelection();
}
}
}
}if (corelApp.ActiveDocument.SelectionRange.Count > 0)

{
double xx, yy, width, height;
corelApp.ActiveDocument.ActiveShape.GetBoundingBox(out xx, out yy, out width, out height);
corelApp.ActiveDocument.ActiveLayer.CreateRectangle2(xx,yy,width,height);
corelApp.ActiveDocument.ClearSelection();
}
}
}
}

There are two issues:

1. The rectangle is created in the first window even if the macro is run in the second window.

2. The form goes below CorelDraw if I click the CorelDraw window.

Thanks and Kind Regards.