简体   繁体   English

将Docx复制到剪贴板C#

[英]Copy Docx To Clipboard C#

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. 我正在通过第三方工具创建动态.docx文件。

What I need to do: 我需要做什么:

  1. Open each file 打开每个文件
  2. Select all the contents 选择所有内容
  3. Copy the contents to the Windows clipboard 将内容复制到Windows剪贴板

Creating a Word macro is a synch since these two lines will do select all the contents and copy the contents for me. 创建Word宏是同步的,因为这两行会选择所有内容并为我复制内容。

Selection.WholeStory
Selection.Copy

Does anyone know how to convert these two lines to equivalent C# code? 有谁知道如何将这两行转换为等效的C#代码? 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. 我不确定运行外部宏是否适合我的情况,因为某些使用整个工具链的用户(包括将数据临时复制到剪贴板)将不允许运行Office宏-公司政策。

With C# I attempted the following. 使用C#,我尝试了以下操作。 What I am doing is opening up a .docx file, selecting the body contents and attempting to copy it to the Windows clipboard. 我正在做的是打开.docx文件,选择正文内容,然后尝试将其复制到Windows剪贴板。 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. 我将内容复制到Windows剪贴板的原因是该工具链中的另一个工具,即接收者。 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. 我知道剪贴板中的文本,但是我无法使用它,因为将要复制的某些数据将是Word表,图片,图形,附加内容-基本上它将是动态的。

Any insights, tips, point to documentation would be appreciative. 任何对文档的见解,技巧,指向将是有益的。

Using your Clipboard.SetDataObject(body, true, 5, 200); 使用您的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. 但是,这是InnerText,它只是文本,不包含任何格式。 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. 您也可以将其作为XML文档,并使用Xpath查询遍历文档的元素,以获取所需的细节,如KyleM在本文中所述。 How to extract text from MS office documents in C# 如何从C#中的MS Office文档中提取文本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM