Welcome to the Word VBA Tutorial. VBA is a great tool not only to be leveraged in MS Excel. Often it is worth to save some time doing repeatable tasks by adopting some VBA macros in Word or PowerPoint too. Today I wanted to focus a little bit on starting you off in Word VBA macro programming.
When moving to macro programming in Word VBA you will stumble upon issues you would normally not expect in Excel. Lets take the example of moving around the Word file – in Excel you have spreadsheets which are easy to navigate around. In Word, however, you have a lot of different content e.g. text, objects like images and charts, tables etc. Navigating around Word file in VBA is the greatest challenge you will face. Today I would like to focus on that and other frequently used features in Word VBA. So let’s kick off this Word VBA Tutorial.
VBA Word Navigating
Let’s start with adding content to the most common places the start and end of a Word document in VBA. Know if you Google for this you will get tons of non-sense methods of navigating around Word files. I was truly amazed at how poorly it is documented.
Beginning and End of the Word Document
Go to the Beginning of a Word Document:
'Start - add text to the beginning of the Word Document Dim startMark As Range Set startMark = ActiveDocument.Range(0, 0) startMark.Text = "This is the start of the document"
Go to the End of a Word Document:
'End - add text to the end of the Word Document Dim endMark As Range Set endMark = ActiveDocument.Range(Len(ActiveDocument.Range)) endMark.Text = "This is the end of the document"
Finding and replacing text in a Word Document with VBA
Finding and replacing text are basic functions that you will probably need to leverage every now and then.
'Find and print text in MsgBox Dim selectText As Range Set selectText = ActiveDocument.Content selectText.Find.Execute "Hello" If selectText.Find.Found Then MsgBox selectText.Text End If
'Find and replace all instances of a specific text Dim replaceText As Range Set replaceText = ActiveDocument.Content replaceText.Find.Execute FindText:="Hello", ReplaceWith:="Goodbye", Replace:=wdReplaceAll
VBA Word Text formatting
One of the first things you would want to do is probably text formatting in Word VBA.
Let’s start by adding some text to our document:
'Select the beginning of the document ActiveDocument.Range(0).Select 'Type "Hello World!" Selection.TypeText Text:="Hello World!"
Bold & Italic
To change the font weight to bold see below:
'Select the word "Hello" ActiveDocument.Range(0, 5).Select 'Toggle the bold property Selection.Font.Bold = wdToggle
To change the text decoration to italic see below:
'Select the word "World" ActiveDocument.Range(0, 5).Select 'Toggle the bold property Selection.Font.Bold = wdToggle
Below the final result of the code above:
Font size and name
Using the “Hello World!” example above we can similarly change the text font name and font size as shown below:
'Change font size Selection.Font.Size = 20 'Size: 20 Selection.Font.Grow 'Size: 22 Selection.Font.Shrink 'Size: 20 'Change font name to "Aharoni" Selection.Font.Name = "Aharoni"
VBA Word Tables
When editing Word files you might want to leverage tables as it is much easier to navigate around them in an automated way. My approach is to insert/modify tables in Word without and borders (invisible). This way you can guarantee a consistent and easy to navigate structure in Word. Let’s go through some of the basic functions around tables.
Add a table
Let’s add a table to the beginning of the Word document:
Dim begin As Range Set startMark = ActiveDocument.Range(0, 0) 'range as Range, NumRows as Long, NumColumns as Long Call ActiveDocument.Tables.Add(startMark, 3, 6) 'add to beginning of doc, 3 rows, 6 cols
Edit cell text
'Modify cell (1,3) to "Hello World!" With ActiveDocument.Tables(1) .Cell(1, 3).Range.Text = "Hello World!" End With
Working on rows and columns
With ActiveDocument.Tables(1) ' Modify height of row 1 .Rows(1).Height = CentimetersToPoints(0.65) ' Modify width of column 1 .Columns(1).Width = CentimetersToPoints(2.54) 'Merge cell (1,3) with cell (1,4) - cells must be next to each other .Cell(1, 3).Merge .Cell(1, 4) End With
'Modify row 1 border, single, black and 100pt With ActiveDocument.Tables(1).Rows(3).Borders(wdBorderBottom) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth100pt .Color = wdColorBlack End With