Reputation: 420
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 :
Upvotes: 1
Views: 2587
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