Writing Word Macros, Second Edition

Book description

Many Microsoft Word users and VBA programmers don't realize the extensive opportunities that exist when Word's Object Model is accessed using Visual Basic for Applications (VBA), which replaced WordBasic in conjunction with the release of Word 97. By creating what is commonly called a "Word Macro" you can automate many features available in Word. Writing Word Macros (previously titled Learning Word Programming is the introduction to Word VBA that allows you to do these things and more, including:

  • Create custom pop-up menus

  • Automatically create tables from lists

  • Append one document to the end (or beginning) of another

  • Create a toggle switch to change a document from draft to final copy by adding or removing a watermark in the header

  • Generate reports using data from other applications

  • The Visual Basic Editor and the Word VBA programming environment. Word features a complete and very powerful integrated development environment for writing, running, testing, and debugging VBA macros.

  • The VBA programming language (which is the same programming language used by Microsoft Excel, Access, and PowerPoint, as well as the retail editions of Visual Basic).

  • The Word object model. Word exposes nearly all of its functionality through its object model, which allows Word to be controlled programmatically using VBA. While the Word object model, with almost 200 objects, is the largest among the Office applications, readers need be familiar with only a handful of objects. Writing Word Macros focuses on these essential objects, but includes a discussion of a great many more objects as well.

  • Not intended to be an encyclopedia of Word programming, Writing Word Macros provides Word users, as well as programmers who are not familiar with the Word object model with a solid introduction to writing VBA macros and programs. In particular, the book focuses on:

    Writing Word Macros is written in a terse, no-nonsense manner that is characteristic of Steven Roman's straightforward, practical approach. Instead of a slow-paced tutorial with a lot of hand-holding, Roman offers the essential information about Word VBA that you must master to program effectively. This tutorial is reinforced by interesting and useful examples that solve practical programming problems, like generating tables of a particular format, managing shortcut keys, creating fax cover sheets, and reformatting documents. Writing Word Macros is the book you need to dive into the basics of Word VBA programming, enabling you to increase your power and productivity when using Microsoft Word.

    Table of contents

    1. Writing Word Macros, 2nd Edition
      1. Preface
        1. A Title Change and Word 2000 Compliance
        2. The Book’s Audience
        3. Organization of This Book
        4. The Book’s Sample Code
          1. Obtaining the Sample Programs
        5. Conventions in This Book
        6. How to Contact Us
        7. Acknowledgments
      2. 1. Introduction
      3. 2. Preliminaries
        1. What Is a Programming Language?
        2. Programming Style
          1. Comments
          2. Readability
          3. Modularity
      4. I. The VBA Environment
        1. 3. The Visual Basic Editor, Part I
          1. The Project Window
            1. Project Names
            2. Project Contents
              1. Document objects
              2. Standard modules
              3. Class modules
              4. UserForm objects
              5. References
          2. The Properties Window
          3. The Code Window
            1. Procedure and Full-Module Views
            2. The Object and Procedure List Boxes
              1. A Document object
              2. A standard module
              3. A UserForm object
          4. The Immediate Window
          5. Arranging Windows
            1. Docking
          6. Document Events
        2. 4. The Visual Basic Editor, Part II
          1. Navigating the IDE
            1. General Navigation
            2. Navigating the Code Window at Design Time
            3. Tracing Code
            4. Bookmarks
          2. Getting Help
          3. Creating a Procedure
          4. Run Mode, Design Mode, and Break Mode
          5. Errors
            1. Design-Time Errors
            2. Compile-Time Errors
            3. Run-Time Errors
            4. Logical Errors
          6. Debugging
            1. Tracing
              1. Step Into (F8)
              2. Step Over (Shift-F8)
              3. Step Out (Ctrl-Shift-F8)
              4. Run To Cursor
              5. Changing the next statement to execute
              6. Breaking out of debug mode
            2. Watching Expressions
              1. Quick Watch (Shift-F9)
              2. The Locals and Watch Window
          7. Macros
            1. Recording Macros
            2. Running Macros
      5. II. The VBA Programming Language
        1. 5. Variables, Data Types, and Constants
          1. Comments
          2. Line Continuation
          3. Constants
            1. Enums
          4. Variables and Data Types
            1. Variable Declaration
            2. The Importance of Explicit Variable Declaration
              1. Option Explicit
            3. Numeric Data Types
            4. Boolean Data Type
            5. String Data Type
            6. Date Data Type
            7. Variant Data Type
            8. Word Object Data Types
              1. The generic As Object declaration
              2. The Set statement
            9. Arrays
              1. The dimension of an array
              2. Dynamic arrays
              3. The UBound function
            10. Variable Naming Conventions
            11. Variable Scope
              1. Procedure-level (local) variables
              2. Module-level variables
            12. Variable Lifetime
              1. Static variables
            13. Variable Initialization
          5. VBA Operators
        2. 6. Functions and Subroutines
          1. Calling Functions
            1. Ignoring Return Values
          2. Calling Subroutines
          3. Parameters and Arguments
            1. Optional Arguments
            2. Named Arguments
            3. ByRef Versus ByVal Parameters
          4. Exiting a Procedure
          5. Public and Private Procedures
          6. Referencing a Project
            1. Fully Qualified Procedure Names
        3. 7. Built-in Functions and Statements
          1. The MsgBox Function
          2. The InputBox Function
          3. VBA String Functions
          4. Miscellaneous Functions and Statements
        4. 8. Control Statements
          1. The If… Then Statement
          2. The For Loop
            1. Exit For
          3. The For Each Loop
          4. The Do Loop
            1. Infinite Loops
          5. The Select Case Statement
          6. A Final Note on VBA
            1. File-Related Functions
            2. Date- and Time-Related Functions
            3. The Format Function
            4. Errors!
      6. III. Objects and Object Models
        1. 9. Object Models
          1. Objects, Properties, and Methods
            1. Properties
            2. Methods
              1. Reading the help files
          2. Collection Objects
            1. The Documents Collection: An Example of a Collection
              1. The Count property
              2. The Add method
              3. The Item method
            2. The Base of a Collection
          3. Object Model Hierarchies
          4. Object Model Syntax
          5. Object Variables
            1. The With Statement
            2. Object Variables Save Execution Time
            3. An Object Variable Is a Pointer
            4. Freeing an Object Variable: The Nothing Keyword
            5. The Is Operator
            6. Default Members
            7. Named Arguments
            8. The New Keyword
            9. Global Objects
        2. 10. The Word Object Model
          1. The Word Object Model: A Perspective
          2. Word Enums
          3. The VBA Object Browser
        3. 11. The Application Object
          1. Properties and Methods
            1. The ActiveDocument Property
            2. The ActivePrinter Property
            3. The Build and Version Properties
            4. The ListCommands Method
            5. The OnTime Method
            6. The PrintPreview Property
            7. The Quit Method
            8. The Selection Property
            9. The FontNames Collection
          2. The Options Object
            1. The System Object
          3. The Task Object
          4. The Template Object
            1. The AttachedTemplate Property
            2. Properties and Methods of the Template Object
            3. Creating a Template
          5. The Window Object
            1. Windows, Panes, and Views
            2. The Windows Collection
            3. Creating a New Window
              1. The Add method
              2. The NewWindow method
            4. Properties and Methods of the Window Object
              1. Arranging open windows
              2. Scrolling properties and methods
              3. Special views
              4. Additional properties and methods of the Window object
          6. The Pane Object
            1. Creating New Panes
              1. The Add method
              2. The Split property
              3. The SplitVertical property
            2. Properties and Methods of the Pane Object
              1. The AutoScroll method
          7. The View Object
            1. The Type Property
            2. The Show Properties
          8. The Zoom Object
            1. Properties of the Zoom Object
        4. 12. The Document Object
          1. Properties That Return Collections
          2. Spelling-Related Properties and Methods
          3. The Documents Collection
          4. Adding, Opening, and Saving Documents
          5. Password-Related Properties
          6. Protection-Related Properties and Methods
          7. The Name Properties
          8. Printing-Related Methods
            1. Printing to a File
            2. Restricted Printing
            3. Other Parameters
            4. PrintPreview
          9. Additional Members of the Document Object
          10. Children of the Document Object
            1. The Bookmark Object
              1. Creating a bookmark
              2. The Exists method
              3. The Copy method
              4. The Delete method
              5. The Empty property
              6. The Start, End, and Range properties
              7. The Name property
              8. The Select method
              9. The StoryType property
            2. The Characters, Words, and Sentences Collections
            3. The Fields Collection
              1. Determining field text
              2. The Code and Result properties
              3. The Delete method
              4. The DoClick method
              5. The Index property
              6. The Kind property
              7. The Locked property
              8. The Next and Previous properties
              9. The ShowCodes property
              10. The Type property
              11. The Update method
            4. List-Related Objects
            5. The PageSetup Object
            6. The Paragraph Object
              1. The CloseUp method
              2. The Format property
              3. The Indent method
              4. The Range property
              5. The TabStops property
            7. The ParagraphFormat Object
            8. Tab Stops
              1. The TabStops collection
              2. The TabStop object
            9. The Style Object
              1. Creating a new style
              2. The AutomaticallyUpdate property
              3. The BaseStyle property
              4. The BuiltIn property
              5. The Description property
              6. The InUse property
              7. The Type property
              8. The ParagraphFormat property
            10. The Variable Object
              1. Creating a Variable object
          11. Example: Printing Document Headings
          12. Example: Finding Used Styles
        5. 13. The Section and HeaderFooter Objects
          1. Adding a New Section
          2. The PageSetup Object
          3. Properties of the Section Object
          4. The HeaderFooter Object
            1. Different Odd and Even Headers or Footers
            2. Properties of the HeaderFooter Object
              1. The Exists property
              2. The IsHeader property
              3. The LinkToPrevious property
              4. Adding page numbers to a header or footer
            3. Example: Setting Headers
        6. 14. The Range and Selection Objects
          1. Comparing the Range and Selection Objects
          2. Range and Selection Variables
          3. Creating a Range or Selection Object
            1. The Entire Document
            2. A Single Point
            3. The Range Method and the Range Property
            4. The FormattedText Property
            5. Characters, Words, and Sentences
            6. The GoTo Methods
            7. The ConvertToText Method
            8. The Find Object
            9. The Field Object
            10. The TextRange Property
          4. Changing a Range Object
            1. The Next and Previous Methods
            2. The SetRange Method
            3. The Start and End Properties
            4. The StartOf and EndOf Methods
            5. The HomeKey and EndKey Methods
            6. The Collapse Method
            7. The Expand Method
            8. The Extend Method
            9. The Shrink Method
            10. The WholeStory Method
            11. The Move Methods
            12. The MoveUntil Methods
            13. The MoveWhile Methods
            14. The MoveUp, MoveDown, MoveLeft, and MoveRight Methods
              1. MoveDown or MoveUp
              2. MoveLeft or MoveRight
          5. Range and Selection Object Properties and Methods
            1. The Text and TextRetrievalMode Properties
            2. Edit Methods
            3. Insert Methods
              1. The InsertAfter and InsertBefore methods
              2. The InsertBreak method
              3. The InsertCaption method
              4. The InsertDateTime method
              5. The InsertFile method
              6. The InsertParagraph, InsertParagraphAfter, and InsertParagraphBefore methods
              7. The InsertSymbol method
            4. Character and Paragraph Formatting Properties
              1. Font-related properties
              2. The Borders and Shading properties
              3. The Case property
              4. The PageSetup property
              5. The ParagraphFormat property
              6. The Style property
              7. Keyboard simulation
            5. Miscellaneous Properties and Methods
              1. Information property
              2. Selecting a range
              3. Deleting a range or selection
              4. Duplicating a range
              5. The IsEqual, InRange, and InStory methods
              6. Collection properties
              7. The ConvertToTable method
              8. Sorting text
        7. 15. The Find and Replace Objects
          1. Searching for Text
            1. The Wrap Property
            2. The Found Property
            3. Consequences of a Successful Search
          2. Searching for Formatting
          3. The Replace Operation
          4. The Execute Method
          5. Example: Repeated Searching
        8. 16. The Table Object
          1. Formatting-Related Properties and Methods
          2. The Cell Method
          3. The Columns and Rows Properties
          4. The ConvertToText Method
          5. Sorting Methods
          6. The Split Method
          7. Example: Creating Tables from Word Lists
          8. Example: Closing Up a Table
        9. 17. The List Object
          1. List Types
            1. What Is a List?
            2. Bulleted Lists
            3. Numbered Lists
          2. The List Object
          3. The ListTemplate Object and ListGalleries
            1. ListGallery Objects
            2. The ListTemplate Object
            3. The ListLevel Object
            4. The ListTemplates Collections
            5. The ListFormat Object
              1. The Apply…Default methods
              2. The ApplyListTemplate method
              3. The ConvertNumbersToText method
              4. The CountNumberedItems method
              5. The List property
              6. The ListIndent, ListOutdent, and ListLevelNumber methods
              7. The ListString and ListValue properties
              8. The ListTemplate property
              9. The ListType property
              10. The RemoveNumbers method
              11. The SingleList property
              12. The SingleListTemplate property
          4. Example: Looking at Lists
        10. 18. Shortcut Key Bindings
          1. Finding a Key Binding
          2. Creating a Key Binding
          3. The KeyBinding Object
            1. Finding a Key Binding
            2. The KeysBoundTo Collection
            3. Example: Listing Key Bindings
        11. 19. Built-in Dialog Objects
          1. The Show Method
          2. The Display and Execute Methods
          3. The DefaultTab Property
          4. The Type Property
          5. The Update Method
          6. Example: Printing Document Statistics
        12. 20. Custom Dialog Boxes
          1. What Is a UserForm Object?
            1. Creating a UserForm Object
            2. Windows Controls
            3. Adding UserForm Code
          2. Example: Adding a Closing to a Letter
            1. Add a UserForm Object and Set Its Properties
            2. Add Controls to the Form
            3. Add Code to the Form
            4. Activating the Application
          3. Example: A Fax Cover Sheet
          4. Example: Quick Selection
        13. 21. Menus and Toolbars
          1. An Overview
          2. CustomizationContext
          3. The CommandBars Collection
          4. The CommandBar Object
            1. Creating a New Menubar or Toolbar
          5. Command Bar Controls
            1. Creating a New Command Bar Control
            2. Built-in Command Bar Control IDs
            3. Example: Creating a Menu
            4. Example: Creating a Toolbar
      7. IV. Appendixes
        1. A. Programming Word from Another Application
          1. Setting a Reference to the Word Object Model
          2. Getting a Reference to the Word Application Object
          3. An Alternative Approach
            1. The CreateObject Function
            2. The GetObject Function
            3. No Object Library Reference
        2. B. The Shape Object
          1. The Shape and InLineShape Objects
          2. Z-Order
          3. Creating InLineShapes
          4. Creating Shapes
            1. AutoShapes
            2. The TextFrame Object
            3. The FillFormat Object
          5. Examples
          6. WordArt
        3. C. Getting the Installed Printers
        4. D. High-Level and Low-Level Languages
          1. BASIC
          2. Visual Basic
          3. C and C++
          4. Visual C++
          5. Pascal
          6. FORTRAN
          7. COBOL
          8. Lisp
      8. Index
      9. Colophon

    Product information

    • Title: Writing Word Macros, Second Edition
    • Author(s): Steven Roman PhD
    • Release date: October 1999
    • Publisher(s): O'Reilly Media, Inc.
    • ISBN: 9781565927254