目的是为Microsoft Word文件获取准确的字数。我们有一台运行Apache和PHP的Windows服务器。在该机器上运行的Web服务基本上获取文档的所有内容,并通过preg_match_all("/\S+/", $string, $matches); return count($matches[0]);
运行内容。工作得很好,但它并不准确。因此,我们写了下面的宏:将事情从Word宏返回到PHP
Sub GetWordCountBreakdown()
Dim x As Integer
Dim TotalWords As Long
Dim FieldWords As Long
TotalWords = ActiveDocument.ComputeStatistics(wdStatisticWords)
For x = 1 To ActiveDocument.Fields.Count
If ActiveDocument.Fields.Item(x).Result.ComputeStatistics(wdStatisticWords) > 25 Then
FieldWords = FieldWords + ActiveDocument.Fields.Item(x).Result.ComputeStatistics(wdStatisticWords)
End If
Next x
MsgBox (TotalWords & " - " & FieldWords & " = " & TotalWords - FieldWords)
End Sub`
当我运行在Word这个宏,它给了我一个整洁的小警告框文档中的计数所有的单词和引用。我不知道如何将这些值返回给PHP,以便我的web服务可以将它们传回给我。
更新:我能够在PHP中重写这个宏,并获得正确的wordcount。基本上是:
$word = new COM("Word.Application")
$word->Documents->Open(file);
$wdStatisticWords = 0;
$wordcount = $word->ActiveDocument->ComputeStatistics($wdStatisticWords);
等
此外,为什么您当前的系统不正确? – JakeSteam 2011-03-09 20:30:11
我只想在主文档中使用文本,而不是任何注释或脚注。如果我做'$ content = $ word-> ActiveDocument-> Content;'来获取文档的内容,它会将所有东西都看作一个长字符串,因此是不正确的。 – Vic 2011-03-09 20:43:16