Microsoft Word: Visual Basic for Applications
Revision: 8/11/2011 Page 1 of 59
Copyright 2002–2011 by Susan J. Dorey
See also:
Access Techniques for VBA code techniques, some is specific to Access, some are general.
This document presumes you know something about VBA. It is not a primer. It is my personal resource.
CONTENTS
About Macros..................................................3
Organizing Macros ..............................................3
Export and Import Code .....................................3
Get to Know the Object Model ...........................3
Miscellaneous Subjects ....................................4
Anatomy of a Word Document ...........................4
Moving Around in a Word Document ................4
Working with Documents ...................................6
Working with Templates .....................................7
Working With Files.............................................7
Working With Styles ...........................................7
Working With Path.............................................8
Working With Text.............................................9
Portrait and Landscape ......................................13
Document Properties.........................................14
Page Numbering................................................15
Lists...................................................................16
Working with Tables.........................................16
Addressing Table Cells...........................................19
Dialog Boxes..................................................20
Message Box Object...........................................20
Present Information...............................................21
Prompt User for Choices ........................................21
Dialog Object....................................................21
Prompt User for File(s) or Directory with
FileFind Dialog – OBSOLETE.........................
22
FileDialog Object ..............................................23
Prompt User to Select Folder with FileDialog
FolderPicker.....................................................
24
Documenting Your Shortcut Keys.................24
About Keyboard Shortcut Keys..........................25
Run This Code..................................................25
DOM Background ............................................26
Field Codes in VBA.......................................27
Index Object......................................................28
Table of Contents..............................................29
RD Field Code ..................................................29
Field Codes and Page Numbers .........................30
Page Headers and Footers............................. 30
Assembling Multi-file Documents ................ 32
Approach...........................................................32
Code Samples ....................................................33
Prompt User for Directory of New File................... 33
Insert Chapter Files............................................... 33
Insert Section Break Odd Page............................... 34
Update Main Table of Contents ............................ 34
Update Main Table of Figures............................... 34
Save New Document ............................................ 34
Update Chapter Tables of Contents........................ 34
Update Indices ..................................................... 34
Delete Macros in New Document .......................... 35
Subroutines.......................................................... 35
Variations............................................................ 36
Shapes ........................................................... 36
About Shapes.....................................................36
Anchoring a Shape................................................ 36
Positioning a Shape .............................................. 37
Formatting a Shape .............................................. 37
Other Important Shape Properties.......................... 37
Remarks............................................................... 37
Converting Visio Picture into Inline Shape ........38
Key Properties....................................................38
RelativeVerticalPosition Property ........................... 38
RelativeHorizontalPosition Property....................... 38
Top Property ........................................................ 38
Left Property ........................................................ 39
Shrink Inline Shapes ..........................................40
Watermarks .................................................. 42
Background Printed Watermark ........................42
Watermark as Text Box .....................................43
Code Created by Insert Print Watermark
Background Wizard.......................................
44
Iterative Document Editing .......................... 45
Reformat Text in Square Brackets......................45
Insert RD Field Codes .......................................46
Change Styles in all Word Files in a Given
Directory.......................................................
49
Passwords and Protection ............................. 50