如何将ContentControl转换为PlainTextContentControl /访问.Text属性?

时间:2018-08-15 00:17:52

标签: c# ms-word vsto office-interop

我试图用C#以编程方式在MS Word文档中填充一些ContentControl。

到目前为止,我已经能够打开文档并找到所有控件,但是它们以通用ContentControl对象的形式返回。使用调试器对其进行检查只会发现一个通用的System.__ComObject

the docs中我可以看到某些控件应该具有.Text属性,但是我不知道如何访问它。

我可以使用下面看到的switch语句来确定控件的类型,但这并不能真正帮助我-我不知道将对象强制转换到哪个类(如果那是我应该的)要做)。

有一个 类别,名为PlainTextContentControl,但它存在于Microsoft.Office.Tools.Word中,但是ApplicationDocumentContentControl仍然存在在Microsoft.Office.Interop.Word下,它们不能很好地配合使用。

所以我迷路了。如何访问Text属性?这就是我所拥有的:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Word;
//using Microsoft.Office.Interop.Word;
using Microsoft.Office.Tools.Word;
using ContentControl = Microsoft.Office.Interop.Word.ContentControl;
using Document = Microsoft.Office.Interop.Word.Document;


namespace ConsoleApplication1
{
    internal class Program
    {
        public static void Main(string[] args)
        {

            Console.WriteLine("Opening Word Application...");
            var app = new Application();
            try
            {
                Console.WriteLine("Loading document...");
                var doc = app.Documents.Open(
                    @"C:\blahblah\template3.docx");
                Console.WriteLine("Finding controls...");
                var controls = GetAllContentControls(doc);
                foreach (var control in controls)
                {

                    Console.WriteLine(control.Tag);

                    switch (control.Type)
                    {
                        case WdContentControlType.wdContentControlText:
                            var pt = control as PlainTextContentControl;
                            Console.WriteLine("hit"); // pt is null
                            break;
                    }

                }

                doc.Close();
            }
            finally
            {
                app.Quit();
            }


        }


        public static List<ContentControl> GetAllContentControls(Document wordDocument)
        {
            var ccList = new List<ContentControl>();
            foreach (Range range in wordDocument.StoryRanges)
            {
                var rangeStory = range;
                do
                {
                    try
                    {
                        foreach (ContentControl cc in rangeStory.ContentControls)
                        {
                            ccList.Add(cc);
                        }
                    }
                    catch (COMException)
                    {
                    }

                    rangeStory = rangeStory.NextStoryRange;
                } while (rangeStory != null);
            }

            return ccList;
        }
    }
}

我应该注意,我使用的是JetBrains Rider而不是Visual Studio。如果由于某种原因无法使用Rider,我可能可以获得VS的副本。

1 个答案:

答案 0 :(得分:2)

您可以只使用类似于以下代码:

switch (control.Type)
   {
     case WdContentControlType.wdContentControlText:
       var text = control.Range.Text;
       //var pt = control as PlainTextContentControl;// pt is null
       Console.WriteLine(text); 
      break;
     case WdContentControlType.wdContentControlRichText:
        var richText = control.Range.Text;
        //var pt1 = control as PlainTextContentControl;// pt1 is null
        Console.WriteLine(richText); 
        break;
      }