vb.net - 使用 VB.NET 计算 Word 文档的字数

标签 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,即只有单词。

请任何人帮助我解决所需的逻辑,

提前致谢。

最佳答案

基于 Words Interface 的文档

The Count property includes punctuation and paragraph marks in the total. If you need a count of the actual words in a document, use the Word Count dialog box.



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

To return only the number of words in a document or a range, excluding paragraph marks and punctuation, use the ComputeStatistics method instead of the Words property.



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

关于vb.net - 使用 VB.NET 计算 Word 文档的字数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21249120/

相关文章:

.net - 从另一个太长的唯一数值创建固定长度的唯一字符串

c++ - 在 C++ 中将 fstreams 加载到 std::vector

c# - OleDbConnection.Open() 仅在一个项目中引发异常,相同的代码适用于其他项目

visual-studio - 让 Visual Studio Code 在已经打开的项目窗口中打开文件?

c# - 如何将 WCF 客户端代理生成的类中的方法标记为虚拟

.net - 将函数地址(委托(delegate))传递给属性

c# - vb.net 和 C# 问题中的编码差异

javascript - 从 javascript 调用 vb.net 函数

c# - 如何在 Visual Studio 中创建插件以从设置文件生成 javascript、CSS 和其他代码

asp.net - 如何在 Web 和图像服务器之间拆分 Web 应用程序的部署