从Word文件复制文本到一个新词 [英] Copy text from word file to a new word

查看:293
本文介绍了从Word文件复制文本到一个新词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读的字文件中的文本并替换从readed文本一些文本。

I am reading the text from word file and replace some text from the readed text.

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文件(新文)。

So I have text from original word file, and now I need it to pass to a new word file which not exist (New Text).

我试过

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

这将打开它没有实际的物理存储在文件系统中这是很好的新的Word文件。但我不知道该文本值怎么能传递给这个新的文件。

This opens new word file which not saved physically in file system which is fine. But I am not sure how can pass the text value to this new file.

更新

在上面code运行我试过

After running above code I tried

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

哪位来了是此命令不可用,因为没有文档是打开的。

Which comes up with "This command is not available because no document is open."

推荐答案

下面是一个简单的示例,复制整个文本,并从一个Word文档格式到一个新的文档。在新文档,文本,然后用字的查找和替换放大器;替换的功能:

Here is a simple sample that copies the entire text and formatting from one Word document to a new document. In the new document, text is then replaced using Words Find & Replace feature:

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

这篇关于从Word文件复制文本到一个新词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