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:
- Open each file
- Select all the contents
- 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.