Paul_Hossler
11-03-2013, 09:43 AM
I'd like to apply a char style to the 'heading' part of a paragraph.
Click in the paragraph, run the macro, and the text from the beginning of the paragraph to the 'dot' marked has the style applied.
I finally got something to work (or at least to not fail yet), but I feel it's not designed in the a Word-oriented fashion, i.e. using the appropriate objects and methods.
Mostly because I still struggle with Range and Selection and such.
Using InStr for example just feels 'un-Word-ish'
Any suggestions appreciated
Option Explicit
Sub test()
Dim rPara As Range, rToBeStyled As Range
Dim iDot As Long
Set rPara = Selection.Paragraphs(1).Range
iDot = InStr(rPara.Text, ".")
If iDot = 0 Or iDot = rPara.Characters.Count Then
Exit Sub
Else
rPara.Collapse (wdCollapseStart)
Selection.Extend (".")
Set rToBeStyled = Selection.Range
MsgBox rToBeStyled.Text
' rToBeStyled.Style= "Special Style"
End If
End Sub
Paul
Click in the paragraph, run the macro, and the text from the beginning of the paragraph to the 'dot' marked has the style applied.
I finally got something to work (or at least to not fail yet), but I feel it's not designed in the a Word-oriented fashion, i.e. using the appropriate objects and methods.
Mostly because I still struggle with Range and Selection and such.
Using InStr for example just feels 'un-Word-ish'
Any suggestions appreciated
Option Explicit
Sub test()
Dim rPara As Range, rToBeStyled As Range
Dim iDot As Long
Set rPara = Selection.Paragraphs(1).Range
iDot = InStr(rPara.Text, ".")
If iDot = 0 Or iDot = rPara.Characters.Count Then
Exit Sub
Else
rPara.Collapse (wdCollapseStart)
Selection.Extend (".")
Set rToBeStyled = Selection.Range
MsgBox rToBeStyled.Text
' rToBeStyled.Style= "Special Style"
End If
End Sub
Paul