Word Interop Quickparts/BuildingBlock/Template to RichText

JParrish

New member
Joined
Apr 5, 2019
Messages
1
Programming Experience
10+
I am struggling to get the rich text out of the building blocks that represent the templates that users have available to them. This is what I mean by "QuickParts". They are sometimes called "Autotext Entries" and the Word Interop library calls them Building Blocks.

520


Below is a file I put together to read and parse a the list of all quickparts. It works great, but in the ToClipboard method towards the bottom, that works, but it does not include formatting. If I use this library to post any of the template values to the clipboard and then paste it into word, all of the regular text is included but no formatting (no bold, italic, font description, size, nothing). So it is truly just the string content with the formatting stripped away.

Is there any way to get the rich text onto the clipboard? Am I using the wrong data construct? am I approaching the problem wrong? I want to give the user the functionality to choose from a list of templates and arbitrarily stitch them together to basically build a complete document, but not having formatting is a big dealbreaker.

QuickpartFactory.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Word;



namespace QuickpartsDocbuilderLib
{
  public class QuickpartFactory
  {

        public List<Quickpart> GetQuickparts()
        {
            var bbList = new List<Quickpart>();
            var word = new Application();
            foreach (Template template in word.Templates)
            {
                var num = template.AutoTextEntries.GetEnumerator();
                foreach (WdBuildingBlockTypes type in Enum.GetValues(typeof(WdBuildingBlockTypes)))
                {
                    var bbType = template.BuildingBlockTypes.Item(type);
                    for (var i = 1; i <= bbType.Categories.Count; i++)
                    {
                        var blocks = bbType.Categories.Item(i).BuildingBlocks;
                        for(var j = 1; j <= blocks.Count; j++) bbList.Add(new Quickpart(blocks.Item(j)));
                    }
                }
            }
            return bbList;           
        }

        public List<string> GetQuickpartNames()
        {
            List<string> names = new List<string>();
            foreach(var entry in GetQuickparts().ToArray())
            {
                names.Add(entry.Name);
            }
            return names;   
        }
    }

    public class Quickpart
    {
        private BuildingBlock block;
        public string Name
        {
            get
            {
                return block.Name;
            }
        }

        public string ID
        {
            get
            {
                return block.ID;
            }
        }

        public string Value
        {
            get
            {
                return block.Value;
            }
        }

        public Quickpart(BuildingBlock block)
        {
            this.block = block;
        }

        public void ToClipboard()
        {
            System.Windows.Forms.Clipboard.SetDataObject(block);
        }
    }
}
 
Back
Top Bottom