You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 15:35:11