PDA

View Full Version : [SOLVED:] Capitalise Words - Between 2 Delimiters



dj44
09-26-2017, 04:50 PM
folks

good day and nice to see everyone.

:)

now i wanted to only Capitalise the words between # and the hyphen -

before

# golden delicous - apples are tasty fruit

After

# Golden Delicous - apples are tasty fruit

there is 1 error, but i cant spot it.

it makes the words after the hypehen capitalise too. :think:




Sub Capitalise_words()


Dim oPar As Paragraph
Dim oRng As Range


For Each oPar In ActiveDocument.Paragraphs

If InStr(oPar.Range.Text, "#") > 0 Then

Set oRng = oPar.Range


oRng.MoveEndUntil Cset:="-", Count:=wdForward
oRng.End = oRng.End - 1

oPar.Range.Case = wdTitleWord


End If


Next oPar
lbl_Exit:
Exit Sub

End Sub




please do advise on my error, as it does look all ok to me

thank you

macropod
09-26-2017, 09:12 PM
Since you've used:
Set oRng = oPar.Range
you've already designated the entire paragraph. Your subsequent:
oRng.MoveEndUntil Cset:="-", Count:=wdForward
extends that range till the next '-' after the paragraph!
Try using:
Set oRng = oPar.Range.Words.First

Frankly, though, I'd do the lot with a Find/Replace, which would be faster:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "#*-"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
.Case = wdTitleWord
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
End Sub
Unlike your present code, it also doesn't assume the paras start with #.

dj44
09-27-2017, 05:04 AM
Thank you Paul,

Many ways to the mountain, i forgot you can do a search and replace, but i can never work out those wild cards.

I couldnt find my other macro that isolated between the 2 delimiters.

But this has done the task nicely.

thankyou