根据变量添加注释

发布于 2025-01-10 07:34:03 字数 1748 浏览 0 评论 0原文

我的目标是根据主表搜索文档中的术语或单词,其中包含不应在文档中使用的术语以及给作者的建议。我有一个称为矩阵的主表,其中包含两列:术语、建议。 例如:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰沐子 2025-01-17 07:34:03

您缺少 Range 参数,该参数告诉 Word 将注释附加到何处:

Sub Tester()
    Dim i As Long
    With ActiveDocument
        For i = 1 To .Range.Words.Count
            If .Words(i).Text = "theme" Then
                .Comments.Add Range:=.Words(i), Text:="not theme"
            End If
        Next i
    End With
End Sub

You are missing the Range argument which tells Word where to attach the comment:

Sub Tester()
    Dim i As Long
    With ActiveDocument
        For i = 1 To .Range.Words.Count
            If .Words(i).Text = "theme" Then
                .Comments.Add Range:=.Words(i), Text:="not theme"
            End If
        Next i
    End With
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文