O'Reilly Book Excerpts: Word Hacks
Hacking Wordby Andrew Savikas
Editor's note: How can you make the world's most widely used word processing program work even better for you? Andrew Savikas, author of Word Hacks, gets you five steps closer to taming your text with his generous collection of hacks. Rid yourself of hidden character styles, tap VBScript for powerful string searching, use Perl from within a Word macro, and more.
Hack #47: Use an Outline to Build an Org Chart
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 Insert -> Diagram), 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 5-6, you can quickly add, remove, or rearrange entries.
Once you complete your outline, the code will use it to produce a diagram like the one shown in Figure 5-7.
The next time you need to change the chart, just edit the outline and make a new one.
Place this macro in the template of your choice [Hack #50] and either run it from the Tools-> Macro -> Macros 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 File ->
Properties 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.
Figure 5-6. Edit your organization information in Outline view
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
Figure 5-7. An organizational chart created from an outline
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.