jedd
jedd

Reputation: 420

Find all controls on msform from c#

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?


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

Upvotes: 1

Views: 2587

Answers (1)

user248871
user248871

Reputation:

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);
        ...
    }
}

Upvotes: 4

Related Questions