tags:

views:

469

answers:

2

I'm trying to find all controls on msform from c# using VBA extensibility interop.

I can find all forms using :

using System;
using Microsoft.Office.Interop.Excel
using  Microsoft.Vbe.Interop;
using  Microsoft.Vbe.Interop.Forms;
.....

 foreach (Microsoft.Vbe.Interop.VBComponent mycom in wb.VBProject.VBComponents)
        {

                 if (mycom.Type == Editor.vbext_ComponentType.vbext_ct_MSForm)

.....

but i can't find the controls on that form.

I think it should look something like :

....
  foreach (Microsoft.Vbe.Interop.Forms.Control ctrl in Microsoft.Vbe.Interop.VBComponent.Designer.Controls)
....

but the Controls collection is not recognized.

Any ideas?

A: 

This thread provides more information on problem I'm facing :

http://groups.google.co.uk/group/microsoft.public.dotnet.languages.csharp/browse_thread/thread/e2fe6e6b6335780e/6c17add3bfa50b4e?hl=en&ie=UTF-8&q=msform+designer+c%23#6c17add3bfa50b4e

jedd
If this solves your problem, pleas put the answer here instead of linking to it. There is no guarantee that the link is valid for all times.
Nifle
No. It doesn't solve my problem, just give another example of it.It show's how the task can be accomplished in VBA (which also works in VB.NET).I can not get it to work in c# as the 'generic' object 'Designer' does not seem to support the 'controls' collection in c#
jedd
+1  A: 

This code should work:

using System;
using Microsoft.Office.Interop.Excel
using VBA = Microsoft.Vbe.Interop;

...

VBA.Forms.UserForm form;
VBA.Forms.Control c;

foreach (VBA.VBComponent mod in wb.VBProject.VBComponents)
{
    // Use only VBA Forms
    if (!(mod.Designer is VBA.Forms.UserForm)) continue;

    form = (VBA.Forms.UserForm) mod.Designer;

    for (int i = 1; i < form.Controls.Count; i++)
    {
        c = (VBA.Forms.Control)form.Controls.Item(i);
        ...
    }
}
stfn