strange error (BUG?) using .BaseStyle property

W

ward

Hi,

I've created a small program that displays the styles in a
document hierarchically in a treeview object. Upon
clicking a style by the user, the hierarchy of this style
is shown in a second treeview object.
However, if (and only if) I click a style with a long name
(e.g. "Body Text First Indent") an error is always raised
(sometimes "critical error xxx, cannot read from xxx" and
sometimes ±"the object invoked has disconnect from its
client'). I've tried it on different computers
(configuration: Win 2000Pro - Word 2000). Always an error.

I've tracked the error down to the line:
If CStr(tSt.BaseStyle) = "" Then

But this is strange, because this line is also used for
evaluating each style (including the long ones) when
creating the big tree view object. And no errors are
raised then.

Do you also have this error? Anybody knows what's
happening (or what i'm doing wrong)?

thanks for any suggestions,
Ward

To test for yourself:
- add the additional control 'Microsoft TreeView control
6.0'
- create new form
- create 2 treeview objects named "treeViewStyles"
and "treeViewDetail"
- set the lineStyle property of these treeview objects to
tvwRootLines (for clarity)
- paste code below in the form.



----CODE -----

Option Explicit

Dim tDoc As Document

Private Sub UserForm_Initialize()
Set tDoc = ActiveDocument
BuildTree
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer,
CloseMode As Integer)
Set tDoc = Nothing
End Sub

Private Sub treeViewStyles_NodeClick(ByVal Node As
MSComctlLib.Node)
'Show detail of selected style in other treeview object
Dim tSt As Style
Dim n As Node

If StyleExist(treeViewStyles.SelectedItem.Text) Then
Set tSt = ActiveDocument.Styles
(treeViewStyles.SelectedItem.Text)
treeViewDetail.Nodes.Clear
AddStyleToTree tSt, treeViewDetail
Else
treeViewDetail.Nodes.Clear
End If

Set tSt = Nothing
Set n = Nothing

End Sub
Private Sub BuildTree()

Dim tSt As Style

treeViewStyles.Nodes.Clear
For Each tSt In tDoc.Styles
AddStyleToTree tSt, treeViewStyles
Next

Set tSt = Nothing

End Sub

Private Sub AddStyleToTree(ByVal tSt As Style, TVObj As
TreeView)

Dim n As Node
Dim Parent As String
Dim ParentDescription As String

If TextInTree(tSt.NameLocal, TVObj) Then Exit Sub

If CStr(tSt.BaseStyle) = "" Then 'ERROR is
raised when "tSt.BaseStyle" is executed in some cases !!
'Add root node if necessary
If tSt.Type = wdStyleTypeCharacter Then
Parent = "C":
ParentDescription = "Character Styles"
Else
Parent = "P"
ParentDescription = "Paragraph Styles"
End If
If Not TextInTree(ParentDescription, TVObj) Then
TVObj.Nodes.Add , tvwChild, Parent, ParentDescription
'Add style node
Set n = TVObj.Nodes.Add(Parent, tvwChild, CStr
(tSt.NameLocal), CStr(tSt.NameLocal))
Else
If Not TextInTree(CStr(tSt.BaseStyle), TVObj) Then
AddStyleToTree tSt.BaseStyle, TVObj
Set n = TVObj.Nodes.Add(CStr(tSt.BaseStyle),
tvwChild, CStr(tSt.NameLocal), CStr(tSt.NameLocal))
End If

'Indicate non built-in styles bold
If Not tSt.BuiltIn Then n.Bold = True

Set n = Nothing

End Sub

Private Function TextInTree(tText As String, TVObj As
TreeView) As Boolean
'Determine if node with text already exists

Dim n As Node
For Each n In TVObj.Nodes
If n.Text = tText Then TextInTree = True: Exit
Function
Next
Set n = Nothing

End Function

Private Function StyleExist(StyleName) As Boolean
'Determine if style exist

Dim tSt As Style
For Each tSt In ActiveDocument.Styles
If tSt.NameLocal = StyleName Then StyleExist =
True: Exit Function
Next
Set tSt = Nothing

End Function
 

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