mpen
mpen

Reputation: 282825

How to convert ContentControl into PlainTextContentControl/access the .Text property?

I'm trying to programatically fill in some ContentControls inside a MS Word document with C#.

So far I've been able to open the document and find all the controls, but they come back as generic ContentControl objects. Inspecting them with a debugger just reveals a generic System.__ComObject.

From the docs I can see that some of the controls should have a .Text property, but I cannot figure out how to access it.

I can determine the type of the control using the switch statement you see below, but it doesn't really help me -- I don't know what class to cast the object to (if that's even what I'm supposed to do).

There is a class called PlainTextContentControl but it exists in Microsoft.Office.Tools.Word, but the Application and Document and ContentControl live under Microsoft.Office.Interop.Word and these do not play nicely together.

So I'm lost. How do I access the Text property? Here's what I've got:

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;
        }
    }
}

I should note that I'm using JetBrains Rider instead of Visual Studio. If this is impossible to do with Rider for some reason, I can probably obtain a copy of VS.

Upvotes: 0

Views: 634

Answers (1)

Seiya Su
Seiya Su

Reputation: 1874

You can just use the code resemble the following:

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;
      }

Upvotes: 1

Related Questions