小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

VSTO 給Word添加內(nèi)容控件,以及下拉控件

 實(shí)力決定地位 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();
        }
//添加內(nèi)容控件

        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對象轉(zhuǎn)化為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;
                    //鎖定內(nèi)容
                    nativeControl.LockContents = true;
                }
            }
        }
    }
}

 

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

注意:我這個是winform用戶控件

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多