从Word文件复制文本到新的Word文件

9
我正在从Word文件中阅读文本,并替换一些已读文本中的文本。
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文件(新文本)中。

我尝试过

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

这会打开一个新的 Word 文件,该文件在文件系统中不被实际保存。但是我不确定如何将文本值传递到这个新文件中。
更新:
运行以上代码后,我尝试了:
 var wordApp = new Microsoft.Office.Interop.Word.Application();            
 var doc = wordApp.ActiveDocument;

出现以下提示:"此命令无法使用,因为没有打开文档。"


你看过这个吗:https://dev59.com/sVTTa4cB1Zd3GeqPvs25 - MUG4N
@MUG4N:我在这里看到了类似的东西(http://pastebin.com/1sV8es7b),但是我不确定什么是`worddocpromo`。没有解释。 - huMpty duMpty
@huMptyduMpty,你应该依靠Word Interop来完成这件事,而不是Process.Start。创建一个新的Word文档,设置内容,将其保存到另一个位置,然后使用Process.Start或其他方式从那里打开它。别忘了正确释放COM对象。 - nawfal
2个回答

5
你只需要做这个:
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);
        }
    }
}

4

这是一个简单的示例,它可以将一个Word文档中的所有文本和格式复制到一个新文档中。在新文档中,使用Word的“查找和替换”功能来替换文本:

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);
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接