Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用c阅读Word文档#_C#_Ms Word - Fatal编程技术网

C# 使用c阅读Word文档#

C# 使用c阅读Word文档#,c#,ms-word,C#,Ms Word,我需要从一个特定的点开始阅读word文档。 该关键字取自下拉组合框。 关键字类似于[blah blah,blah,001] 所以,我只需要从那个关键词读到下一个标题的内容 我用这个来逐行阅读标题数字 但它不起作用 string headNum = objparagraph.Range.ListFormat.ListString; string sLine = objparagraph.Range.Text; 如果我理解正确,您需要从关键字到下一个标题阅读Word文档。换句话说,类似于以下文档中

我需要从一个特定的点开始阅读word文档。 该关键字取自下拉组合框。 关键字类似于[blah blah,blah,001]

所以,我只需要从那个关键词读到下一个标题的内容

我用这个来逐行阅读标题数字 但它不起作用

string headNum = objparagraph.Range.ListFormat.ListString;
string sLine = objparagraph.Range.Text;

如果我理解正确,您需要从关键字到下一个标题阅读Word文档。换句话说,类似于以下文档中的红色文本:

在这种情况下,您可以通过以下方式实现:

string关键字=“[blah blah,blah,001]”;
DocumentModel document=DocumentModel.Load(“input.docx”);
ContentPosition start=document.Content
.Find(关键字)
.First()
.结束;
ContentPosition end=新的ContentRange(开始,document.Content.end)
.GetChildElements(ElementType.段落)
.Cast()
.First(p=>p.ParagraphFormat.Style!=null&&p.ParagraphFormat.Style.Name.Contains(“标题”))
.内容
开始
字符串文本=新的ContentRange(开始、结束).ToString();
文本
变量的值为:

我们要检索的文本内容示例。
另一个样本是paragrap


另外,这里还有一些附加的和示例,它们包含一些有用的信息。

@soner请为我提供一些可能的解决方案。tmpRange.Text的可复制副本-从Word doc的文档文件部分读取文本,以便您可以阅读文本并进行比较。它确实帮助了我@JohnHave有一个小疑问…StoryRanges获取每个标题的内容??获取一个StoryRanges集合,该集合表示文档中的所有故事。我需要每个标题的内容分别。。。我想检查是否有一个单词(我的关键字)存在,然后只阅读该特定内容….停止阅读超出该范围的内容。。。。你能帮我做这个吗??
       Word.Application word = new Word.Application();
       Word.Document doc = new Word.Document();
       object fileName = @"C:\wordFile.docx";
        // Define an object to pass to the API for missing parameters
        object missing = System.Type.Missing;                
        doc = word.Documents.Open(ref fileName,
                ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing);
        string ReadValue = string.Empty;
            // Activate the document
        doc.Activate();

         foreach (Word.Range tmpRange in doc.StoryRanges)
         {
            ReadValue += tmpRange.Text;
         }
string keyword = " [blah blah, blah, 001]";
DocumentModel document = DocumentModel.Load("input.docx");

ContentPosition start = document.Content
    .Find(keyword)
    .First()
    .End;

ContentPosition end = new ContentRange(start, document.Content.End)
    .GetChildElements(ElementType.Paragraph)
    .Cast<Paragraph>()
    .First(p => p.ParagraphFormat.Style != null && p.ParagraphFormat.Style.Name.Contains("heading"))
    .Content
    .Start;

string text = new ContentRange(start, end).ToString();