J
JB
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
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.MoveLeft wdCharacter, 1
Selection.MoveRight Unit:=wdWord, Count:=1, _
Extend:=wdExtend
If StyleNow = "Persname" Then GoTo Pers1
If StyleNow = "Perstitle" Then GoTo perstitle1
If StyleNow = "Persinits" Then GoTo persinits1
If StyleNow = "Office" Then GoTo office1
If StyleNow = "Department" Then GoTo department1
If StyleNow = "Email" Then GoTo email1
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
perstitle1:
While StyleNow = "Perstitle"
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
persinits1:
While StyleNow = "Persinits"
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
office1:
While StyleNow = "Office"
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
department1:
While StyleNow = "Department"
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
email1:
While StyleNow = "Email"
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
y = y + 1
If y = 20 Then
Selection.MoveLeft wdCharacter, 2, wdExtend
GoTo CheckEntry
End If
Wend
Selection.MoveLeft Unit:=wdCharacter, Count:=1, _
Extend:=wdExtend
CheckEntry:
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 wdCharacter, 3
Selection.MoveLeft wdCharacter, 1, wdExtend
StyleNow = Selection.Style
If test <> test1 Then
If Right(Selection.Style, 5) = "Entry" Then
ListType(x) = Selection.Style
End If
End If
'GoTo Persname
Else
End1:
End If
If test <> test1 Then s = 6
Next s
Next x
endnow:
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
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.MoveLeft wdCharacter, 1
Selection.MoveRight Unit:=wdWord, Count:=1, _
Extend:=wdExtend
If StyleNow = "Persname" Then GoTo Pers1
If StyleNow = "Perstitle" Then GoTo perstitle1
If StyleNow = "Persinits" Then GoTo persinits1
If StyleNow = "Office" Then GoTo office1
If StyleNow = "Department" Then GoTo department1
If StyleNow = "Email" Then GoTo email1
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
perstitle1:
While StyleNow = "Perstitle"
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
persinits1:
While StyleNow = "Persinits"
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
office1:
While StyleNow = "Office"
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
department1:
While StyleNow = "Department"
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
email1:
While StyleNow = "Email"
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
y = y + 1
If y = 20 Then
Selection.MoveLeft wdCharacter, 2, wdExtend
GoTo CheckEntry
End If
Wend
Selection.MoveLeft Unit:=wdCharacter, Count:=1, _
Extend:=wdExtend
CheckEntry:
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 wdCharacter, 3
Selection.MoveLeft wdCharacter, 1, wdExtend
StyleNow = Selection.Style
If test <> test1 Then
If Right(Selection.Style, 5) = "Entry" Then
ListType(x) = Selection.Style
End If
End If
'GoTo Persname
Else
End1:
End If
If test <> test1 Then s = 6
Next s
Next x
endnow: