Hi Folks,
I've written some code to check the character style in paragraphs and
'do stuff' based of the style found. First question: Is it better to
use the 'Do While' clause with this (which I'm thinking it might be).
Second Question: Is it better to use range instead of selection, and if
so how do I convert this to ranges? I've never used ranges before and
don't really know that much about the properties/methods.
Any pointers/improvements gratefully accepted.
Cheers

J

Dim x As Integer, y As Integer, s As Integer
Dim StyleNow As String
Dim test As Variant, test1 As Variant

StyleNow = "Persname"
SavedMemData = ActiveDocument.BuiltInDocumentProperties(wdPropertyParas)
For x = 0 To SavedMemData - 1
styletype = Selection.Style
For s = 1 To 6
y = 0
test1 = Selection.Paragraphs.Last
test = Selection.Document.Paragraphs.Item(x + 1)
If test <> test1 Then GoTo End1
If Selection.Style = StyleNow Then

StyleNow = Selection.Style
Selection.MoveLeft wdCharacter, 1
Selection.MoveRight Unit:=wdWord, Count:=1, _
Extend:=wdExtend

If StyleNow = "Persname" Then GoTo Pers1
While StyleNow = "Persname"
Selection.MoveRight wdWord, 1, wdExtend
If Right(Selection.Style, 5) = "Entry" Then
Selection.MoveLeft wdWord, 1, wdExtend
Selection.MoveLeft wdCharacter, 1, wdExtend
StyleNow = Selection.Style
GoTo CheckEntry
End If
Wend

This must be performed on multiple paragraphs and multiple styles to
have the effect I need.

Re: Do While or While & Selection or Range (REPOSTED AS PER REQUEST) by Helmut

Helmut
Fri Jun 25 14:43:13 CDT 2004

Hi,
... a bit clearer now, but still
there are 2 goto's with nowhereto go. ;-)
Anyway.
Seems as if you were in the need for a loop
over all paragraphs in your doc at first.
Could look like this:
Sub test488()
Dim oDcm As Document ' object document
Dim oPrg As Paragraph ' object paragraph
Dim pStl As String ' property style
Set oDcm = ActiveDocument
For Each oPrg In oDcm.Paragraphs
pStl = oPrg.Style
Select Case pStl
Case "Style0": ' do things according to Style0
Case "Style1": ' do things according to Style1
Case "Style2": ' do things according to Style2
End Select
Next
End Sub
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98


Re: Do While or While & Selection or Range (REPOSTED AS PER REQUEST) by JB

JB
Mon Jun 28 09:51:21 CDT 2004

Helmut Weber wrote:
> Hi,
> ... a bit clearer now, but still
> there are 2 goto's with nowhereto go. ;-)
> Anyway.
> Seems as if you were in the need for a loop
> over all paragraphs in your doc at first.
> Could look like this:
> Sub test488()
> Dim oDcm As Document ' object document
> Dim oPrg As Paragraph ' object paragraph
> Dim pStl As String ' property style
> Set oDcm = ActiveDocument
> For Each oPrg In oDcm.Paragraphs
> pStl = oPrg.Style
> Select Case pStl
> Case "Style0": ' do things according to Style0
> Case "Style1": ' do things according to Style1
> Case "Style2": ' do things according to Style2
> End Select
> Next
> End Sub
> ---
> Greetings from Bavaria, Germany
> Helmut Weber, MVP
> "red.sys" & chr(64) & "t-online.de"
> Word XP, Win 98
>
HI Helmut,
Would this work with Character styles? It's really the character styles
I'm interested in. There may be up to 6 different character styles on
the same paragraph and I need to find the style, save the selected text
to a variable and move to the next style.

Cheers
J

Re: Do While or While & Selection or Range (REPOSTED AS PER REQUEST) by Helmut

Helmut
Mon Jun 28 10:46:28 CDT 2004

Hi J,
(I'd appreciate a real name very much),
this may become a long story,
so you need a loop over all paragraphs? No Problem.
Then you need a loop over not builtin character styles,
I assume. No problem.
Then you would like to know, whether characters formatted
in that style are in the paragraph? Could be done.
And then you want to know, which characters. OK.
However, what if this returns several successfull finds
in the paragraph? You want to concatenate them?
---
Sub Test488()
Dim oDcm As Document ' object document
Dim oPrg As Paragraph ' object paragraph
Dim oRng As Range
Dim oStl As Style ' object style
ResetSearch
Set oDcm = ActiveDocument
For Each oPrg In oDcm.Paragraphs
Set oRng = oPrg.Range
For Each oStl In oDcm.Styles
If oStl.Type = wdStyleTypeCharacter _
And oStl.BuiltIn = False Then
With oRng.Find
.Style = oStl.NameLocal
While .Execute
Stop
MsgBox oRng.Text
oRng.Start = oRng.End
oRng.End = oPrg.Range.End
Wend
End With
End If
Next
Next
End Sub
Sub ResetSearch()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
End Sub

---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98



Re: Do While or While & Selection or Range (REPOSTED AS PER REQUEST) by JB

JB
Mon Jun 28 11:22:48 CDT 2004

Helmut Weber wrote:

> Hi J,
> (I'd appreciate a real name very much),
> this may become a long story,
> so you need a loop over all paragraphs? No Problem.
> Then you need a loop over not builtin character styles,
> I assume. No problem.
> Then you would like to know, whether characters formatted
> in that style are in the paragraph? Could be done.
> And then you want to know, which characters. OK.
> However, what if this returns several successfull finds
> in the paragraph? You want to concatenate them?
> ---
> Sub Test488()
> Dim oDcm As Document ' object document
> Dim oPrg As Paragraph ' object paragraph
> Dim oRng As Range
> Dim oStl As Style ' object style
> ResetSearch
> Set oDcm = ActiveDocument
> For Each oPrg In oDcm.Paragraphs
> Set oRng = oPrg.Range
> For Each oStl In oDcm.Styles
> If oStl.Type = wdStyleTypeCharacter _
> And oStl.BuiltIn = False Then
> With oRng.Find
> .Style = oStl.NameLocal
> While .Execute
> Stop
> MsgBox oRng.Text
> oRng.Start = oRng.End
> oRng.End = oPrg.Range.End
> Wend
> End With
> End If
> Next
> Next
> End Sub
> Sub ResetSearch()
> Selection.Find.ClearFormatting
> Selection.Find.Replacement.ClearFormatting
> With Selection.Find
> .Text = ""
> .Replacement.Text = ""
> .Forward = True
> .Wrap = wdFindContinue
> .Format = False
> .MatchCase = False
> .MatchWholeWord = False
> .MatchWildcards = False
> .MatchSoundsLike = False
> .MatchAllWordForms = False
> End With
> End Sub
>
> ---
> Greetings from Bavaria, Germany
> Helmut Weber, MVP
> "red.sys" & chr(64) & "t-online.de"
> Word XP, Win 98
>
>
Hi Helmut
Name=John :-)

Thanks for this, I've had a look at this code (very nice) and I think I
can modify it for what I'm trying to do. This is FAR better than
anything I've written to do the same thing.

This will/should only ever return one sucessful find of any given
character style in a paragraph. Ideally what I'd like is to read from
the paragraph one at a time and get this into an array for title,
initials,surname,office,dept,email and have it all match up at the other
end :-)

Regards
John

Re: Do While or While & Selection or Range (REPOSTED AS PER REQUEST) by Helmut

Helmut
Tue Jun 29 03:01:19 CDT 2004

Hi John,
this was a demo on how it could be done.
Tested on a 2 paragraph document. ;-)
Of course it would be far better,
to read the names of characterstyles into an array,
or hardcode them in the macro, and not to loop
through all characterstyles for each paragraph.
That is no good at all.
If you want only the first successful find,
simply replace the while .execute wend by
if .execute end if. And in that case you wouldn't
have to rearrange the range either, but make sure,
that it is always again the actual paragraph.
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98