You are previewing Mastering VBA for Microsoft Office 2007, 2nd Edition.
O'Reilly logo
Mastering VBA for Microsoft Office 2007, 2nd Edition

Book Description

Even if you're not a programmer, you can quickly learn to write macros, automate tasks, and create custom applications for Office 2007 with Microsoft's Visual Basic for Applications (VBA) and the in-depth instruction in this comprehensive guide. You'll jump right into the basics of recording and running macros with Office's built-in Macro Recorder, before quickly moving to the essentials of VBA syntax, using loops and functions, building effective code, and programming applications in Word, Excel, PowerPoint, Outlook, and Access. Includes pages of real-world examples and techniques.

Table of Contents

  1. Copyright
  2. Dear Reader
  3. Dedication
  4. Acknowledgments
  5. About the Author
  6. Introduction
  7. Recording Macros and Getting Started with VBA
    1. Recording and Running Macros in the Microsoft Office Applications
      1. What Is VBA and What Can You Do With It?
      2. Understanding Macro Basics
      3. Recording a Macro
      4. Running A Macro
      5. Recording a Sample Word Macro
      6. Recording A Sample Excel Macro
      7. Assigning A Way Of Running The Macro
      8. Deleting a Macro
      9. The Bottom Line
    2. Getting Started with the Visual Basic Editor
      1. Opening The Visual Basic Editor
      2. Using The Visual Basic Editor's Main Windows
      3. Setting Properties For A Project
      4. Customizing The Visual Basic Editor
      5. Closing The Visual Basic Editor And Returning To The Host Application
      6. The Bottom Line
    3. Editing Recorded Macros
      1. Testing a Macro in the Visual Basic Editor
      2. Editing the Word Macro
      3. Editing The Excel Macro
      4. Editing a Powerpoint Macro
      5. The Bottom Line
    4. Creating Code from Scratch in the Visual Basic Editor
      1. Setting Up The Visual Basic Editor For Creating The Procedures
      2. Creating a Proedure for Word
      3. Creating A Procedure For Excel
      4. Creating a Procedure for Powerpoint
      5. Creating a Procedure for Access
      6. The Bottom Line
  8. Learning How to Work With VBA
    1. Understanding the Essentials of VBA Syntax
      1. Getting Ready
      2. Procedures
      3. Statements
      4. Keywords
      5. Expressions
      6. Operators
      7. Variables
      8. Constants
      9. Arguments
      10. Objects
      11. Collections
      12. Properties
      13. Methods
      14. Events
      15. The Bottom Line
    2. Working with Variables, Constants, and Enumerations
      1. Working With Variables
      2. Working with Constants
      3. Working with Enumerations
      4. The Bottom Line
    3. Using Array Variables
      1. What Is an Array?
      2. Declaring an Array
      3. Storing Values In An Array
      4. Multidimensional Arrays
      5. Declaring a Dynamic Array
      6. Redimensioning an Array
      7. Returning Information from an Array
      8. Erasing an Array
      9. Finding Out Whether a Variable Is an Array
      10. Finding the Bounds of an Array
      11. Sorting an Array
      12. Searching Through an Array
      13. The Bottom Line
    4. Finding the Objects, Methods, and Properties You Need
      1. What Is an Object?
      2. Working with Collections
      3. Finding the Objects You Need
      4. Using object Variables to Represent objects
      5. The Bottom Line
  9. Making Decisions and Using Loops and Functions
    1. Using Functions
      1. what Is a Function?
      2. Using Functions
      3. Using Functions to Convert Data from One Type to Another
      4. Using Functions to Manipulate Strings
      5. Using VBA's Mathematical Functions
      6. Using VBA's Date and Time Functions
      7. Using File-Management Functions
      8. The Bottom Line
    2. Creating Your Own Functions
      1. Components of a Function
      2. Creating a Function
      3. Examples of Functions for Any VBA-Enabled Application
      4. Creating a Function for Word
      5. Creating a Function for Excel
      6. Creating a Function for Powerpoint
      7. Creating a Function for Access
      8. The Bottom Line
    3. Making Decisions in Your Code
      1. How Do You Compare Things in VBA?
      2. Testing Multiple Conditions by Using Logical Operators
      3. If Statements
      4. Select Case Statements
      5. The Bottom Line
    4. Using Loops to Repeat Actions
      1. When Should You Use a Loop?
      2. Understanding the Basics of Loops
      3. Using For... Loops for Fixed Repetitions
      4. Using Do... Loops for Variable Numbers of Repetitions
      5. While... Wend Loops
      6. Nesting Loops
      7. Avoiding Infinite Loops
      8. The Bottom Line
  10. Using Message Boxes, Input Boxes, and Dialog Boxes
    1. Getting User Input with Message Boxes and Input Boxes
      1. Opening a Procedure to Work on
      2. Displaying Status Bar Messages in Word and Excel
      3. Message Boxes
      4. Input Boxes
      5. Forms: When Message Boxes and Input Boxes Won't Suffice
      6. The Bottom Line
    2. Creating Simple Custom Dialog Boxes
      1. When Should You Use a Custom Dialog Box?
      2. Creating a Custom Dialog Box
      3. Linking a Dialog Box to a Procedure
      4. Retrieving the User's Choices from a Dialog Box
      5. Examples of Connecting Dialog Boxes to Procedures
      6. Using an Application's Built-in Dialog Boxes from VBA
      7. The Bottom Line
    3. Creating Complex Dialog Boxes
      1. Creating and Working with Complex Dialog Boxes
      2. Using Events to Control Forms
      3. The Bottom Line
  11. Creating Effective Code
    1. Building Modular Code and Using Classes
      1. Creating Modular Code
      2. Creating And Using Classes
      3. The Bottom Line
    2. Debugging Your Code and Handling Errors
      1. Principles of Debugging
      2. The Different Types of Errors
      3. VBA'S Debugging Tools
      4. Dealing With Infinite Loops
      5. Dealing With Runtime Errors
      6. Suppressing Alerts
      7. Handling User Interrupts in Word, Excel, and Project
      8. Documenting Your Code
      9. The Bottom Line
    3. Building Well-Behaved Code
      1. What Is a Well-Behaved Procedure?
      2. Retaining or Restoring the User Environment
      3. Leaving The User in The Best Position to Continue Working
      4. Keeping the User Informed During The Procedure
      5. Making Sure the Procedure Is Running Under Suitable Conditions
      6. Cleaning Up after a Procedure
      7. The Bottom Line
    4. Securing Your Code with VBA's Security Features
      1. Understanding How VBA Implements Security
      2. Signing Your Macro Projects With Digital Signatures
      3. Choosing a Suitable Level of Security
      4. Locking Your Code
      5. The Bottom Line
  12. Programming the Office Applications
    1. Understanding the Word Object Model and Key Objects
      1. Examining the Word Object Model
      2. Working with the Documents Collection and the Document Object
      3. Working with the Selection Object
      4. Creating and Using Ranges
      5. Manipulating Options
      6. The Bottom Line
    2. Working with Widely Used Objects in Word
      1. Using Find and Replace via VBA
      2. Working with Headers, Footers, and Page Numbers
      3. Working with Sections, Page Setup, Windows, and Views
      4. Working with Tables
      5. The Bottom Line
    3. Understanding the Excel Object Model and Key Objects
      1. Getting an Overview of the Excel Object Model
      2. Understanding Excel'S Creatable Objects
      3. Working with Workbooks
      4. Working with Worksheets
      5. Working with the Active Cell or Selection
      6. Working with Ranges
      7. Setting Options
      8. The Bottom Line
    4. Working with Widely Used Objects in Excel
      1. Working with Charts
      2. Working with Windows
      3. Working with Find and Replace
      4. Adding Shapes
      5. The Bottom Line
    5. Understanding the PowerPoint Object Model and Key Objects
      1. Getting an Overview of the Powerpoint Object Model
      2. Understanding Powerpoint'S Creatable Objects
      3. WOrking With Presentations
      4. WOrking with Windows and Views
      5. WOrking With Slides
      6. WOrking With Masters
      7. The Bottom Line
    6. Working with Shapes and Running SLide Shows
      1. WOrking With Shapes
      2. WOrking with Headers and Footers
      3. Setting Up and Running a Slide Show
      4. The Bottom Line
    7. Understanding the Outlook Object Model and Key Objects
      1. Getting an Overview of the Outlook Object Model
      2. Working with the Application Object
      3. Understanding General Methods for Working with Outlook Objects
      4. Working with Messages
      5. WOrking with Calendar Items
      6. Working with Tasks and Task Requests
      7. Searching for Items
      8. The Bottom Line
    8. Working with Events in Outlook
      1. WOrking with Application-Level Events
      2. WOrking with Item-Level Events
      3. The Bottom Line
    9. Understanding the Access Object Model and Key Objects
      1. Getting Started with VBA in Access
      2. Getting an Overview of the Access Object Model
      3. Understanding Creatable Objects in Access
      4. Opening and Closing Databases
      5. Working with the Screen Object
      6. Using the Docmd Object to Run Access Commands
      7. The Bottom Line
    10. Manipulating the Data in an Access Database via VBA
      1. Understanding How to Proceed
      2. Preparing to Access the Data in the Database
      3. Opening a Recordset
      4. Accessing a Particular Record in a Recordset
      5. Searching for a Record
      6. Returning the Fields in a Record
      7. Editing a Record
      8. Inserting and Deleting Records
      9. Closing a Recordset
      10. The Bottom Line
    11. Accessing One Application from Another Application
      1. Understanding the Tools for Communicating with Other Applications
      2. Using Automation to Transfer Information
      3. Using the Shell Function to Run an Application
      4. Using Data Objects to Store and Retrieve Information
      5. Communicating via DDE
      6. Communicating via SendKeys
      7. The Bottom Line
    12. Programming the Office 2007 Ribbon
      1. Hiding the Editing Group on the Word Ribbon
      2. Working with Excel and PowerPoint
      3. Undoing Ribbon Modifications
      4. Selecting the Scope of Your Ribbon Customization
      5. Adding a New Group
      6. Adding Callbacks
      7. Adding Attributes
      8. Using Menus and Lists
      9. Toggling with a Toggle Button Control
      10. Modifying the Ribbon in Access
      11. Adding a Callback in Access
      12. What to Look For if Things Go Wrong
      13. Where to Go from Here
      14. The Bottom Line
  13. The Bottom Line Solutions
    1. Chapter 1: Recording and Running Macros in theMicrosoft Office Applications
    2. Chapter 2: Getting Started with the Visual Basic Editor
    3. Chapter 3: Editing Recorded Macros
    4. Chapter 4: Creating Code from Scratch in the Visual Basic Editor
    5. Chapter 5: Understanding the Essentials of VBA Syntax
    6. Chapter 6:Working with Variables, Constants, and Enumerations
    7. Chapter 7: Using ArrayVariables
    8. Chapter 8: Finding theObjects,Methods, and Properties YouNeed
    9. Chapter 9: Using Functions
    10. Chapter 10: Creating Your Own Functions
    11. Chapter 11: Making Decisions in Your Code
    12. Chapter 12: Using Loops to Repeat Actions
    13. Chapter 13: Getting User Input with Message Boxes and Input Boxes
    14. Chapter 14: Creating Simple Custom Dialog Boxes
    15. Chapter 15: Creating Complex Dialog Boxes
    16. Chapter 16: Building Modular Code and Using Classes
    17. Chapter 17: Debugging Your Code and Handling Errors
    18. Chapter 18: Building Well-Behaved Code
    19. Chapter 19: Securing Your Code with VBA's Security Features
    20. Chapter 20: Understanding the Word Object Model and Key Objects
    21. Chapter 21: Working with Widely Used Objects in Word
    22. Chapter 22: Understanding the Excel Object Model and Key Objects
    23. Chapter 23: Working with Widely Used Objects in Excel
    24. Chapter 24: Understanding the Powerpoint Object Model and Key Objects
    25. Chapter 25: Working with Shapes and Running Slide Shows
    26. Chapter 26: Understanding the Outlook Object Model and Key Objects
    27. Chapter 27: Working with Events in Outlook
    28. Chapter 28: Understanding the Access Object Model and Key Objects
    29. Chapter 29: Manipulating the Data in an Access Database via VBA
    30. Chapter 30: Accessing One Application from Another Application
    31. Chapter 31: Programming the Office 2007 Ribbon
  14. Glossary
    1. A
    2. B
    3. C
    4. D
    5. E
    6. F
    7. G
    8. H
    9. I
    10. K
    11. L
    12. M
    13. O
    14. P
    15. R
    16. S
    17. T
    18. U
    19. V
    20. W