+3 votes
244 views
in Sell ​​on the Internet by (242k points)
reopened
Word macros: how to create and run

1 Answer

+4 votes
by (1.6m points)
edited
 
Best answer

What is a macro in Word?
How do macros work in Word?
Create macros in Word: step-by-step explanation
Word macros: useful script examples
Record an executable macro with a button
Record a macro with a keyboard shortcut
Run a macro in Word
Add a macro button to the ribbon
Import macros into Word
Macros to automatically determine the average length of sentences
Macro to correct typos
Macro code to remove all hyperlinks

image

Word macros: how to create and run

Microsoft Office Suite is one of the most popular office solutions. Using its various applications, we can work with Word, Excel or PowerPoint files that allow us to write texts, calculate mathematical formulas or make presentations. Even as the years go by, this office suite continues to prove its versatility. However, many users are unaware of most of the features and tools it includes. For example, MS Office programs have functionality that allows us to create, record, and run macros to automate certain tasks and processes . In this way, we can save a lot of time and effort, especially when our work involves repeating the same steps and tasks over and over again..

What exactly steps do you have to follow to create macros in Word and what are macros for? We answer these questions below and teach you how to create your own macros and apply them to your documents.

Index
  1. What is a macro in Word?
  2. How do macros work in Word?
  3. Create macros in Word: step-by-step explanation
    1. Record an executable macro with a button
    2. Record a macro with a keyboard shortcut
    3. Run a macro in Word
    4. Add a macro button to the ribbon
    5. Import macros into Word
  4. Word macros: useful script examples
    1. Macros to automatically determine the average length of sentences
    2. Macro to correct typos
    3. Macro code to remove all hyperlinks

What is a macro in Word?

A macro is a series of instructions or commands that are stored so that they can be executed sequentially using a single execution command. They are present in programming and in certain applications of the most common computer programs. Therefore, they can be used by developers and users to create sequences of actions. In the case of Microsoft Word, which is a word processor, macros constitute an essential functionality that allows us to automate a series of tasks when we have to develop activities repeatedly..

So that we can record and run our own Word macros, we have an integrated tool that we can also find in other applications such as Access, Excel or PowerPoint. This tool, to be able to record frames, uses the Visual Basic for Applications (VBA) scripting language that has also been developed by Microsoft.

Note

In the mid-1990s, the event-driven programming language VBA replaced earlier programs created by Microsoft Office that used macros and were incompatible with each other. Visual Basic provided a uniform solution that allowed you to create and run compatible command strings in multiple programs, making it possible, for example, to transfer data from a Word document to an Excel spreadsheet with a single click..

How do macros work in Word?

Creating macros in Word may sound like a chore reserved for experienced programmers. The truth is that most Word users do not usually have advanced skills in code generation and programming languages. However, because the built-in macro tool allows us to program in a graphical environment without writing code, creating and using macros in Word does not require programming knowledge . To create a new macro we must, firstly, start the recording of our macro , then it is necessary to record the steps by clicking on the corresponding commands and, finally, we must stop the recording. When you record a Word macro, the VBA code is generated automatically.

Note

The Visual Basic editor is one more Microsoft Office tool that makes a VBA development environment available to us. Thanks to this tool, it is possible to view and edit the code of the recorded macros.

When you create a new macro in Word, you can assign a key combination or button to it and add it to the Quick Access Toolbar so you can easily launch the macro and run it at any time. To make the macro available in all future documents, add it to the Normal.dotm template .

Create macros in Word: step-by-step explanation

If you want to automate your most used tasks in Word, but don't have VBA programming knowledge, you can make use of the macro recorder . To access the macro toolbar, you must go to the "View" tab, where you will find the "Macros" group. In any case, it is advisable to activate the programmer tools if we want to create and edit macros in Word since, among other things, in this way we will have access to the Visual Basic editor. To activate this optional toolbox, you have to do the following:

  1. Click on the tab? File ?.
  2. Then go to? Options ?
  3. In ? Customize the ribbon ? Y ? Main tabs ?, check the box? Developer ?.
image
Unchecking the? Developer? Box, you can remove it from the Word ribbon at any time.

Once you have activated the "Developer" box, a new tab with that name will automatically appear in the Word interface. When we open this tab, we can see on the left all the actions necessary to write and edit macros. Next, we show you step by step how to create your own macros using this quick access menu and how to run them once they are created.

Record an executable macro with a button

One of the most popular ways to create a new macro in Word is to record an executable macro with a button . To do this, we must access the? Developer? and select? Record macro ?:

image
If we want to open the menu? Record macro? In Word, we can also press [Alt], [W] and [G].

In the "Macro name" box, type a name for the macro. Next on the list? Save macro in ?, Select the document for which you want to create the macro. To use this macro in future documents, in the? Save macro in? you must specify? All documents (Normal.dotm) ?. To have the macro run when a button is clicked, choose? ? Button :

image
In the "Description" box, you have the option to enter specific information about the Word macro you are creating.

In the window on the left, click on the new macro and then on? Add ?. Then click? Modify ? Finally, select a button icon for the new Word macro:

image
When you assign a button to a new macro, you can specify whether you want it to be available for all Word documents (default) or only for a specific document.

To confirm, double-click? Accept ? Now is the time to record the steps. Please click commands or press the keys for each step of the task. Word records clicks and keystrokes, but not mouse movements and gestures. To select text while you are recording the macro you have to use the keyboard (Shift + scroll arrow).

When you're done, you can stop recording by clicking? Stop recording ?:

image
Stop recording? in the developer tools in Word 2016.

The button for the created macro appears on the Quick Access Toolbar:

image
In the quick access menu, the icons associated with the macros are incorporated.

Record a macro with a keyboard shortcut

To record a macro with a keyboard shortcut, we will initially follow the same steps that we followed to record a macro with a button. First, we click on? View? >? Macros? >? Record macro? , we write a name for the macro, and then we specify whether or not we want to use this macro in future documents. To run the macro by pressing a keyboard shortcut, we click? Keyboard? :

image
If we enter a name for the new macro that already exists, Word will ask us if we want to replace the original macro.

The menu? Customize keyboard? Will open. and in it we must select the macro under? Commands ?. Then left click on? New shortcut key? and in that same box type the key combination : Alt + Ctrl + L

image
Word alerts us when a key combination is assigned to another function. If not, the message? Unassigned ?.

To use this keyboard shortcut macro in future documents, make sure the? Save changes to? specify? Normal ?. Then click? Assign ?. When you close the menu, it is time to record the steps.

Run a macro in Word

Once you've created a macro, you can run it whenever you want by clicking a button on the quick access menu or by pressing a combination of keys. You can also run your Word macros from your collection of macros by following these steps:

  1. In the tab? Developer ?, click the button? Macros? or, in the tab? View ?, click the menu? Macros? and then the button? View macros ?.
  2. Select the macro you want from the list by clicking the left mouse button on it.
  3. Click? Run ?
image
If you have enabled the? Developer? Box, you can open the Word macro you want using the Visual Basic editor. To do this, you must click on? Modify ?.

Add a macro button to the ribbon

In case you find that the macro icon visible in the quick access bar is too small or if you want to add a macro shortcut button, Word gives you the option to add it to any tab via the ribbon. . To do this, you must create a new custom group, which is where you are going to add the macro. Then from? File? >? Options? >? Customize the Ribbon? you can select the macro you want.

Create a custom group:

Under? Customize Ribbon ?, click the? Button. New group ?, which appears next to the standard groups on this tab. Then click? Change name ? and enter a name for the custom group. You can also assign an icon to it:

image
Word only displays the selected symbol when the program window is reduced and the group is folded.

Add a macro to a custom group in Word:

Once we have created a custom group for the ribbon, we can add all the Word macros we want to that group. To do this, first you have to go to the list? Commands available in? and select the option? Macros ? Next, select the macro you want to add to your group in the left window and the group you want to add the macro to in the right column and click? Add ? and in? OK ?.

image
If you want to remove a macro from a custom group, you have to select it in the window on the right and click the "Remove" button.

If all went well, the new group should appear on the ribbon:

image
By default, a new group is added to the existing groups that appear on the right in the same tab, although there is the option to modify it to place the button on another tab.

