首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Word宏将注释添加到表中失败的文档中

Word宏将注释添加到表中失败的文档中
EN

Stack Overflow用户
提问于 2016-05-03 22:21:03
回答 1查看 177关注 0票数 0

我正在编写一个Microsoft宏,它贯穿Word文档的每一段,并向每个段落添加一个注释。该评论包含该段的样式。这样,同事就可以打印出带有注释的文档,并知道如何在将来设置类似的文档样式。

我快到了,代码将注释添加到每个段落,但在表的第一行死亡:

“此方法或属性不可用,因为对象引用表行的末尾。”

以下是代码:

代码语言:javascript
运行
复制
Sub aa_AddStylesComment()
'
' aa_AddStylesComment Macro
' Author: Me!
'

Dim strParaStyle As String
Dim cmtNewComment As Comment

'Run through word file and delete any comments with author set to a space character (that is the author of the comments added by the script)
For J = ActiveDocument.Comments.Count To 1 Step -1
  With ActiveDocument
    If .Comments(J).Author = " " Then
      .Comments(J).Delete
    End If
  End With
Next J

'Running through every paragraph
For i = 1 To ActiveDocument.Paragraphs.Count
  With ActiveDocument

    'Get paragraph style
    strParaStyle = .Paragraphs(i).Style

    'Create a new comment and collect it - then change the author to space character
    Set cmtNewComment = Selection.Comments.Add(.Range(.Paragraphs(i).Range.Words(1).Start, (.Paragraphs(i).Range.Words(1).End - 1)), strParaStyle)
    cmtNewComment.Author = " "

  End With
Next

End Sub
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-03 23:24:29

如果是表,则可以添加检查,如果段落中有单元格,则如下所示:

代码语言:javascript
运行
复制
    If .Paragraphs(i).Range.Tables.Count = 0 Then
        Set cmtNewComment = .Paragraphs(i).Range.Comments.Add(.Range(.Paragraphs(i).Range.Words(1).Start, (.Paragraphs(i).Range.Words(1).End - 1)), strParaStyle)
        cmtNewComment.Author = " "
    ElseIf .Paragraphs(i).Range.Cells.Count > 0 Then
        Set cmtNewComment = .Paragraphs(i).Range.Comments.Add(.Range(.Paragraphs(i).Range.Words(1).Start, (.Paragraphs(i).Range.Words(1).End - 1)), strParaStyle)
        cmtNewComment.Author = " "
    End If

请注意,您不需要使用Selection,因为您从不更改它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37015386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档