Word VBA

This is a collection of recipes for scripting Microsoft Word using Visual Basic for Applications.

Macro Recording

A great way of learning about Word VBA is using its macro recording function. With the function, you tell Word to start recording, then perform various steps as if you were working without a macro recorder, and finally, tell Word to stop recording. VBA code corresponding to what you did using Word GUI has been recorded by Word. While the code often cannot be meaningfully used without a modification, by starting from it and modifying it you can save a lot of time that would otherwise be spent reading the VBA documentation.

Menu paths:

  • Word 2007: View (tab) > Macros (group) > down-pointing triangle below Macros button > Record Macro
  • Word 2007: Developer (tab) > Code (group) > Record Macro

Links:

Text Editing

You can insert and delete text as follows:

Selection.TypeText Text:="Inserted as if by typing on keyboard"
Selection.Delete 'Deleted the single char after cursor, or a non-empty selection

Moving Cursor

You can move cursor around as follows:

Selection.MoveDown Unit:=wdLine
Selection.MoveRight Unit:=wdCell 'At the end of a row, moves to the next row

Selecting

You can select regions of text as follows:

Selection.EndKey Unit:=wdLine, Extend:=wdExtend

Formatting

You can format text including text color, background color, and font properties as follows:

Selection.Font.Color = RGB(0, 0, 255) 'Foreground color AKA text color
Selection.Range.HighlightColorIndex = wdYellow 'Background color as highlight
Selection.Font.Name = "Verdana" 'Font face
Selection.Font.Size = 8 'Font size
Selection.Font.Bold = True 'Or False
Selection.Font.Bold = wdToggle
Selection.Font.Italic = True
Selection.Font.Underline = True

Copying and Pasting

You copy and paste as follows:

Selection.Copy
Selection.Paste

Clipboard

Prerequisites: Accessing the clipboard from a Word document requires that a reference to MSForms (Microsoft Forms Object Library) is set in the document. You can set the reference by adding and subsequent removing of a user form, via Insert > UserForm in a pop-up menu. To check the presence of a reference, see Tools > References menu.

Placing text on the clipboard:

Set MyClipboard = New MSForms.DataObject
MyClipboard.SetText "My string"
MyClipboard.PutInClipboard

Getting text from the clipboard:

Set MyClipboard = New MSForms.DataObject
MyClipboard.GetFromClipboard
TextContent = MyClipboard.GetText

Links:

  • DataObject Class at msdn.microsoft.com; contains a section on Visual Basic, whose applicability to Word VBA is unclear.

Various

Sub PasteTabSeparatedPlainTextToTable()
  'This paste prevents loss of formatting of the table cells
  
  Set MyClipboard = New MSForms.DataObject
  MyClipboard.GetFromClipboard
  TextContent = MyClipboard.GetText
 
  SplitArray = Split(TextContent, vbNewLine)
  For Each Element In SplitArray
    SplitArray2 = Split(Element, vbTab)
    TabSkipNeeded = False
    Set OldSelection = Selection.Range
    For Each CellContent In SplitArray2
      If TabSkipNeeded Then
        Selection.MoveRight Unit:=wdCell
      Else
        TabSkipNeeded = True
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend
      End If
      Selection.TypeText Text:=CellContent
    Next
    OldSelection.Select
    Selection.MoveDown Unit:=wdLine
  Next
End Sub
Category:Subject:Microsoft software/all books Category:Subject:Computer software/all books Category:Subject:Computing/all books Category:Subject:Books by subject/all books Category:Subject:Office suites#Word%20VBA Category:Subject:Office suites/all books Category:Subject:Computer software/all books Category:Subject:Computing/all books Category:Subject:Books by subject/all books Category:Book:Word VBA#%20 Category:Book:Wikibooks Stacks/Books#Word%20VBACategory:Shelf:Microsoft software Category:Shelf:Microsoft software/all books Category:Shelf:Computer software/all books#Computer%20software Category:Department:Computing/all books#ComputingCategory:Shelf:Office suites Category:Shelf:Office suites/all books Category:Shelf:Computer software/all books#Computer%20software Category:Department:Computing/all books#Computing Category:One-page books#Word%20VBA Category:Partly developed booksCategory:Books by completion status/all books
Category:Book:Wikibooks Stacks/Books Category:Book:Word VBA Category:Books by completion status/all books Category:Department:Computing/all books Category:One-page books Category:Partly developed books Category:Shelf:Computer software/all books Category:Shelf:Microsoft software Category:Shelf:Microsoft software/all books Category:Shelf:Office suites Category:Shelf:Office suites/all books Category:Subject:Books by subject/all books Category:Subject:Computer software/all books Category:Subject:Computing/all books Category:Subject:Microsoft software Category:Subject:Microsoft software/all books Category:Subject:Office suites Category:Subject:Office suites/all books