Link to home
Start Free TrialLog in
Avatar of cotton9
cotton9

asked on

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!
Avatar of Rgonzo1971
Rgonzo1971

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
SOLUTION
Avatar of Kimputer
Kimputer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cotton9

ASKER

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?