如何通过Aspose Words获取ActiveX单选按钮的GroupName属性
获取Word ActiveX单选按钮的GroupName属性
要读取Word文档中ActiveX单选按钮的GroupName属性,你不能直接通过Forms2OleControl的顶层属性获取,需要访问其Properties集合来提取这个隐藏属性。
修改后的完整代码如下:
var controls = document.GetChildNodes(NodeType.Shape, true) .Select(node => (Shape)node) .Where(shape => shape.OleFormat?.OleControl?.IsForms2OleControl ?? false) .Select(shape => (Forms2OleControl)shape.OleFormat.OleControl); foreach (var control in controls) { switch (control.Type) { case Forms2OleControlType.OptionButton: var caption = control.Caption; var selected = control.Value; // 读取GroupName属性 string groupName = string.Empty; if (control.Properties.Contains("GroupName")) { groupName = control.Properties["GroupName"].Value.ToString(); } break; } }
关键说明
Forms2OleControl的很多配置属性(包括GroupName)都存储在Properties字典中,需要通过键名直接访问;- 加入存在性判断可以避免因控件未设置
GroupName而引发的空引用异常; - 注意原代码中枚举值写法可能存在笔误,正确枚举应为
Forms2OleControlType.OptionButton。
内容的提问来源于stack exchange,提问作者Eric Patrick
相关产品推荐
相关产品推荐

