添加指向特定单词的旁注
我有 2 个 Word 文档:
- 用于检查错误单词的文档。文档中的例句:便便的维尼很可爱。
- 带有矩阵的文档,其中包含要搜索的错误单词和建议。
示例:术语=Winnie the poop 建议=正确的拼写是Winnie the pooh。
此时,我的代码添加了一条注释,但它突出显示了整个句子(小熊维尼很可爱)。如何将建议与错误的特定术语(小熊维尼)联系起来?
Sub Search4WrongWords()
Dim MatrixCounter As Integer 'Counter to search for all terms in the Matrix
Dim DocToValidate As Word.Document 'Document to validate and search for wrong words
Dim MaxWordsInMatrix As Integer 'Total rows in Matrix
Const ColumnWithTerm = 2 'Matrix wrong terms Example: Winnie the Poop
Const ColumnWithSuggestion = 3 'Matrix suggested term. Example: Winnie The Pooh
MatrixCounter = 0
DocumentPath = "C:\Folder\File_to_validate.docx" 'File to validate for wrong words
MatrixPath = "C:\Folder\Matrix_with_suggestions.docx" 'Matrix with terms & suggestions
Set MatrixDoc = Documents.Open(MatrixPath) 'File path is provided by user
Set DocToValidate = Documents.Open(DocumentPath) 'File path is provided by user
MaxWordsInMatrix = MatrixDoc.Tables(1).Rows.Count 'Total rows in matrix
For MatrixCounter = 2 To MaxWordsInMatrix 'counter =2 to avoid reading matrix header row
With DocToValidate.range.Find
.Text = Trim(LCase(Left(MatrixDoc.Tables(1).Rows(MatrixCounter).Cells(ColumnWithTerm).range.Text, Len(MatrixDoc.Tables(1).Rows(MatrixCounter).Cells(ColumnWithTerm).range.Text) - 2)))
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.NoProofing = False
Do While .Execute(Forward:=True) = True
suggestion = MatrixDoc.Tables(1).Rows(MatrixCounter).Cells(ColumnWithSuggestion).range.Text
DocToValidate.Comments.Add DocToValidate.range, Text:=suggestion
Loop 'do while
End With 'DocToValidate
Next 'MatrixCounter
End Sub
I have 2 word documents:
- Document to review for wrong words. Sample sentence in document: Winnie the poop is cute.
- Document with a matrix that contains wrong words to search for, and a suggestion.
Example: Term=Winnie the poop Suggestion=Correct spelling is Winnie the pooh.
At this point my code adds a comment, but it highlights the whole sentence (Winnie the poop is cute). How do I link the suggestion to the specific term that is wrong (Winnie the poop)?
Sub Search4WrongWords()
Dim MatrixCounter As Integer 'Counter to search for all terms in the Matrix
Dim DocToValidate As Word.Document 'Document to validate and search for wrong words
Dim MaxWordsInMatrix As Integer 'Total rows in Matrix
Const ColumnWithTerm = 2 'Matrix wrong terms Example: Winnie the Poop
Const ColumnWithSuggestion = 3 'Matrix suggested term. Example: Winnie The Pooh
MatrixCounter = 0
DocumentPath = "C:\Folder\File_to_validate.docx" 'File to validate for wrong words
MatrixPath = "C:\Folder\Matrix_with_suggestions.docx" 'Matrix with terms & suggestions
Set MatrixDoc = Documents.Open(MatrixPath) 'File path is provided by user
Set DocToValidate = Documents.Open(DocumentPath) 'File path is provided by user
MaxWordsInMatrix = MatrixDoc.Tables(1).Rows.Count 'Total rows in matrix
For MatrixCounter = 2 To MaxWordsInMatrix 'counter =2 to avoid reading matrix header row
With DocToValidate.range.Find
.Text = Trim(LCase(Left(MatrixDoc.Tables(1).Rows(MatrixCounter).Cells(ColumnWithTerm).range.Text, Len(MatrixDoc.Tables(1).Rows(MatrixCounter).Cells(ColumnWithTerm).range.Text) - 2)))
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.NoProofing = False
Do While .Execute(Forward:=True) = True
suggestion = MatrixDoc.Tables(1).Rows(MatrixCounter).Cells(ColumnWithSuggestion).range.Text
DocToValidate.Comments.Add DocToValidate.range, Text:=suggestion
Loop 'do while
End With 'DocToValidate
Next 'MatrixCounter
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您执行查找时,范围或选择将重新定义为找到的匹配项。如果您要进一步处理找到的范围,这非常有用。在大多数情况下,可以使用文档的内置范围对象。
例外情况是您需要使用找到的范围作为另一个操作的输入参数,就像添加注释一样。在代码中,当您使用 DocToValidate.range 作为注释的锚点而不是引用找到的匹配项时,它引用整个文档。
您可以通过使用范围对象变量来克服这个问题,如下所示。
When you execute a find the range, or selection, is redefined to the found match. This is useful if you are then going to further process the found range. In most circumstances it is possible to use the built-in range object of a document.
The exception to this is where you need to use the found range as an input parameter for another operation, as you do with adding a comment. In your code when you use
DocToValidate.range
as the anchor for the comment instead of referring to the found match it refers to the entire document.You can overcome this by using an object variable for the range, as below.