Use an Outline to Build an Org Chart





Use an Outline to Build an Org Chart

figs/moderate.gif figs/hack47.gif

Word's outlining feature excels at managing hierarchical information. This hack shows you how to use an outline to create an organizational chart on the fly.

Maintaining a company's organizational charts can be a time-consuming task. Word does include a Diagram feature that makes it easy to create an organizational chart (select InsertDiagram), but editing charts can be a real challenge, particularly after any substantial reorganization.

Unfortunately, the macro recorder ignores diagrams, which removes a valuable tool for deciphering unfamiliar Word objects. But you can still automate diagrams—you just need to decipher the Diagram object on your own. The code in this hack should give you a good starting point.

Rather than manipulating an existing diagram, you can store the organizational information in an outline and create the diagram from scratch after any changes. With your organizational information stored in an outline, like the one shown in Figure, you can quickly add, remove, or rearrange entries.

Edit your organization information in Outline view
figs/wrdh_0506.gif


Once you complete your outline, the code will use it to produce a diagram like the one shown in Figure.

An organizational chart created from an outline
figs/wrdh_0507.gif


The next time you need to change the chart, just edit the outline and make a new one.

1 The Code

Place this macro in the template of your choice [Hack #50] and either run it from the ToolsMacroMacros dialog or put a button for it on a menu or toolbar [Hack #1].

The text for the top-level entry (or root node) is culled from the CompanyName property in the outline document. To enter a company name, select FileProperties and go to the Summary tab. If you don't fill in the property, Word inserts some dummy text.

Though your chart could go 10 levels deep (9 for each of Word's outline levels, plus one more for the body-text level), this code goes only 4 levels deep. Adding more levels would require substantially more code, most of which would be nearly identical to that for the first four levels. You'll need to add your own additional code to handle an outline more than four levels deep.

Sub MakeOrgChartFromOutline( )

Dim doc As Document

Dim para As Paragraph

Dim sCompanyName As String

Dim sParaText As String

Dim nodeRoot As DiagramNode

Dim shShape As Shape

Dim node1 As DiagramNode

Dim node2 As DiagramNode

Dim node3 As DiagramNode

Dim node4 As DiagramNode



Set doc = ActiveDocument



sCompanyName = doc.BuiltInDocumentProperties("Company")

If Len(sCompanyName) <= 1 Then 

    sCompanyName = "Type Company Name Here"

End If



Set shShape = _

    Documents.Add.Shapes.AddDiagram(msoDiagramOrgChart, 0, 0, 500, 500)

Set nodeRoot = shShape.DiagramNode.Children.AddNode

nodeRoot.TextShape.TextFrame.TextRange.text = sCompanyName



For Each para In doc.Paragraphs

    Select Case para.OutlineLevel

        Case wdOutlineLevel1

            sParaText = Left(para.Range.text, _

               para.Range.Characters.Count - 1)

            Set node1 = nodeRoot.Children.Ad dNode

            node1.TextShape.TextFrame.TextRange.text = sParaText

            Set node2 = Nothing

            Set node3 = Nothing

            Set node4 = Nothing

        Case wdOutlineLevel2

            sParaText = Left(para.Range.text, _

                para.Range.Characters.Count - 1)

            Set node2 = node1.Children.AddNode

            node2.TextShape.TextFrame.TextRange.text = sParaText

            Set node3 = Nothing

            Set node4 = Nothing

        Case wdOutlineLevel3

            sParaText = Left(para.Range.text, _

                para.Range.Characters.Count - 1)

            Set node3 = node2.Children.AddNode

            node3.TextShape.TextFrame.TextRange.text = sParaText

            Set node4 = Nothing

        Case wdOutlineLevel4

            sParaText = Left(para.Range.text, _

                para.Range.Characters.Count - 1)

            Set node4 = node3.Children.AddNode

            node4.TextShape.TextFrame.TextRange.text = sParaText

    End Select

Next para



End Sub

Rather than attempting to position elements in the diagram, the macro just relies on Word's default automatic behavior to align and position the entries.


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows