Currently i have a userform with a combobox where once an option is chosen a document variable is set based on what option is chosen. Once the docvariable is set I want to only bold a part of the text. The docvariavble is LSBV, the combobox is 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
Currently it bolds “The Company Lump Sum price to” or “The Company budget estimate to”. It shouldn’t bold the initial “The” and it misses alot of what should be bolded. How would I fix it to bold what should be bolded?
ZacJ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
If I understand your question correctly, you simply need to exclude the first word from the Word search.
Microsoft documentation:
Find.Execute method (Word)
Dim sTxt As String
Debug.Print LSB.Value, (LSB.Value = "Lump Sum") ' debugging code
If LSB.Value = "Lump Sum" Then
sTxt = "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
sTxt = "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
oVars("LSBV") = sTxt
Dim iLoc As Long: iLoc = InStr(1, sTxt, " ")
If iLoc > 0 Then ' remove the first word
sTxt = Mid(sTxt, iLoc + 1)
End If
With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Font.Bold = True
.Text = sTxt
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.Execute Replace:=wdReplaceAll
End With
Sub Test()
Dim sTxt As String
sTxt = "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:"
Dim iLoc As Long: iLoc = InStr(1, sTxt, " ")
If iLoc > 0 Then ' remove the first word
sTxt = Mid(sTxt, iLoc + 1)
End If
With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Font.Bold = True
.Text = sTxt
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.Execute Replace:=wdReplaceAll
End With
End Sub
8