Example: Printing Document Statistics
I will now present the application mentioned earlier. In particular, the code in Example 19-1 gathers statistics about the active document and places that data in a table in a new document. The only point to note is that I must reference the dialog object before creating a new document, since that new document will become the active document and I will lose the statistics on the original document.
Example 19-1. The PrintStatistics Routine
Sub PrintStatistics() ' Prints statistics on the active document ' to a table in a new document Dim dial As dialog Dim tbl As Table Dim sName As String Dim sProp As String ' Get this now, before the active document changes Set dial = Dialogs(wdDialogDocumentStatistics) ' Create new document Documents.Add ' Insert document heading Selection.InsertAfter "Document Statistics. Created " & — Now() & vbCr & vbCr ' Give it a nice style Selection.Style = wdStyleHeading1 Selection.Collapse wdCollapseEnd ' Create a table with 2 columns Set tbl = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=1, — NumColumns:=2) ' Print the document statistics to the table ' adding rows as we go tbl.Cell(1, 1).Select Selection.Text = "File Name" Selection.Font.Bold = True Selection.Move wdCell Selection.Text = dial.FileName ' DoNextRow adds a new row and ' fills it with the next piece of data DoNextRow tbl, "Directory", dial.Directory DoNextRow tbl, "Template", dial.Template DoNextRow tbl, "Title", dial.Title DoNextRow tbl, ...
Get Writing Word Macros, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.