本文介绍了从c#中查找msform上的所有控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以使用以下方式查找所有表单:
使用系统;
使用Microsoft.Office.Interop.Excel
使用Microsoft.Vbe.Interop;
使用Microsoft.Vbe.Interop.Forms;
.....
foreach(Microsoft.Vbe.Interop.VBComponent mycom in wb.VBProject.VBComponents)
{
if(mycom .Type == Editor.vbext_ComponentType.vbext_ct_MSForm)
.....
但我找不到该表单上的控件。
我觉得应该看起来像:
...
foreach(Microsoft.Vbe.Interop.Forms.Control ctrl in Microsoft.Vbe.Interop.VBComponent.Designer.Controls)
....
但控件集合无法识别。
任何想法?
此线程提供有关我面临的问题的更多信息:
解决方案
/ p>
using System;
使用Microsoft.Office.Interop.Excel
使用VBA = Microsoft.Vbe.Interop;
...
VBA.Forms.UserForm表单;
VBA.Forms.Control c;
foreach(vb.VBProject.VBComponents中的VBA.VBComponent mod)
{
//仅使用VBA Forms
if(!(mod.Designer是VBA) Forms.UserForm))继续;
form =(VBA.Forms.UserForm)mod.Designer;
for(int i = 1; i< form.Controls.Count; i ++)
{
c =(VBA.Forms.Control)form.Controls.Item(i );
...
}
}
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 :
解决方案
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);
...
}
}
这篇关于从c#中查找msform上的所有控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!