PDA

View Full Version : Character style to part of paragraph



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

macropod
11-03-2013, 02:37 PM
Try:

Sub Test()
Dim Rng As Range
With Selection.Paragraphs(1).Range
Set Rng = .Sentences.First
If Rng.End <> .End Then
Rng.Style = "Special Style"
End If
End With
End Sub

Paul_Hossler
11-03-2013, 06:53 PM
Thanks

I think I understand that

I really didn't think about the fact that the 'dot' will make what I want into .Sentences(1). Pretty clever

Paul

fumei
11-04-2013, 03:30 PM
Assuming of course there are NO other characters - like a question mark say - that Word uses to define a sentence. Otherwise .First be something not intended.

Paul_Hossler
11-05-2013, 08:43 AM
Assuming of course there are NO other characters - like a question mark say - that Word uses to define a sentence. Otherwise .First be something not intended.


Hmmmm - I am in the process of automating some simple APA formatting processes with my own ribbon group. The APA heading level 3 is per the screen shot: Bold paragraph leadin with terminating period.

I could select the lead in heading text and apply a char style, but it's easier (IHMO) to click in the bigger target (i.e. the paragraph) and click a ribbon button to apply APA3 (my char style) to just the characters up to and including the period at the beginning of the paragraph (error checking of course)

It's highly unlikely that there would be a question mark (but there could be)

Probably low risk ?

Paul

fumei
11-05-2013, 03:40 PM
Not for me to determine any risk level.

Paul_Hossler
11-06-2013, 06:37 PM
Probably low risk ?


Sorry ... I was thinking out loud

Paul