Prepend a word in table with "*"

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Prepend a word in table with "*"

Post by Robie »

Hi

I need to prepend "*" to the searched word in a table. If the word is found outside a table then this is ignored, i.e. no changes.
I am doing this as follows but is there a better way of doing it? It seems rather clumsy to me.

Code: Select all

Private Function PrependStar2String(Str As String)
    Dim selEnd As Long
    
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = Str
        .Replacement.Text = "*" + Str
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    While (Selection.Find.Execute)
        If (Selection.Information(wdWithInTable)) Then
            selEnd = Selection.Range.End
            Selection.Range.Text = "*" + Selection.Range.Text
            Selection.Start = selEnd + 1
        End If
    Wend
End Function
Thanks.
Robie

User avatar
HansV
Administrator
Posts: 78598
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Prepend a word in table with "*"

Post by HansV »

The code looks fine to me - I don't think there is a more efficient or elegant way to do it.
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: Prepend a word in table with "*"

Post by Robie »

HansV wrote:The code looks fine to me - I don't think there is a more efficient or elegant way to do it.
Thanks Hans.

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Prepend a word in table with "*"

Post by macropod »

IMHO, the following would be more efficient:

Code: Select all

Private Function PrependStar2String(Str As String)
With Selection.Range
    With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = Str
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
    End With
    Do While .Find.Found = True
        If .Information(wdWithInTable) Then .InsertBefore "*"
        .Collapse wdCollapseEnd
        .Find.Execute
    Loop
  End With
End Function
Note: If you're trying to search the whole document, change 'Selection' to 'ActiveDocument'. If you need to match the whole word, set '.MatchWholeWord = True' and, if it's case-sensitive, set '.MatchCase = True'
Paul Edstein
[Fmr MS MVP - Word]

User avatar
HansV
Administrator
Posts: 78598
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Prepend a word in table with "*"

Post by HansV »

Thanks, Paul, that should be slightly more efficient.
Best wishes,
Hans

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Prepend a word in table with "*"

Post by macropod »

FWIW, there's nothing in the original code or the code I posted that would cause it to stop execution when the end of the Selection is reached. As it stands, the code simply starts at the beginning of the Selection and continues executing until the end of the document is reached. Additional logic would be needed to make it stop when the end of the Selection is reached.
Paul Edstein
[Fmr MS MVP - Word]

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: Prepend a word in table with "*"

Post by Robie »

macropod wrote:IMHO, the following would be more efficient:

Code: Select all

Private Function PrependStar2String(Str As String)
With Selection.Range
    With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = Str
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
    End With
    Do While .Find.Found = True
        If .Information(wdWithInTable) Then .InsertBefore "*"
        .Collapse wdCollapseEnd
        .Find.Execute
    Loop
  End With
End Function
Note: If you're trying to search the whole document, change 'Selection' to 'ActiveDocument'. If you need to match the whole word, set '.MatchWholeWord = True' and, if it's case-sensitive, set '.MatchCase = True'
Hi Paul

That works as expected. Thank you so much.