Table A Members of the Diagram NodeChildren object











We can use the Children property of the DiagramNode object to return this collection object. For instance, to get the last child of a node, we can write:


Unfortunately, the programmatic aspects of the Diagram-related objects do not seem to be functioning correctly (as of Service Pack 1 of Excel XP), as we will see shortly.

To create a diagram programmatically, we use the AddDiagram method of the Shapes collection of the worksheet. The following code creates the diagram in Figure A-6. Note, however, that there seems to be a problem in adding or editing text for the nodes in a diagram. Frankly, I could not get the feature to work at all. Even Microsoft's own example generates an error (whose message is totally useless) on my PC. (I even tried recording a macro that changes the text and then replaying the macro, but that too generates an error!) Here is the code:

Sub Example Diagram() Dim ws As Worksheet Set ws = Application.ActiveSheet

Dim sh As Shape Dim dia As Diagram Dim node1 As DiagramNode Dim node2Of1 As DiagramNode Dim node3Of1 As DiagramNode Dim node4Of2 As DiagramNode

Set sh = ws.Shapes.AddDiagram(msoDiagramOrgChart, 0, 0, 300, 300)

' Add first node. Note the odd syntax, since this node is not a child.

Set node1 = sh.DiagramNode.Children.AddNode ' Add subsequent nodes

Set node2Of1 = node1.Children.AddNode Set node3Of1 = node1.Children.AddNode Set node4Of2 = node2Of1.Children.AddNode

MsgBox node1.TextShape.TextFrame.Characters.Text

' This does not work -- cannot set the text!! node1.TextShape.TextFrame.Characters.Text = "Hamlet" End Sub

0 0

Post a comment