使用dsoframer 加载word 获取 word中 checkBox的属性值 和改变属性值

本文介绍如何使用dsoframer组件来操作Word文档中的checkBox控件,包括获取其属性值并进行修改,实现对checkBox状态的控制。
    public void LoadWordChecked()
        {
            if (templateType.GetN_Name(T_id, 1) == "")
            {
                MessageBox.Show(" 操作失败,没有相应的模板!"); // 判断是否有word模板
                return;
            }
            string P_path = Pathq + templateType.GetN_Name(T_id,1) + ".docx";//获取到模板路径
            File.Copy(P_path, newfilename);//复制一个模板到另一个路径中,newfilename是另一个路径值
            Object filename = newfilename;
            Object oMissing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word.Application wordapp = new Microsoft.Office.Interop.Word.Application();// word对象
            Microsoft.Office.Interop.Word.Document doc = new Document();//Document 对象
            doc = wordapp.Documents.Open(ref filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            try
            {
                DataSet ds = templateType.GetDateSet(T_id, E_xtena, 0);//业务方法,按个人需要 可有可无
                foreach (InlineShape shape in doc.InlineShapes)
                {
                    object groupName = shape.OLEFormat.Object.GetType().InvokeMember("GroupName", System.Reflection.BindingFlags.GetProperty, null, shape.OLEFormat.Object, null);//找到 checkBox分组的值

                    //找到checkBox的名字
                    object name = shape.OLEFormat.Object.GetType().InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, shape.OLEFormat.Object, null);

                    //看 checkbox是否选择
                    object value = shape.OLEFormat.Object.GetType().InvokeMember("Value", System.Reflection.BindingFlags.GetProperty, null, shape.OLEFormat.Object, null);


                    #region 勾选Checkbox


                    try
                    {                  
                        if (ds.Tables[0].Rows.Count != 0)
                        {
                            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                            {
                                if (name.ToString().Equals(ds.Tables[0].Rows[i][5].ToString()))

                                {

                                   //如果上述条件成功 那么把这个checkBox控件选中 也是就是等于true

                                    shape.OLEFormat.Object.GetType().InvokeMember("Value", System.Reflection.BindingFlags.SetProperty, null, shape.OLEFormat.Object, new object[] { "True" });
                                    break;
                                }
                                else if (ds.Tables[0].Rows[i][5].ToString().Contains(name.ToString()))

                                {

                                   // 同上

                                    shape.OLEFormat.Object.GetType().InvokeMember("Value", System.Reflection.BindingFlags.SetProperty, null, shape.OLEFormat.Object, new object[] { "True" });
                                    break;
                                }


                            }
                        }
                        else
                        {
                           
                        }
                    }
                    catch (Exception ex)
                    {
                        
                        throw ex;
                    }


                    #endregion
                }
                doc.Save();
                doc.Close();
                wordapp.Quit();
               
            }
            catch (Exception ex)
            {


                throw ex;
            }
            finally
            {
                foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("WINWORD"))
                {
                    p.Kill();
                }
            }
            
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值