分享

VSTO 给Word添加内容控件,以及下拉控件

 实力决定地位 2016-07-21

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using wordTool = Microsoft.Office.Tools.Word;
using vsto = Microsoft.Office.Tools;

namespace WordAddIn1
{
    public partial class MyWordLeft1UserControl : UserControl
    {
        public MyWordLeft1UserControl()
        {
            InitializeComponent();
        }
//添加内容控件

        private void button1_Click(object sender, EventArgs e)
        {
            Word.Document currentDocument = Globals.ThisAddIn.Application.ActiveDocument;
           
             if (currentDocument.Paragraphs != null &&
               currentDocument.Paragraphs.Count != 0)
             {                
                 // 在第一段文字前添加一个段落 
                 currentDocument.Paragraphs[1].Range.InsertParagraphBefore(); 
                 currentDocument.Paragraphs[1].Range.Select(); 
                 // 将Interop的Document对象转化为VSTO中的Document对象 
                 wordTool.Document document = Globals.Factory.GetVstoObject(currentDocument);
                 // 添加DropDownList         
                 //wordTool.DropDownListContentControl dropdown = document.Controls.AddDropDownListContentControl(currentDocument.Paragraphs[1].Range, "MyContentControl");
                 //dropdown.PlaceholderText = "My DropdownList Test";
                 //dropdown.DropDownListEntries.Add("Test01", "01", 1);
                 //dropdown.DropDownListEntries.Add("Test02", "02", 2); 
                 //dropdown.DropDownListEntries.Add("Test03", "03", 3);

                 wordTool.RichTextContentControl d = document.Controls.AddRichTextContentControl(currentDocument.Paragraphs[1].Range, "合同管理");
               
                 d.Text = "合同文本";
                 d.Title = "合同文本";
                 //添加背景色
                 d.Range.Shading.BackgroundPatternColor = Word.WdColor.wdColorBlue;

                 //wordTool.RichTextContentControl dNO = document.Controls.AddRichTextContentControl(currentDocument.Paragraphs[1].Range, "合同编号");
                 //dNO.Text = "合同编号";
                 //dNO.Title = "合同编号";
                 ////添加背景色
                 //dNO.Range.Shading.BackgroundPatternColor = Word.WdColor.wdColorBlue;

                 //去掉背景色
                 //d.Range.Shading.BackgroundPatternColor = Word.WdColor.wdColorWhite;

                 //foreach (var ct in document.Controls.GetEnumerator)
                 //{
                 //    (wordTool.RichTextContentControl)
                 //}
             }
        }
        private System.Collections.Generic.List
         <Microsoft.Office.Tools.Word.RichTextContentControl> richTextControls;

      //遍历控件赋值
        private void button2_Click(object sender, EventArgs e)
        {
            //if (Word.ContentControls<= 0)
            //    return;

            //richTextControls = new System.Collections.Generic.List
            //    <Microsoft.Office.Tools.Word.RichTextContentControl>();
            //int count = 0;

            //foreach (Word.ContentControl nativeControl in Word.ContentControls)
            //{
            //    if (nativeControl.Type ==
            //        Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText)
            //    {
            //        count++;
            //        Microsoft.Office.Tools.Word.RichTextContentControl tempControl =
            //            this.Controls.AddRichTextContentControl(nativeControl,
            //            "VSTORichTextControl" + count.ToString());
            //        richTextControls.Add(tempControl);
            //    }
            //}
            int count = 0;
            Word.Document currentDocument = Globals.ThisAddIn.Application.ActiveDocument;
            wordTool.Document document = Globals.Factory.GetVstoObject(currentDocument);
            foreach (Word.ContentControl nativeControl in document.ContentControls)
            {
                if (nativeControl.Type == Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText)
                {
                    //count++;
                    //Microsoft.Office.Tools.Word.RichTextContentControl tempControl =
                    //    document.Controls.AddRichTextContentControl(nativeControl,
                    //    "VSTORichTextControl" + count.ToString());
                    //richTextControls.Add(tempControl);
                    //tempControl.Text = "测试" + count;
                    nativeControl.Range.Text = "123";
                  //锁定控件
                    nativeControl.LockContentControl =true;
                    //锁定内容
                    nativeControl.LockContents = true;
                }
            }
        }
    }
}

 

 微软官方:https://msdn.microsoft.com/zh-cn/library/bb386200.aspx

注意:我这个是winform用户控件

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多