Selecting text in Doc base on Character style? Word XP Problem

J

John

Hi Folks,

I'm having a problem with Word XP and selecting text from a VBA macro! the
code I've been using is listed below and if anyone can have a look and
suggest a better way of doing this (and possibly one which works for XP) I
would be very grateful. This works fine in office 2k but XP is kicking me
up and down the house! :p

Cheers

John

p.s. I'm new to VBA and this isn't my code so I'm still a bit sketchy on how
all the selection thing works. Any pointers gratefuly accepted.

Public Sub AmendList()
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
Persname:
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.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
y = y + 1
If y = 20 Then GoTo endnow
If test <> test1 Then
If Right(Selection.Style, 5) = "Entry" Then
ListType(x) = Selection.Style
End If
End If
GoTo Persname
Else
End1:
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1,
Extend:=wdExtend
If StyleNow = "Persinits" Then
txtInits(x) = Selection.Text
ElseIf StyleNow = "Perstitle" Then
txtTitles(x) = Selection.Text
ElseIf StyleNow = "Persname" Then
txtpersname(x) = Selection.Text
ElseIf StyleNow = "Office" Then
txtOffices(x) = Selection.Text
ElseIf StyleNow = "Department" Then
txtDepartments(x) = Selection.Text
ElseIf StyleNow = "Email" Then
txtEmails(x) = Selection.Text
End If
Selection.MoveRight Unit:=wdCharacter, Count:=2
StyleNow = Selection.Style
Selection.MoveLeft Unit:=wdCharacter, Count:=2
Selection.MoveRight Unit:=wdCharacter, Count:=1,
Extend:=wdExtend
If Right(Selection.Style, 5) = "Entry" Then
ListType(x) = Selection.Style
End If
Selection.MoveRight Unit:=wdCharacter, Count:=2
End If
If test <> test1 Then s = 6
Next s
Next x
endnow:
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
If StyleNow = "Persinits" Then
txtInits(x) = Selection.Text
ElseIf StyleNow = "Perstitle" Then
txtTitles(x) = Selection.Text
ElseIf StyleNow = "Persname" Then
txtpersname(x) = Selection.Text
ElseIf StyleNow = "Office" Then
txtOffices(x) = Selection.Text
ElseIf StyleNow = "Department" Then
txtDepartments(x) = Selection.Text
ElseIf StyleNow = "Email" Then
txtEmails(x) = Selection.Text
End If
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
If Right(Selection.Style, 5) = "Entry" Then
ListType(x) = Selection.Style
End If
Selection.MoveRight Unit:=wdCharacter, Count:=2
End Sub
 
K

Klaus Linke

Hi John,

I haven't analyzed exactly what your code does and how, but the problem may
well be that you don't have control in Word VBA whether "StyleNow =
Selection.Style" returns the paragraph style or the character style.
It may depend on such things as whether you have (only) a paragraph mark
selected, or whether the paragraph contains text in several character
styles...

To get the character style reliably (if you are sure that the text only
contains one character style), you would have to loop through all styles,
and check if the style is of Type wdStyleTypeCharacter, and if it's applied
to the selected text.
A real nuisance...

Perhaps you could try to use .Find instead to go to text formatted in a
specific character style.

Regards,
Klaus
 

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