Last update: October 2019
Applicability: Word 365, 2019, 2016; Windows and MAC OS X operating systems.
Here’s a question from a reader::
I am a teacher and I am using Word 2019 to write important documents. I repeatedly need to change the font and size of my document paragraphs. Is there a way to automate my work with Macros, so i can modify my document paragraph with a one click? I am sure this is going to save me a couple of hours per week, which i’ll gladly invest in other tasks.
Thanks for your question. The key advantage of Macros is that they allow you automate your tasks in Microsoft Office applications. Macros can be either recorded or manually coded using Visual Basic for Applications (VBA). Word ships a Macro recorder that allows you to develop automation scripts without coding. If you are just starting out with automation, this might be a good enough solution. That said, with a little bit of VBA knowledge, you can write efficient and powerful Macros.
If you don’t see the Developer tab in your Microsoft Word Ribbon, you’ll need to setup your Macro development environment.
Note: This is an optional step that should be attempted after you have followed the Macro recording tutorial above.
OK, so far we got our Macro basics working. Now it’s time to improve a bit our Macro usability in the Word doc. Let me show you how you can associate your Macro with a quick access button, so you can launch it more easily.
Note: You are able to associate your Word Macro not only with quick access buttons but also with command buttons embedded in your document and specific custom keyboard shortcuts.
With some simple Visual Basic for Applications coding skills we can edit the macros and write programs to automate various kinds of works using VBA.
Aside Note: FYI – some of the Microsoft Office applications, such as Powerpoint and Outlook, do not have built in macro recorders. Therefore, writing VBA is mandatory for writing Powerpoint macros and automating Outlook.
Selection.Font.Color = 16737792
Since publishing this tutorial, many readers asked for more in depth examples of Word Macros. So here we go with the first batch.
Sub CreateNewDoc() 'This small snippet first creates a new document, then it checks whether a document with the same name already exists before saving. Dim myDoc As New Document Dim filePath As String 'Modify your file path as needed filePath = "C:\MyNewDoc.docx" Set myDoc = Documents.Add With myDoc If Dir(filePath) = "" Then .SaveAs2 (filePath) Else 'You have already an existing document MsgBox ("Please use a different file name") End If End With myDoc.Close SaveChanges:=wdPromptToSaveChanges End Sub
Note: When creating new documents, you are able to specify the template (.dotm/.dotx files) you would like to use. Templates are typically stored at: C:\Users\<your_user_name>\AppData\Roaming\Microsoft\Templates
Sub OpenDoc() 'This code checks whether your document exists and then opens it filePath = "C:\MyNewDoc.docx" If Dir(filePath) = "" Then MsgBox ("file doesn't exist") Else Documents.Open (filePath) End If End Sub
Sub CloseDoc() 'This code closes a specific document filePath = "C:\MyNewDoc.docx" Documents(filePath).Close SaveChanges:=wdPromptToSaveChanges End Sub
Sub CloseAllDocs() 'This code closes all opened documents in your computer Documents.Close SaveChanges:=wdPromptToSaveChanges End Sub
Sub SaveAsPdf() 'This code saves a word document in a PDF format FileName = Left(CStr(ActiveDocument.Name), Len(CStr(ActiveDocument.Name)) - 5) ActiveDocument.SaveAs2 FileName:="c:\" + FileName + ".pdf", FileFormat:=wdFormatPDF End Sub
This code sets the header and footer of your Word document first page.
Sub InsertHeaderFooterFirstPage() Dim myDoc As Document Dim headerText As String Dim footerText As String Set myDoc = ActiveDocument 'Replace the header and footer text as needed headerText = "This document was written by you" footerText = "All rights reserved to you" With myDoc.Sections(1) 'We first ensure that we can set different header and footer texts .PageSetup.DifferentFirstPageHeaderFooter = True 'Setting the header and footer texts .Headers(wdHeaderFooterFirstPage).Range.Text = headerText .Footers(wdHeaderFooterFirstPage).Range.Text = footerText End With End Sub
Here are a few more ideas which we’ll be posting in the upcoming future.
This concludes our tutorial for today. As you just saw, there is a lot to be done with the Word macro recorder and VBA Macros in Word. In case you are looking for more specific help that goes beyond the scope of this tutorial, kindly contact us via our contact form.