我正在尝试列出Microsoft Word“Shape”对象的所有运行时属性。我首先使用QueryInterface获取com对象的类型,然后调用GetProperties()。当我在控制台应用程序中使用它时,这很好用,我将COM对象类型作为“Shape”并返回70个属性。当我点击winform按钮后面的相同代码时,我只获得1个属性。是否有任何具体原因可以通过这种方式或任何其他方式实现这一目标。
Microsoft.Office.Interop.Word.Shape tbx = wordApp.ActiveDocument.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal,
leftPosition + 5, topPosition + 5, 100, 50);
Type objType = tbx.GetType();
if (objType.IsCOMObject)
{
objType = GetTypeForComObject(tbx);
}
PropertyInfo[] properties2 = objType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
private Type GetTypeForComObject(object wordComObject)
{
IntPtr iunkwn = System.Runtime.InteropServices.Marshal.GetIUnknownForObject(wordComObject);
System.Reflection.Assembly wordAssembly = System.Reflection.Assembly.GetAssembly(typeof(Word.Application));
Type[] wordTypes = wordAssembly.GetTypes();
foreach (Type currType in wordTypes)
{
Guid iid = currType.GUID;
if (!currType.IsInterface || iid == Guid.Empty)
{
continue;
}
IntPtr ipointer = IntPtr.Zero;
System.Runtime.InteropServices.Marshal.QueryInterface(iunkwn, ref iid, out ipointer);
if (ipointer != IntPtr.Zero)
{
// yeah, that’s the one we’re after
return currType;
}
}
return null;
}
我也试过使用GetFields()然后调用Invoke()也没用。将这项工作放在后台工作线程中也没有帮助。