Import macros into Word

We can find the case that we already have macros in VBA, but we need to import them into Word. This happens, for example, when we have created our macros on another device or have been sent to us by someone else . Furthermore, on the Internet, there are several web pages where it is possible to find numerous macros and code. Microsoft Word gives us the option to import those command strings already created so that we can, in this way, use them in Word. For this, the necessary tool is the Visual Basic editor that we have already mentioned previously. To open it, we can use the keyboard shortcut [ Alt] + [F11] or go to the "Developer" tab.

The steps that we must take next will be different depending on whether we are importing a file with macros or macro code.

Import files with ready-to-use macros ( .frm , .bas , .cls ):

If you have a file with macros, you can import it into your Word program in a few steps. The first is to search in the Visual Basic Project Explorer for the document to which we want to add our imported macro. If the Project Explorer is not visible in the code editor, you have to go to the? View? and click? Project Explorer ?. Then click on your current project or on another Word project if you want it to apply to a specific document. If we select the? Normal? Project, the Word macro will be imported into all documents:

image
When you no longer need the browser, you can close it by clicking the? X? located in the upper right corner.

To import the macro, click? File? and then on? Import file ?. You have to specify the location of the macro file and click? Open ? to start the import.

image
If you have created a Word macro, you can also export it as a file using the "Export file" button.

Import code from a macro:

If you only have the VBA code of a macro, the import is done differently. The first thing is to select the document to which you want to add the automated script. To do this, open the Project Explorer and double-click on? Normal? (so the macro will be saved in the general template) or in the input? ThisDocument ? (subfolder of? Microsoft Word Objects?):

image
When you select the "Normal" project, Word saves the macro in the general template Normal.dotm.

A code window will appear which is where we are going to copy the macro code. Then, we click on? Save ?. If you selected a specific Word document in the previous step, you will be informed that it needs to be saved as? Word document with macros ?. To do this, click on? No? and select the correct option under? File type ?. When you're done, click? Save ? to create the new file format:

image
When we save an imported macro in our current Word document, it is necessary to modify the format of our current document, which will not be saved in the general template.

Word macros: useful script examples

Now that you know what a Word macro is and that you have learned how to create, run and import macros, it is time to show you some concrete examples of macros in Word so that you can see, through a practical case, the end of these tools automation with commands in the Microsoft word processor.

Macros to automatically determine the average length of sentences

There are several web tools that allow us to analyze our texts in order to determine, for example, what is the average length of the sentences we use. But, if we have the appropriate macro, Word can calculate it directly. Next, we include a code that is used to automatically capture all the phrases in a document and divide the total number of words by the number of phrases. This macro uses the text message ? Average words per phrase? to display the average length:

  Sub Contador de palabras() Dim s As Range Dim numWords As Integer Dim numSentences As Integer numSentences = 0 numWords = 0 For Each s In ActiveDocument.Sentences numSentences = numSentences + 1 numWords = numWords + s.Words.Count Next MsgBox "Promedio de palabras por frase: " + Str(Int(numWords / numSentences)) End Sub  

Macro to correct typos

Anyone who has to write texts from the computer frequently knows that it is very easy to make a mistake when typing words. Correcting these errors is simple, but with the following macro you will only have to place the cursor in front of the exchanged characters . When you run the macro by keyboard combination or by clicking a button, the characters will be corrected automatically.

  Sub Error tipográfico() Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend Selection.Cut Selection.MoveRight Unit:=wdCharacter, Count:=1 Selection.Paste End Sub  

Macro code to remove all hyperlinks

In Word, when we enter web pages or email addresses, links to the corresponding web destinations are automatically created . If you don't want those hyperlinks to appear in your Word document, you have to delete them one by one. The following macro example in Word will save you a lot of work since it automatically removes the first hyperlink it detects in the document . You just have to run the macro until all the links and links are gone:

  Sub SinHipervínculos() 'On Error Resume Next Dim x As Variant For Each x In ActiveDocument.Hyperlinks Selection.WholeStory Selection.Range.Hyperlinks(1).Delete Next x End Sub  

Please, take into account the legal notice related to this article.


...