1

For the last two hours I searched this great site, but couldn't figure out a solution looking at similar posts.

I'm creating dynamic .docx files from a third party tool.

What I need to do:

  1. Open each file
  2. Select all the contents
  3. Copy the contents to the Windows clipboard

Creating a Word macro is a synch since these two lines will do select all the contents and copy the contents for me.

Selection.WholeStory
Selection.Copy

Does anyone know how to convert these two lines to equivalent C# code? I am unsure if running a external macro will be ideal in my situation since some users that will use the overall tool chain that includes copying data temporarily to the clipboard will not be allowed to run Office macros - company policy.

With C# I attempted the following. What I am doing is opening up a .docx file, selecting the body contents and attempting to copy it to the Windows clipboard. I couldn't get it to work.

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;

namespace copy_docx_clipboard
{
    using System.Windows.Forms;

    internal class Program
    {
        [STAThread]
        private static void Main(string[] args)
        {
            string fileName = @"C:\out\table_42.docx";
            using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(fileName, false))
            {
                Body body = wordDocument.MainDocumentPart.Document.Body;
                Clipboard.Clear();
                Clipboard.SetDataObject(body, true, 5, 200);

                Console.WriteLine("done");
                Console.ReadKey();
            }
        }
    }
}

The reason I am copying the contents to the Windows clipboard is another tool, in this tool chain, will be the receiver. I am aware of the clipboard text but I am unable to use that since some of the data that will be copied will be Word tables, pictures, figures, excetra - basically it will be dynamic.

Any insights, tips, point to documentation would be appreciative.

  • So the tool that you are talking about can work with `WordprocessingDocument`? – Kosala W Dec 07 '15 at 02:53
  • There are several tools involved here so hopefully this is clearer. Tool A generates .DOCX files that I parse. I have no control over Tool A in anyway. I wrote Tool B that will parse content from tool A that creates N .docx file. Tool C, which my post is about, is to open a Tool B file for me to copy the contents to the clipboard to use in Tool D. Tool D can read the Windows clipboard and has a very limited API unfortunately. – practice every day Dec 07 '15 at 02:58
  • And, Tool D can't work with WordprocessingDocuments – practice every day Dec 07 '15 at 03:07
  • So what can the Tool D work with? Clip board can have anything? Do you only want the string contents to be copied to the clip board? – Kosala W Dec 07 '15 at 04:54

1 Answers1

0

Using your Clipboard.SetDataObject(body, true, 5, 200); will not work. You can set the clipboard by using

  Clipboard.SetText(body.InnerText, TextDataFormat.Unicode);

However, this is the InnerText, which is just text, and contains no formatting. You can alternatively, body as an XML document and walk through elements of the document using Xpath queries to get detail you want as described by KyleM in this post. How to extract text from MS office documents in C#

Community
  • 1
  • 1
Markus
  • 420
  • 3
  • 7