如果您经常使用Microsoft Word并发现自己重复做同样的事情,请创建一个宏. Macro是macroinstruction这个词的缩写,意思是完成一项任务的一系列指令。
在Word(Word)中创建宏的好处是您不需要知道如何编程(know how to program)。如果您可以按记录并停止,则可以执行此操作。
宏不危险吗?(Aren’t Macros Dangerous?)
您是否听说过宏之所以不好是因为它们可能包含病毒?尽管您必须小心打开来自未知人员的Office文档,因为它们可能包含恶意宏,但这不是问题。您制作自己的宏,因此您知道它不是病毒。
如何在 Word 中录制宏(How To Record A Macro in Word)
对于此示例,您将在Word中创建一个宏以在文档末尾插入您的签名。(insert your signature)
- 打开 Word,转到“查看”(View )选项卡。
- 选择宏(Macros )按钮下的向下箭头。
- 选择录制宏...(Record Macro… )将打开一个新窗口。
- 在宏名称:(Macro name: )字段中,为宏输入一个有意义的名称。不允许有空格。使用下划线或破折号。
- 在将宏存储在:(Store macro in:)下拉列表中,您可以选择要使用此宏的文档。如果您选择所有文档(Documents)( Normal.dotm ),那么您从现在开始制作的每个新Word文档中都可以使用该宏。如果您选择单个文档,它将仅适用于该单个文档。通常最好选择All Documents。
- 在描述:(Description: ) 字段中,写下宏的作用。这是一个带有描述性名称的简单宏,但是当您对宏有信心时,您会做更复杂的事情,所以描述总是一个好主意。
- 您可以选择将宏分配给(Assign macro to)您将使用按钮创建的按钮(Button )或您可以使用键盘(Keyboard )按钮选择的热键。Word已经有很多快捷键,所以最好使用一个按钮。选择按钮(Button)。将打开一个名为Word Options的新窗口。
- 在这里,您将在Word Ribbon上为宏分配一个按钮。选择自定义功能区(Customize Ribbon)。
- 在Choose commands from:下拉菜单中,选择Macros。这将向我们展示其下方区域中的宏。
- 您需要在功能区的某个位置放置宏。对于此示例,请在Main Tabs区域中选择Home 。然后选择新组(New Group)。
- 选择重命名(Rename),以便您可以给它一个有意义的名称。
- 在重命名(Rename)窗口中,选择代表组的图标之一,然后在显示名称(Display name )字段中输入我的宏。(My Macros )
- 选择确定(OK)以应用它。您将在主选项卡(Main Tabs )区域中看到更改。
- 在Main Tabs区域中选择新的My Macros后,选择(My Macros)Normal.NewMacros.Insert_Signature宏。
- 选择添加(Add)将其插入我的宏(My Macros)组。
- 当然,你会想要重命名它。在主选项卡(Main Tabs)区域中选择宏后,选择重命名...(Rename…)
- 在重命名(Rename)窗口中,选择一个图标并在显示名称:(Display name: )字段中输入一个简单的名称。
- 选择确定(OK )以提交更改。您将在主选项卡(Main Tabs)区域中看到更改。
- 选择Main Tabs区域下方的OK以完成为宏创建按钮。
- 继续创建您想要的任何签名块。添加(Add)您的签名或其他图像的扫描。随便你。完成后,选择宏(Macros)按钮下的向下箭头。
- 选择停止录制(Stop Recording)。就是这样。您的插入签名(Signature)宏已制作完成。
- 通过找到您在“主页”选项卡中创建的“(Home)插入签名”(Insert Signature)按钮并选择它来测试它。您的签名块应该会像您设计的那样神奇地出现。
如何编写宏(How To Write a Macro)
对于大多数宏,录制方法是创建它们的最有效方法。最终,你会想做一些更复杂的事情。这需要使用Visual Basic for Applications ( VBA ) 语言编写宏。我们为初学者准备了最好的 VBA 指南(best VBA guide for beginners),所以请收藏它。让我们创建一个宏来为我们提供每个句子的平均单词数,以帮助我们判断我们什么时候是罗嗦的。
- 您需要访问默认情况下隐藏在Word中的(Word)Developer选项卡。在Word(Word)的左上角,选择File。
- 在左下角附近,选择选项(Options)。
- 在打开的窗口中,选择自定义功能区(Customize Ribbon)。
- 在右侧,查找Developer并在其旁边打勾。
- 选择确定(OK)关闭窗口。现在将显示开发人员(Developer )选项卡。
- 选择开发人员(Developer )选项卡。
- 选择宏(Macros)按钮。
- 在宏名称中输入一个有意义的名称:(Macro name:)将宏保留在:(Macros in: )作为Normal.dotm,这样它将适用于所有Word文档。始终在(Always)描述:(Description: )区域中输入描述。
- 选择创建(Create)。Microsoft Visual Basic for Applications开发工具将打开。
- 下面绿色矩形中显示的代码应该已经存在,它们之间有一些空格。将以下代码复制并粘贴到该空间中:
Dim s As Range
Dim numWords As Integer
Dim numSentences As Integer
numSentences = 0
numWords = 0
对于 ActiveDocument.Sentences 中的每个
numSentences = numSentences + 1
numWords = numWords + s.Words.Count
Next
MsgBox “每个句子的平均单词” + Str ( Int (numWords / numSentences)) + “. 最好少于 15 个。”
- 选择运行(Run )按钮以测试代码。如果它没有按预期运行,请编辑代码,直到它以您想要的方式运行。
- 选择保存(Save )按钮并关闭开发窗口。
- (Create a button in the Word ribbon)使用上面“如何录制宏”说明中所示的相同方法在 Word 功能区中创建一个按钮。
- 找到您刚刚创建的按钮并对其进行测试。
保存 Word 文档时,我的宏不起作用(My Macro Doesn’t Work When I Save My Word Document)
Word默认使用文件扩展名.docx保存,这不允许宏自动运行。如果您希望宏运行,您需要将文件类型更改为Word 启用宏的文档(Word Macro-Enabled Document)(*.docm)。
这种文件类型的存在部分是为了解决宏安全问题。如果您在不希望收到的文档上看到此文件扩展名,请保持怀疑。
我还能用宏做什么?(What Else Can I Do With Macros?)
您可以在 Excel(create macros in Excel)、Outlook甚至PowerPoint中创建宏。您当前正在手动执行的任何操作,您都应该能够记录或编写宏来执行此操作。在Word(Word)中使用宏以及出色的 Windows 快捷键(great Windows shortcut keys)将使您的工作效率提高十倍。
How to Create and Run a Macro in Word
If you work with Microsoft Word a lot and find yourself doing the same things repeatedly, create a macro. Macro is a shortening of the word macroinstruction, which means a series of instructions to accomplish a task.
The great thing about creating macros in Word is that you don’t need to know how to program. If you can press record and stop, you can do this.
Aren’t Macros Dangerous?
Have you heard about macros being bad because they can contain viruses? Although you do have to be careful about opening Office documents from unknown people because they might have malicious macros, that’s not an issue here. You make your own macro, so you know it’s not a virus.
How To Record A Macro in Word
For this example, you’re going to create a macro in Word to insert your signature at the end of a document.
- With Word open, go to the View tab.
- Select the down arrow under the Macros button.
- Select Record Macro… a new window will open.
- In the Macro name: field, enter a meaningful name for the macro. Spaces are not allowed. Use an underscore or dash.
- In the Store macro in: dropdown, you can select what documents you want this macro to be used. If you choose All Documents (Normal.dotm), the macro will be available to you in every new Word document you make from now on. If you choose a single document, it will only apply to that single document. It’s usually best to choose All Documents.
- In the Description: field, write what the macro does. This is a simple macro with a descriptive name, but as you get confident with macros you’ll do more complex things, so a description is always a good idea.
- You can choose Assign macro to either a button you’ll create with the Button or hotkeys you can choose using the Keyboard button. There’s already a lot of shortcut keys for Word, so a button may be best. Select Button. A new window will open called Word Options.
- Here you’ll assign the macro a button on the Word Ribbon. Select Customize Ribbon.
- In the Choose commands from: dropdown, select Macros. This will show us our macros in the area beneath it.
- You need somewhere on the ribbon to put the macro. For this example, select Home in the Main Tabs area. Then select New Group.
- Select Rename so you can give it a meaningful name.
- In the Rename window, select one of the icons to represent the group then enter My Macros in the Display name field.
- Select OK to apply it. You’ll see the change in the Main Tabs area.
- With the new My Macros selected in the Main Tabs area, select the Normal.NewMacros.Insert_Signature macro.
- Select Add to insert it into the My Macros group.
- Of course, you’ll want to rename it. With the macro selected in the Main Tabs area, select Rename…
- In the Rename window, select an icon and enter a simple name in the Display name: field.
- Select OK to commit the change. You’ll see the change in the Main Tabs area.
- Select OK below the Main Tabs area to finish creating the button for the macro.
- Go ahead and create whatever signature block you’d like. Add in a scan of your signature or other images. Whatever you’d like. When done that, select the down arrow under the Macros button.
- Select Stop Recording. That’s it. Your Insert Signature macro is made.
- Test it out by finding the Insert Signature button you created in the Home tab and select it. Your signature block should magically appear exactly as you designed it.
How To Write a Macro
For most macros, the recording method is the most efficient way to create them. Eventually, you’ll want to do something more complex. This requires writing the macro in the Visual Basic for Applications (VBA) language. We’ve got the best VBA guide for beginners, so bookmark that. Let’s create a macro to give us the average words per sentence to help us tell when we’re being wordy.
- You need access to the Developer tab which is hidden by default in Word. In the top-left corner of Word, select File.
- Near the bottom-left, select Options.
- In the window that opens, select Customize Ribbon.
- On the right side, look for Developer and put a checkmark next to it.
- Select OK to close the window. The Developer tab will now show.
- Select the Developer tab.
- Select the Macros button.
- Enter a meaningful name in Macro name: Leave the Macros in: as Normal.dotm so it will apply to all Word documents. Always enter a description in the Description: area.
- Select Create. The Microsoft Visual Basic for Applications development tool will open.
- The code that’s shown in the green rectangles below should already be there with some blank space between them. Copy and paste the following code into that space:
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 “Average words per sentence” + Str(Int(numWords / numSentences)) + “. Less than 15 is best.”
- Select the Run button to test the code. If it doesn’t run as expected, edit the code until it runs the way you want.
- Select the Save button and close the development window.
- Create a button in the Word ribbon using the same method as shown in the “How to Record a Macro” instructions above.
- Find the button you just created and test it.
My Macro Doesn’t Work When I Save My Word Document
Word defaults to save with the file extension .docx, which doesn’t allow macros to automatically run. If you want the macro to run, you’ll need to change the file type to Word Macro-Enabled Document (*.docm).
This filetype exists partly to combat macro security issues. If you see this file extension on a document you weren’t expecting to receive, be suspicious.
What Else Can I Do With Macros?
You can create macros in Excel, Outlook, even PowerPoint. Anything that you’re currently doing manually, you should be able to record or write a macro to do it. Using macros in Word along with great Windows shortcut keys will amplify your productivity tenfold.