Microsoft Word文档以编程方式查找修订词 [英] Microsoft word document find revision words programatically .NET
问题描述
使用修订对象可以识别已插入或删除文件的项目。下面给出了vb代码,通过传递Rang,它将返回一个可用的RevisionText对象列表[text,pageid,lineid和type]。
代码正在执行所需的操作。但是它通过所提供范围内的所有内容非常慢(大约在该范围内有10000个)。有没有办法加快循环?
Vb或C#代码没问题。
我尝试了什么:
Using Revisions Object it is possible to identify Inserted or Deleted items of the document. vb code is given below where by passing Rang it will return a list of RevisionText object available [text, pageid, lineid and type].
Code is doing what is needed. But it is very slow as it go through everything in the provided range (approximately there are 10000 in the range). Is there a way to speed up the loop?
Vb or C# code is fine.
What I have tried:
<pre>Public Function Revisions(ByVal myRange As Microsoft.Office.Interop.Word.Range) As List(Of RevisionText)
Dim RevisionText As New List(Of RevisionText)
Try
For Each oRevision In myRange.Revisions
Dim ht As RevisionText = New RevisionText()
If oRevision.Type = Microsoft.Office.Interop.Word.WdRevisionType.wdRevisionInsert Then
ht.Text = oRevision.Range.Text
ht.PageNo = oRevision.Range.Information(Microsoft.Office.Interop.Word.WdInformation.wdActiveEndAdjustedPageNumber)
ht.LineNo = oRevision.Range.Information(Microsoft.Office.Interop.Word.WdInformation.wdFirstCharacterLineNumber)
ht.Type = "Insert"
RevisionText.Add(ht)
ElseIf oRevision.Type = Microsoft.Office.Interop.Word.WdRevisionType.wdRevisionDelete Then
ht.Text = oRevision.Range.Text
ht.PageNo = oRevision.Range.Information(Microsoft.Office.Interop.Word.WdInformation.wdActiveEndAdjustedPageNumber)
ht.LineNo = oRevision.Range.Information(Microsoft.Office.Interop.Word.WdInformation.wdFirstCharacterLineNumber)
ht.Type = "Delete"
RevisionText.Add(ht)
End If
Next oRevision
Return RevisionText
Catch ex As Exception
Return RevisionText
End Try
End Function
推荐答案
我没有看到太多的优化可能性。与许多对象的Office互操作总是很慢。
但您可以尝试将ht
声明移出循环这样它就可以重复使用,而不是为每个对象创建。
你也可以考虑更改ht.Type
数字类型的成员。当只有两个可能的值时,不需要使用字符串,并且分配数值比分配字符串要快得多。
也不与速度相关:
为什么在代码中有两次相同的return语句而不是函数末尾的一个返回语句?
I don't see much optimisation possibilities. Office interop with many objects is always slow.
But you can try moving theht
declaration out of the loop so that it can be re-used instead of being created for each object.
You might also think about changing theht.Type
member to a numeric type. There should be no need to use a string when there are only two possible values and assigning a numeric value is much faster than assigning a string.
Also not speed related:
Why do you have the same return statement twice in the code instead of a single one at the end of the function?
以下只是一个非常小的改变代码的工作方式并提升性能:
The following is just a very minor change to how your code works and can give the appearance of a performance boost:
Public Iterator Function Revisions(ByVal myRange As Microsoft.Office.Interop.Word.Range) As IEnumerable(Of RevisionText)
Try
For Each oRevision In myRange.Revisions
Dim ht As RevisionText = New RevisionText()
If oRevision.Type = Microsoft.Office.Interop.Word.WdRevisionType.wdRevisionInsert Then
ht.Text = oRevision.Range.Text
ht.PageNo = oRevision.Range.Information(Microsoft.Office.Interop.Word.WdInformation.wdActiveEndAdjustedPageNumber)
ht.LineNo = oRevision.Range.Information(Microsoft.Office.Interop.Word.WdInformation.wdFirstCharacterLineNumber)
ht.Type = "Insert"
ElseIf oRevision.Type = Microsoft.Office.Interop.Word.WdRevisionType.wdRevisionDelete Then
ht.Text = oRevision.Range.Text
ht.PageNo = oRevision.Range.Information(Microsoft.Office.Interop.Word.WdInformation.wdActiveEndAdjustedPageNumber)
ht.LineNo = oRevision.Range.Information(Microsoft.Office.Interop.Word.WdInformation.wdFirstCharacterLineNumber)
ht.Type = "Delete"
End If
Yield ht
Next oRevision
'Return RevisionText (left in by mistake - code not tested)
Catch ex As Exception
Yield Nothing
End Try
End Function
现在,您可以一次单步执行(迭代)修订,而不是一次查找所有出现的内容然后单步执行它们。
在此处阅读更多内容:实现迭代器 - VB - Eric White的博客 [ ^ ]
这篇关于Microsoft Word文档以编程方式查找修订词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!