2

我已将气球评论更改为脚注,并使用了作者的名字。我需要作者的姓名以粗体显示,但我无法让我的代码阅读脚注。我的问题在于设置:oFootnote

我尝试调用 strAuthor 并将其设为粗体,但因为它不再是评论。作者我无法再设置它,因为它现在在脚注中。我在互联网上尝试了很多例子,但我无法让它们工作:StackOverflow 的 How do i make a string bold; 也使用 VBA 将粗体文本插入 Word

 Set oFootnote = oDoc.Footnotes.Add(Range:=Selection.Range, Text:="Some text") 

我是练习生,请不要太苛刻地评价我

'Convert comments to footnotes with Author name in bold
Dim i As Long
Dim oDoc As Document
dim oComment as Comments
Dim oFootnote As Footnotes

'Document is the ActiveDocument
Set oDoc = Application.ActiveDocument

'the author's name needs to be bold (the last two words in each footnote)
Set oFootnote = oDoc.Footnotes

    With oFootnote
      Selection.Range.Words.Last.Words (2)
        'Make the last two words bold'
        With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Font.bold = True
        End With
    End With
    Selection.Find.Execute
    'Set oFootnote = Nothing
  Next

我试过了

 Set oFootnote = oDoc.Footnotes Range:=Selection.Words.Last.Words(2) 

但它不喜欢“Range:= onwards”所以我做了

 Selection.Range.Words.Last.Words (2)                invalid use of a property
4

2 回答 2

1

通常有不止一种方法可以实现这样的目标,但关键通常是使用专用Range对象。

在下面的代码中,基于问题中的代码,Range对象被分配FootnoteFootnotes. 然后它被折叠到它的终点,并且起点向后延伸了两个词。(为了更好地理解这是如何工作的,请考虑选择脚注,按右箭头,然后按两次 ctrl+shift+左箭头以选择最后两个单词。)

Dim oDoc As Document
Dim oFootnotes As Footnotes
Dim Ftnote As Footnote
Dim rngFootnote As Word.Range

'Document is the ActiveDocument
Set oDoc = Application.ActiveDocument

'the author's name needs to be bold (the last two words in each footnote)
Set oFootnotes = oDoc.Footnotes
For Each Ftnote In oFootnotes
    Set rngFootnote = Ftnote.Range
    rngFootnote.Collapse wdCollapseEnd
    rngFootnote.MoveStart wdWord, -2
    rngFootnote.Font.Bold = True
Next

请注意,问题中出现其中一个错误的原因是因为Words.Last返回了一个Range包含最后一个单词的对象。由于它只包含一个单词 - 最后一个 -Words(2)找不到它可以使用的任何内容。

另一个错误的原因是无法将 a 分配Range给 a FootnoteorFootnotes对象。它们是不同的东西,完全...

于 2019-07-10T19:42:36.763 回答
0

不太熟悉单词对象,但试试这个。为我的几个测试工作。

基本上它遍历所有脚注。并使用单词的索引将该单词的粗体属性设置为 true。

Sub Test()

    Dim oFootNote As Footnote
    Dim oLastIndex As Long

    For Each oFootNote In ActiveDocument.Footnotes

        oLastIndex = oFootNote.Range.Words.Count

        If oLastIndex > 2 Then
            oFootNote.Range.Words(oLastIndex).Bold = True
            oFootNote.Range.Words(oLastIndex - 1).Bold = True
        End If
    Next

End Sub
于 2019-07-10T15:05:03.653 回答