使用VB.NET计算Word文档的单词

时间:2014-01-21 04:21:41

标签: vb.net visual-studio-2010 visual-studio

我想只计算一个word文档中的单词,但我得到一个不同的o / p,

我试过了,

 Dim objapp As Word.Application
    objapp = New Word.Application()
    Dim app As Application = New Application

    ' Open specified file.
    Dim doc As Document = objapp.Documents.Open(TextBox1.Text & "\" & "TEST.doc")

    ' Loop through all words.
    Dim count As Integer = doc.Words.Count
    For i As Integer = 1 To count
        ' Write word to screen.
        Dim text As String = doc.Words(i).Text

    Next
    Dim objWriter As New System.IO.StreamWriter(TextBox1.Text & "\" & "Error.txt")
    objWriter.Write("Word Count :" & count)
    objWriter.Close()
    ' Quit the application.
    app.Quit()
    doc.Close

在这里我可以计算单词,但是当我输入文档时它也在计数,即如果文档中有8个单词,其中2个输入则显示我计数:10而不是它应该只计数:8即只有单词。

Plz任何人都会帮助我完成所需的逻辑,

提前致谢。

3 个答案:

答案 0 :(得分:1)

基于Words Interface

的文档
  

Count属性包括标点符号和段落标记   总数。如果您需要计算文档中的实际单词,请使用   字数统计对话框。

我找到了支持知识库文章:Word count appears inaccurate when you use the VBA "Words" property

  

仅返回文档或范围中的字数,不包括   段落标记和标点符号,使用ComputeStatistics方法   而不是Words属性。

Range.ComputeStatistics Method

'Usage
Dim Statistic As WdStatistic
Dim returnValue As Integer
Dim range1 As Range
returnValue = range1.ComputeStatistics(Statistic)

答案 1 :(得分:0)

使用正则表达式来匹配其单词是否

喜欢这个

Dim WordCount = New Regex("\w+").Matches(text).Count

答案 2 :(得分:0)

我不确定vb.net但是如果c#代码可以帮助你,那么这里是C#中的字数统计代码。

    /* button click event - create the object from file path.
     * get the whole string then count the word.
     */
    private void btnWordCount_Click(object sender, EventArgs e)
    {
        Microsoft.Office.Interop.Word.Application word =
            new Microsoft.Office.Interop.Word.Application();
        object miss = System.Reflection.Missing.Value;

        object path = doc_file_path;
        object readOnly = true;

        Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(
                                                    ref path, ref miss, ref readOnly, ref miss,
                                                    ref miss, ref miss, ref miss, ref miss,
                                                    ref miss, ref miss, ref miss, ref miss,
                                                    ref miss, ref miss, ref miss, ref miss);
        string totaltext = "";
        for (int i = 0; i < docs.Paragraphs.Count; i++)
        {
            totaltext += " \r\n " + docs.Paragraphs[i + 1].Range.Text.ToString();
        }
        tbText.Text = totaltext;
        lblWordCount.Text = WordCount(totaltext).ToString();
        docs.Close();
        word.Quit();
    }

    /* this function accepts the string (here in case string mean all the line on word)
     * and then return the word count in that line.
     */
    private int WordCount(string line)
    {
        line = line.Trim();
        return line.Split(' ').Length;
    }