如何使用互操作复制word文档的一部分 [英] How to copy a part of word document using interop

查看:89
本文介绍了如何使用互操作复制word文档的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用密钥识别word文档的一部分,然后将其复制到另一个word文档中。我怎样才能通过C#.net实现它。



i尝试以下结果作为文本我需要它作为文件

和段落应该是我用一个关键词确认了



我尝试过:



I need to identity a part of word document with a key then in need to copy it into another word document. how can i achieve it through C#.net.

i tried the following it result as text i need it as document
and paragraph should be i identified with a key word

What I have tried:

public string ReadFileContent(string path, int paraGraphNum)
        {
            int i = 0;

            StringBuilder sb = new StringBuilder();

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

            object file = path;

            object nullobj = System.Reflection.Missing.Value;

            Microsoft.Office.Interop.Word.Document 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);


            Microsoft.Office.Interop.Word.Paragraphs DocPar = doc.Paragraphs;

            // Count number of paragraphs in the file
            long parCount = DocPar.Count;
           
            // Step through the paragraphs
            while (i < parCount)
            {
                i++;
                if (i == paraGraphNum)
                {
                    sb.Append(DocPar[i].Range.Text);
                    break;
                }
            }

            doc.Close(ref nullobj, ref nullobj, ref nullobj);
            wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);
            return (sb.ToString());
        }
    }

推荐答案

按照以下说明操作:



如何:以编程方式搜索和替换文档中的文本 [ ^ ]

如何:以编程方式在Word中设置搜索选项 [ ^ ]

如何:以编程方式遍历文档中找到的项目 [ ^ ]

如何:以编程方式定义和选择Docume中的范围nts [ ^ ]



试试吧!

祝你好运!
Follow these instructions:

How to: Programmatically Search for and Replace Text in Documents[^]
How to: Programmatically Set Search Options in Word[^]
How to: Programmatically Loop Through Found Items in Documents[^]
How to: Programmatically Define and Select Ranges in Documents[^]

Try!
And Good Luck!


这篇关于如何使用互操作复制word文档的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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