将文本从word文件复制到新单词

时间:2012-12-20 16:21:51

标签: c# .net .net-4.0 ms-word office-interop

我正在从word文件中读取文本并从readed文本中替换一些文本。

var wordApp = new Microsoft.Office.Interop.Word.Application();
object file = path;

object nullobj = System.Reflection.Missing.Value;

var doc = wordApp.Documents.Open(ref file, ref nullobj, ref nullobj,
                                                 ref nullobj, ref nullobj, ref nullobj,
                                                 ref nullobj, ref nullobj, ref nullobj,
                                                 ref nullobj, ref nullobj, ref nullobj);
 doc.ActiveWindow.Selection.WholeStory();

doc.ActiveWindow.Selection.Copy();

IDataObject data = Clipboard.GetDataObject();
var text =data.GetData(DataFormats.Text);

所以我有来自原始word文件的文本,现在我需要它传递给一个不存在的新word文件(New Text)。

我试过

 ProcessStartInfo startInfo = new ProcessStartInfo();
 startInfo.FileName = "WINWORD.EXE";
 Process.Start(startInfo);

这会打开一个新的word文件,它没有物理保存在文件系统中,这很好。但我不确定如何将文本值传递给这个新文件。

更新

运行上面的代码后,我尝试了

 var wordApp = new Microsoft.Office.Interop.Word.Application();            
 var doc = wordApp.ActiveDocument;

提出 “此命令不可用,因为没有文档打开。”

2 个答案:

答案 0 :(得分:4)

这是一个简单的示例,它将整个文本和格式从一个Word文档复制到一个新文档。在新文档中,然后使用单词 Find&替换功能:

using System;
using System.Linq;
using Word = Microsoft.Office.Interop.Word;

namespace WordCopy
{
    class Program
    {
        static void Main(string[] args)
        {
            var fileName = args[0];

            var wordApp = new Word.Application();
            wordApp.Visible = true;
            var document = wordApp.Documents.Open(fileName);

            var newDocument = CopyToNewDocument(document);

            SearchAndReplaceEverywhere(newDocument, "this", "that");
        }

        static Word.Document CopyToNewDocument(Word.Document document)
        {
            document.StoryRanges[Word.WdStoryType.wdMainTextStory].Copy();

            var newDocument = document.Application.Documents.Add();
            newDocument.StoryRanges[Word.WdStoryType.wdMainTextStory].Paste();
            return newDocument;
        }

        static void SearchAndReplaceEverywhere(
            Word.Document document, string find, string replace)
        {
            foreach (Word.Range storyRange in document.StoryRanges)
            {
                var range = storyRange;
                while (range != null)
                {
                    SearchAndReplaceInStoryRange(range, find, replace);

                    if (range.ShapeRange.Count > 0)
                    {
                        foreach (Word.Shape shape in range.ShapeRange)
                        {
                            if (shape.TextFrame.HasText != 0)
                            {
                                SearchAndReplaceInStoryRange(
                                    shape.TextFrame.TextRange, find, replace);
                            }
                        }                        
                    }
                    range = range.NextStoryRange;
                }
            }
        }

        static void SearchAndReplaceInStoryRange(
            Word.Range range, string find, string replace)
        {
            range.Find.ClearFormatting();
            range.Find.Replacement.ClearFormatting();
            range.Find.Text = find;
            range.Find.Replacement.Text = replace;
            range.Find.Wrap = Word.WdFindWrap.wdFindContinue;
            range.Find.Execute(Replace: Word.WdReplace.wdReplaceAll);
        }
    }
}

答案 1 :(得分:4)

您需要做的就是:

using System.Runtime.InteropServices;
using MSWord = Microsoft.Office.Interop.Word;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main()
        {
            var application = new MSWord.Application();
            var originalDocument = application.Documents.Open(@"C:\whatever.docx");

            originalDocument.ActiveWindow.Selection.WholeStory();
            var originalText = originalDocument.ActiveWindow.Selection;

            var newDocument = new MSWord.Document();
            newDocument.Range().Text = originalText.Text;
            newDocument.SaveAs(@"C:\whateverelse.docx");

            originalDocument.Close(false);
            newDocument.Close();

            application.Quit();

            Marshal.ReleaseComObject(application);
        }
    }
}