如何让 vba 查找文本然后将其加粗?

问题描述 投票:0回答:1

目前我有一个带有组合框的用户表单,一旦选择了一个选项,就会根据选择的选项设置文档变量。设置文档变量后,我只想将文本的一部分加粗。文档变量是 LSBV,组合框是 LSB。


Private oVars As Variables

Set oVars = ActiveDocument.Variables

If LSB.Value = "Lump Sum" Then
    oVars("LSBV") = "The Company Lump Sum price to complete the above scope is $xxx + GST. Additional work or variations will be carried out at the following rates:"
    Selection.Find.Execute FindText:="Company Lump Sum price to complete the above scope is $xxx + GST."
    Selection.Font.Bold = wdToggle
ElseIf LSB.Value = "Budget" Then
    oVars("LSBV") = "The Company budget estimate to complete the above scope is $xxx + GST. The works will be carried out on the following schedule of rates:"
    Selection.Find.Execute FindText:="Company budget estimate to complete the above scope is $xxx + GST."
    Selection.Font.Bold = wdToggle
End If

目前加粗“公司总价至”或“公司概算至”。它不应该将第一个“The”加粗,并且它遗漏了很多应该加粗的内容。我如何将其修复为粗体应该粗体的内容?

vba ms-word
1个回答
0
投票

如果我正确理解您的问题,您只需从单词搜索中排除第一个单词即可。

微软文档:

Find.Execute 方法(Word)

    If LSB.Value = "Lump Sum" Then
        oVars("LSBV") = "The Company Lump Sum price to complete the above scope is $xxx + GST. Additional work or variations will be carried out at the following rates:"
    ElseIf LSB.Value = "Budget" Then
        oVars("LSBV") = "The Company budget estimate to complete the above scope is $xxx + GST. The works will be carried out on the following schedule of rates:"
    End If

    Dim sFindTxt As String: sFindTxt = oVars("LSBV")
    Dim iLoc As Long: iLoc = InStr(1, oVars("LSBV"), " ")
    If iLoc > 0 Then
        sFindTxt = Mid(sFindTxt, iLoc + 1)
    End If

    With ActiveDocument.Content.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Replacement.Font.Bold = True        
        .Text = sFindTxt
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .MatchCase = False
        .Execute Replace:=wdReplaceAll
    End With
© www.soinside.com 2019 - 2024. All rights reserved.