我只想计算 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/