VBAbyMBA
VBAbyMBA

Reputation: 826

Select a particular sentence in range using find method and making it bold

Using following code firstly I make the selected paragraph as range and then I want to bold text from start of the paragraph till first sentence end which in my case it is ". "

How its is possible to select the first sentence in given range?

Dim CON As Range
selection.MoveDown Unit:=wdParagraph, COUNT:=1, Extend:=wdExtend
If selection.Range.ComputeStatistics(wdStatisticLines) < 3 Then
selection.Font.Bold = True
selection.MoveRight Unit:=wdCharacter, COUNT:=1
Else
selection.MoveLeft Unit:=wdCharacter, COUNT:=1
selection.ExtendMode = True
selection.EndKey Unit:=wdLine
selection.MoveDown Unit:=wdLine, COUNT:=2
Set CON = selection.Range
selection.ExtendMode = False
With CON.Find
.Text = ". "
.Forward = False
.Wrap = wdFindStop
.Execute
End With
If CON.Find.Found Then
'' Now here I want to bold the sentence 
else 
end if

UPDATE

I set the code and now it can evaluation what it is found.

Set CON = selection.Range
selection.ExtendMode = False
Set conFind = CON.Duplicate
'''''''''''''>>''''''''''''
    With conFind.Find
    .Text = ">>"
    .Forward = False
    .Wrap = wdFindStop
    .Execute
    End With
    If conFind.Find.Found Then
      CON.End = conFind.End
      CON.Font.Bold = True
    Else
'''''''''''''. ''''''''''''
        With conFind.Find
        .Text = "^?. "
        .Forward = False
        .Wrap = wdFindStop
        .Execute
        End With
        If conFind.Find.Found Then

                If confind = "S. " Then
                conFind.Find.Execute
                If conFind.Find.found Then
                CON.End = conFind.End
                CON.Font.Bold = True
                Else
                End If

            Else
            CON.End = conFind.End
            CON.Font.Bold = True
            End If


        Else
''''''''''''', ''''''''''''
            With conFind.Find
            .Text = ", "
            .Forward = False
            .Wrap = wdFindStop
            .Execute
            End With
            If conFind.Find.Found Then
            CON.End = conFind.End
            CON.Font.Bold = True
            Else
            End If
        End If
    End If

Upvotes: 0

Views: 148

Answers (1)

Cindy Meister
Cindy Meister

Reputation: 25703

Declare an additional Range variable, for example

Dim conFind as Word.Range

then set it to be a duplicate of the original Range. Use this for the Find - if Find is successful conFind will be the found Range. Then set the End point of the original Range to the end point of the found Range and apply the bold formatting.

Note: I prefer to also create a boolean variable to hold the success of Find.Found, rather than testing Range.Find.Found as in my experience it's more reliable. I've left the code as you have it, however...

Set CON = selection.Range
selection.ExtendMode = False
Set conFIND = CON.Duplicate
With conFind.Find
  .Text = ". "
  .Forward = False
  .Wrap = wdFindStop
  .Execute
End With
If conFind.Find.Found Then
  CON.End = conFind.End
  CON.Font.Bold = True
else 
end if

Upvotes: 1

Related Questions