根据变量添加注释
我的目标是根据主表搜索文档中的术语或单词,其中包含不应在文档中使用的术语以及给作者的建议。我有一个称为矩阵的主表,其中包含两列:术语、建议。 例如:
Term= ubiquitous Suggestion=“不要使用 ubiquitous,而是在任何地方使用”。
每次我在主表中找到一个单词时,我都需要帮助找到一种将建议添加为评论的方法。
下面是我的代码。我省略了循环和用户输入,因为问题是:如何添加指向正在验证的文档中找到的单词的注释。
Sub AddSideCommentsToWordDocument()
Dim MatrixDoc As Word.Document 'This is the master table with terms
Dim MatrixPath As String 'File path to master table
Dim DocToValidate As Word.Document 'This is the file in question to validate
Dim DocumentPath As String 'This is the file path to the file in question
Dim Suggestion As String 'Variable to store suggested comment
Dim WordToCheck As String 'Variable to store the word to validate against Master table term
DocumentPath = "C:\Folder\FileInQuestion.docx" 'These paths will come from user input
MatrixPath = "C:\Folder\Master Terms Matrix.docx"
Set MatrixDoc = Documents.Open(MatrixPath) 'Open Master Table
Set DocToValidate = Documents.Open(DocumentPath) 'Open doc in question
'Get first term from Master Table into a variable
TermFromMatrix = MatrixDoc.Tables(1).Rows(1).Cells(ColumnWithTerm).range.Text
'Get first suggested comment from Master Table into a variable
Suggestion = MatrixDoc.Tables(1).Rows(1).Cells(ColumnWithSuggestion).range.Text
'Get first word from document to validate into a variable
WordToCheck = DocToValidate.Words(i).Text
'Compare first term from Master Table vs. First word from document to validate
If TermFromMatrix = WordToCheck Then
DocToValidate.Comments.Add Text:=Suggestion 'I get the following error: Argument not optional
End If
Close
End Sub
My goal is to search for terms or words in documents against a Master table that contains the terms that should not be used in documents as well as a suggestion for the writer. I have a Master table I call the Matrix that contains two columns: Term, Suggestion.
For example:
Term= ubiquitous Suggestion= "Don't use ubiquitous, instead use everywhere".
I need help to find a way to add Suggestions as comments every time I find a word that is in the Master table.
Below my code. I omitted the loops and user input because the issue is: How to add comments that point to the word that was found in the document being validated.
Sub AddSideCommentsToWordDocument()
Dim MatrixDoc As Word.Document 'This is the master table with terms
Dim MatrixPath As String 'File path to master table
Dim DocToValidate As Word.Document 'This is the file in question to validate
Dim DocumentPath As String 'This is the file path to the file in question
Dim Suggestion As String 'Variable to store suggested comment
Dim WordToCheck As String 'Variable to store the word to validate against Master table term
DocumentPath = "C:\Folder\FileInQuestion.docx" 'These paths will come from user input
MatrixPath = "C:\Folder\Master Terms Matrix.docx"
Set MatrixDoc = Documents.Open(MatrixPath) 'Open Master Table
Set DocToValidate = Documents.Open(DocumentPath) 'Open doc in question
'Get first term from Master Table into a variable
TermFromMatrix = MatrixDoc.Tables(1).Rows(1).Cells(ColumnWithTerm).range.Text
'Get first suggested comment from Master Table into a variable
Suggestion = MatrixDoc.Tables(1).Rows(1).Cells(ColumnWithSuggestion).range.Text
'Get first word from document to validate into a variable
WordToCheck = DocToValidate.Words(i).Text
'Compare first term from Master Table vs. First word from document to validate
If TermFromMatrix = WordToCheck Then
DocToValidate.Comments.Add Text:=Suggestion 'I get the following error: Argument not optional
End If
Close
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您缺少
Range
参数,该参数告诉 Word 将注释附加到何处:You are missing the
Range
argument which tells Word where to attach the comment: