detect several styles on a paragraph

L

Luis Abreu

Hello.

I'm new to this list and to word development. well, I'd really
appreciate if someont could give me some pointers on how to detect the
change of styles on a parapraph...

for instance, a paragaph can have several styles applied to it (ex.:
first line would style X, while the 2nd would be Y). the question is:
how do I know which styles a paragraph has and how can I get the text
which is formatted acording to those styles.

thanks.

regards,
Luis
 
H

Helmut Weber

Hi Luis,
I am restricting my advice to character styles.
The following finds text parts in the actual paragraph,
(that is where the selection starts), that are formatted
with a character style and displays the text formatted in this way.
Unfortunately "Absatz-Standardschriftart" is german localized.
Should be something like "paragraph-standardfont",
and beware of line breaks in the listing.
---
Dim oStl As Style
Dim oRng As Range
Set oRng = Selection.Paragraphs(1).Range
For Each oStl In ActiveDocument.Styles
If oStl.Type = wdStyleTypeCharacter And oStl.NameLocal <>
"Absatz-Standardschriftart" Then
With oRng.Find
.Style = oStl.NameLocal
While .Execute
MsgBox oStl.NameLocal
MsgBox oRng.Text
oRng.Select ' for testing only
oRng.Start = oRng.End
oRng.End = Selection.Paragraphs(1).Range.End
Wend
End With
End If
Next
 
D

Dave Neve

Hi

I've tried to understand your code and I've changed it slightly cos of the
German into

Sub SpotFormat()


Dim oStl As Style
Dim oRng As Range
Set oRng = Selection.Paragraphs(1).Range
For Each oStl In ActiveDocument.Styles
If oStl.Type = wdStyleTypeCharacter And oStl.NameLocal <> "Arial Black"
Then
With oRng.Find
.Style = oStl.NameLocal
While .Execute
MsgBox oStl.NameLocal
MsgBox oRng.Text
oRng.Select ' for testing only
oRng.Start = oRng.End
oRng.End = Selection.Paragraphs(1).Range.End
Wend
End With
End If
Next

End Sub

I've also created a style called "Arial Black" which uses the font 'Arial
Black', as it seemed necessary to me looking at the code.

But when I run the macro, it always says 'font by default' and then selects
the text no matter what style/font is applied.

This isn't quite right is it?

Thanks in advance
 
L

Luis Abreu

Hello Helmut.

thanks a lot. I'll give it a try as soon as I get home.

thanks again.

Regards,
Luis
 
H

Helmut Weber

Hi Dave,
here comes a version, which should work with both US-English and
German. What You are doing, is searching for a character style
which name is not "Arial Black". The result is that the
"default paragraph font" is found.
---
Sub Test389()
' wdStyleTypeCharacter = 2
' msoLanguageIDEnglishUS = 1033
' msoLanguageIDGerman = 1031
Dim sChr As String ' default paragraph font
Dim oStl As Style ' a style
Dim oRng As Range ' a range
' get language US English or German
If Application.Language = 1033 Then
sChr = "Default Paragraph Font"
ElseIf Application.Language = 1031 Then
sChr = "Absatz-Standardschriftart"
Else
MsgBox "Neither USEnglish nor German"
Exit Sub
End If
Set oRng = Selection.Paragraphs(1).Range
For Each oStl In ActiveDocument.Styles
If oStl.Type = 2 And oStl.NameLocal <> sChr Then
With oRng.Find
.Style = oStl.NameLocal
While .Execute
MsgBox oStl.NameLocal
MsgBox oRng.Text
' oRng.Select
oRng.Start = oRng.End
oRng.End = Selection.Paragraphs(1).Range.End
Wend
End With
End If
Next
End Sub
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top