Word VBA:将注释和用户名转换为脚注;需要作者(现在在脚注中)加粗

时间:2019-07-11 13:06:38

标签: vba ms-word

将注释和用户名转换为脚注;需要作者(现在在脚注中)加粗。

我可以正确输入所有文本,我一直在使用strAuthor展示oComment.Author,这对我来说一直有效。既然文本在脚注中,我现在不能使作者大胆。它仍然可以识别strAuthor;我尝试将rngAuthor声明为Range,但无法将其与strAuthor结合使用。感谢您在设置rngAuthor方面的帮助。我已经能够将所有脚注都加粗,但是我只需要作者加粗即可。

昨天我问了一个类似的问题,但是我想的方法是错误的。我要求将最后两个字变粗体,现在我意识到这会弄乱脚注,这些脚注尚未转换为注释(感谢辛迪的帮助)。

Dim oDoc As Document
Dim oComment As Comment
Dim strAuthor As String
Dim rngAuthor As Range
Dim oFootNotes As Footnotes
Dim Ftnote As Footnote
Dim rngFootnote As Word.Range

'Document is the ActiveDocument
Set oDoc = Application.ActiveDocument

'find comment
For Each oComment In ActiveDocument.Comments

Set rngAuthor = oComment.Scope.Editors

'Comment made by "Author"
  strAuthor = oComment.Author

  'create a footnote and move comment text to footnote with rngAuthor(bold)
  oDoc.oFootNotes.Add Range:=oComment.Scope, Text:=oComment.Range.Text, Text:=oComment.Range.Editors
  rngAuthor.Font.bold = True
  ActiveWindow.ActivePane.View.SeekView = wdSeekFootnotes
  'Delete Comment
  oComment.Delete

Next

运行代码时,我得到 运行时错误:Set rngAuthor = oComment.Scope.Editors为13,oDoc.oFootNotes.Add Range:=oComment.Scope, Text:=oComment.Range.Text, Text:=oComment.Range.Editors的运行时错误438

1 个答案:

答案 0 :(得分:1)

声明脚注范围允许对该范围进行操作,以便可以将注释添加到脚注中,然后在将注释作者声明为字符串时,可以将作者名称作为文本应用于脚注范围:“ rngFootnote .Text = strAuthor”,这意味着可以通过“ rngFootnote.Font.bold = True”将作者的姓名加粗。还有代码,如果作者的名字显示为“姓氏,姓氏”,则将其转换为“姓氏”。

Dim oDoc As Document
Dim oComment As Comment
Dim strAuthor As String
Dim strForename As String
Dim strSurname As String
Dim oFootnotes As Footnotes
Dim Ftnote As Footnote
Dim rngFootnote As Word.Range

'Document is the ActiveDocument
Set oDoc = Application.ActiveDocument
Set oFootnotes = oDoc.Footnotes

'find comment
For Each oComment In ActiveDocument.Comments

  'Comment made by "Author"
  strAuthor = oComment.Author

  'create a footnote and move comment text to footnote with Author
  oDoc.Footnotes.Add Range:=oComment.Scope, Text:=oComment.Range.Text & "  "

  'the author's name now needs to be bold (now the last two words in each footnote)
  For Each Ftnote In oFootnotes               'this adds the new footnote with the comment
    Set rngFootnote = Ftnote.Range            'this is where the comment is added

    rngFootnote.Collapse wdCollapseEnd

    If InStr(strAuthor, ",") > 0 Then       'name added
      'split surname from strUserName
      strSurname = Split(strAuthor, " ")(0)
        'delete comma
        strSurname = Trim(Left(strSurname, Len(strSurname) - 1))
      'split forename from strUserName
      strForename = Right(strAuthor, Len(strAuthor) - InStrRev(strAuthor, ","))
      strForename = Trim(Right(strForename, Len(strForename) - 1))
      strAuthor = strForename & " " & strSurname
    ElseIf InStr(strAuthor, "") > 0 Then       'name added
    End If
  Next
    rngFootnote.Text = strAuthor
    rngFootnote.Font.bold = True
  'Delete comment
  oComment.Delete
Next