SIMPLE - Use VBA to change hyphenated "list" to sequential numbers

Hello,

I'm working with medical office management software that generates reports as a Word doc.  One of its annoyances is that It lists the treatments at the bottom of the report in a hyphenated list (although Word doesn't recognize it as an actual list - which is ok.)

I want to use VBA to change the hyphens to sequential numbers followed by a ")" symbol.

For example...

TREATMENT:
- This is treatment one.
- This is treatment two.
- This is an optional third treatment, etc.

turns into...

TREATMENT:
1) This is treatment one.
2) This is treatment two.
3) This is an optional third treatment, etc.

I'm guessing one of you VBA geniuses can whip up this code in less time that it took me to type out the problem.  I'd really appreciate the help.  Thanks!
cotton9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try

Sub Macro()
'
Selection.HomeKey Unit:=wdStory

With Selection.Find
    .ClearFormatting
    .Text = "- "
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Execute
End With

Do While Selection.Find.Found = True
     Selection.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
        ListGalleries(wdNumberGallery).ListTemplates(1), ContinuePreviousList:= _
        True, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
        wdWord10ListBehavior
    Selection.Find.Execute
Loop
End Sub

Open in new window

Regards
KimputerCommented:
Just in case you don't like the listing with above code (it creates a dot in my Word), and you really want the ")"  for some reason:

Sub Macro3()

Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "TREATMENT:"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    counter = 0
    Do While Selection.Find.Found
        counter = counter + 1
        With Selection.Find
            .Text = "-"
            .Replacement.Text = counter & ")"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchByte = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceOne
    Loop

End Sub

Open in new window

Rgonzo1971Commented:
HI,

corrected code to search the 1) format

Sub Macro()
'

For Idx = 1 To ListGalleries(wdNumberGallery).ListTemplates().Count
    If ListGalleries(wdNumberGallery).ListTemplates(Idx).ListLevels(1).NumberFormat = "%1)" Then
        listIdx = Idx
        Exit For
    End If
Next
Selection.HomeKey Unit:=wdStory

With Selection.Find
    .ClearFormatting
    .Text = "- "
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Execute
End With

Do While Selection.Find.Found = True
     Selection.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
        ListGalleries(wdNumberGallery).ListTemplates(listIdx), ContinuePreviousList:= _
        True, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
        wdWord10ListBehavior
    Selection.Find.Execute
Loop
End Sub

Open in new window

Regards

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cotton9Author Commented:
Both of the above solutions result in changing all hyphens found within the document, not just those found in the "TREATMENT:" section.  

I thought I might be able to fix it myself by finding "TREATMENT:" first to set the cursor location (per Kimputer's solution) and then changing wdFindContinue to wdFindStop within the loop that finds/replaces the hyphens.  But that resulted in not finding any hyphens at all.

However, if I physically place the cursor at the word "TREATMENT:" instead of finding it with VBA, using wdFindStop works w/  Rgonzo1971's code.

Ideas?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.