Why can my Form only open once per Excel session?

  • Thread starter Thread starter pinkfloydfan
  • Start date Start date
P

pinkfloydfan

Hi there

I'm still creating a Form to be used in Excel. The Form is opened via
a vba macro which calls a C# wrapper within an xll that opens the
Form, like this:

VBA:

Sub TestMacro2()
Call Application.Run("OpenForm")
End Sub

C#:

public class XLFormWrap
{
[STAThread]
public static void OpenForm()
{
XLForm Form1 = new XLForm();
Form1.ShowDialog();
}
}

What is strange is that while the first time I run the vba macro the
form opens up, any subsequent time produces nothing. I have to close
the session of Excel and reopen it to get the form to open again.

Does anyone know why this would happen please?

Many Thanks
Lloyd
 
Can you wrap in some error catching/detecting code (either in the macro
or in the call to OpenForm) to see if there is an error that is occuring?
 
Nicholas

I am very new to C#: I don't yet know how to write error catching
code...what sort of error would you be expecting?

One other interesting thing: if I just test the "OpenForm" macro
within Visual C# it can be run as many times as I like.

All rather confusing...
 
Well, you could wrap the call to open the form in a try/catch block, and
see what exception is thrown (if any).
 
Brilliant!

The problem was that I had the following line:

System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false)

and you can't use this if you are calling the macro from another
application. Ah well, that will teach me to just lift code I don't
fully understand.

Thanks
 
Back
Top