【问题标题】:How do I make the last two words of each footnote bold using Word VBA?如何使用 Word VBA 将每个脚注的最后两个单词加粗?
【发布时间】:2019-11-20 05:28:48
【问题描述】:

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

我尝试调用 strAuthor 并将其设为粗体,但因为它不再是评论。作者我无法再设置它,因为它现在在脚注中。我在互联网上尝试了很多例子,但我无法让它们工作: StackOverflow 的如何使字符串变为粗体;使用 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

【问题讨论】:

    标签: vba ms-word formatting footnotes


    【解决方案1】:

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

    在下面的代码中,基于问题中的代码,Range 对象在Footnotes 的循环中分配给每个单独的Footnote 对象。然后它被折叠到它的终点,并且开始向后延伸两个词。 (为了更好地理解其工作原理,请考虑选择脚注,按右箭头,然后按两次 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) 找不到任何可以使用的词。

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

    【讨论】:

    • Cindy,非常感谢您,这非常有效。我也非常感谢您解释您的代码。祝你有美好的一天!
    【解决方案2】:

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

    基本上它循环遍历所有脚注。并使用单词的索引将该单词的粗体属性设置为 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
    

    【讨论】:

    • 您好 JosephC,谢谢。我已经尝试过了,但我得到“找不到方法或数据成员”;它不喜欢 oFootNote.Range.Words(oFootNote.Range。不过我很感谢你的帮助。黛比
    • 猜测不是所有脚注都至少有 2 个字长?编辑代码以检查。因此,如果脚注超过 2 个单词,它现在应该只加粗最后两个单词。
    • 嗨,再次感谢,但它仍然不喜欢 oFootNote.Range;我猜这是因为 oFootNote 的 Range 没有智能感知。我知道这太令人沮丧了,我一整天都在努力解决这个问题。谢谢,晚上好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    相关资源
    最近更新 更多