Activate vba in word

How do I get to the VBA editor in MS Word 2013? All my searches return information on Excel.

asked Apr 23, 2014 at 15:11

John S's user avatar

0

First you need to enable the Developer button in Word.

Click File, then Options, Customize Ribbon.

Then Select the Developer tab checkbox.

Developer will appear is a menu option now. Select it and you can the select Visual Basic to open the VBA editor.

answered Apr 23, 2014 at 15:24

Keltari's user avatar

KeltariKeltari

71.4k26 gold badges178 silver badges228 bronze badges

Visual Basic Dev Button

I think you can access it via the visual basic button under the Developer tab as above

answered Apr 23, 2014 at 15:17

Simkill's user avatar

SimkillSimkill

1,62711 silver badges15 bronze badges

0

For most cases, you are often using VBA code to do various tasks in Your Word. However, do you really get hang of the steps to run VBA code? Therefore, in this article, we offer a detailed description of those steps for you.

In the followings, we take Word 2010 as an example to show the detailed steps. And you can easily apply the method to other versions in a similar way.

Step 1 Open the “Developer”

  1. First of all, go to the “File”, and click “Options”.Click "Options"
  2. Next click “Customize Ribbon” to check whether the “Developer” is selected.

If checked, you can go to the Step 2; if not, you need do the followings:

(1). On the right sections of Word Options, you need choose “Main Tabs” under the “Customize Ribbon”.

(2). Now you will find the “Developer”, and then check it.

(3). Finally, click “OK” to close the window.Enable "Developer"

(4). Back to the home screen of your word, you can easily find the “Developer” tab.

Step 2 Check Securities for the Macro

Before running VBA code, you need follow this step to check whether the operating environment is secure to run your macro.

  1. After enabling the “Developer”, you need click “Macro Security” in the “Code” group.Click "Macro Security"
  2. Then make sure the “Disable all macro with notifications” is checked in “Macro Setting”.Security Setting

Step 3 Edit the VBA code

  1. Firstly, click “Visual Basic” in the “Code” group, on “Developer” tab or you can press “Alt” + “F11” in your keyboard to open the VBA editor.
  2. Then click “Insert”, in the drop-down menu, you can click “Module”.
  3. Next double click to open a new module.Add New Module
  4. Finally, in the empty area of this project, you can edit your VBA code in it.Edit Code

Step 4 Run VBA code

After editing the VBA code you need, next comes to run it.

  1. Chick “Run” (the green triangle like bellowed picture) in the toolbar or hit “F5” in your keyboard in the current module.Run VBA Code

Alternative Option: Add Macro Buttons to the Quick Access Toolbar

When finishing above steps, you can also add shortcut button for your macro.

  1. Similarly, click “Options” in the “File” tab, and then click “Quick Access Toolbar”.
  2. Choose “Macros” and click “Add” to the toolbar after selecting “All Commands” from the “Choose commands from” drop down list.
  3. Then click “Ok” to close the window.
  4. Back to the document, you can find the macro is already in the Quick Access Toolbar. Therefore, you can easily click it to run your code.Add Marcos to Quick Access Toolbar

Author Introduction:

Micky Ao is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including Excel fix and pdf repair software products. For more information visit www.datanumen.com

Как установить VBA в приложениях Microsoft Office

Некоторые приложения пакета Microsoft Office, такие как Word, Excel, Access, Outlook, позволяют использовать для решения пользовательских задач язык программирования Visual Basic for Applications (VBA).

Для того чтобы использовать возможности VBA, макросы, процедуры и надстройки, написанные на этом языке программирования, необходимо чтобы компонент Visual Basic для приложений был установлен вместе с приложениями пакета Microsoft Office.

Как проверить установлен ли Visual Basic для приложений?

Самый простой способ проверить наличие либо отсутствие установленного VBA это попробовать его запустить. Запустить VBA можно сочетанием клавиш Alt+F11 на клавиатуре, либо кнопкой «Visual Basic» на вкладке «Разработчик». По умолчанию вкладка «Разработчик» отключена во всех приложениях пакета Microsoft Office, поэтому предварительно ее нужно отобразить в настройках ленты.

Итак, если VBA установлен, то нажатие горячих клавиш либо кнопки «Visual Basic» на ленте приложения приводит к появлению окна редактора Visual Basic на экране монитора. Выглядит редактор примерно так, как показано на изображении ниже.

redaktor VBA v excel

В случае, если окно редактора не появилось, то компонент Visual Basic для приложений необходимо установить.

Как установить Visual Basic для приложений?

Чтобы установить компонент пакета Visual Basic for Applications, необходимо нажать кнопку Пуск, зайти в Панель управления/Программы и компоненты, выбрать программу Microsoft Office и нажать кнопку «Изменить», либо запустить установочный файл Setup.exe.

izmenit ustanovki programmy

Далее выбрать опцию «Добавить или удалить компоненты», в параметрах установки выбрать компонент Visual Basic для приложений и доустановить его.

How to Run VBA Code in Your Word

For most cases, you are often using VBA code to do various tasks in Your Word. However, do you really get hang of the steps to run VBA code? Therefore, in this article, we offer a detailed description of those steps for you.

In the followings, we take Word 2010 as an example to show the detailed steps. And you can easily apply the method to other versions in a similar way.

Step 1 Open the “Developer”

  1. First of all, go to the “File”, and click “Options”.Click "Options"
  2. Next click “Customize Ribbon” to check whether the “Developer” is selected.

If checked, you can go to the Step 2; if not, you need do the followings:

(1). On the right sections of Word Options, you need choose “Main Tabs” under the “Customize Ribbon”.

(2). Now you will find the “Developer”, and then check it.

Enable "Developer"

(3). Finally, click “OK” to close the window.

(4). Back to the home screen of your word, you can easily find the “Developer” tab.

Step 2 Check Securities for the Macro

Before running VBA code, you need follow this step to check whether the operating environment is secure to run your macro.

  1. After enabling the “Developer”, you need click “Macro Security” in the “Code” group.Click "Macro Security"
  2. Then make sure the “Disable all macro with notifications” is checked in “Macro Setting”.Security Setting

Step 3 Edit the VBA code

  1. Firstly, click “Visual Basic” in the “Code” group, on “Developer” tab or you can press “Alt” + “F11” in your keyboard to open the VBA editor.
  2. Then click “Insert”, in the drop-down menu, you can click “Module”.
  3. Next double click to open a new module.Add New Module
  4. Finally, in the empty area of this project, you can edit your VBA code in it.Edit Code

Step 4 Run VBA code

After editing the VBA code you need, next comes to run it.

  1. Chick “Run” (the green triangle like bellowed picture) in the toolbar or hit “F5” in your keyboard in the current module.Run VBA Code

Alternative Option: Add Macro Buttons to the Quick Access Toolbar

When finishing above steps, you can also add shortcut button for your macro.

Add Marcos to Quick Access Toolbar

  1. Similarly, click “Options” in the “File” tab, and then click “Quick Access Toolbar”.
  2. Choose “Macros” and click “Add” to the toolbar after selecting “All Commands” from the “Choose commands from” drop down list.
  3. Then click “Ok” to close the window.
  4. Back to the document, you can find the macro is already in the Quick Access Toolbar. Therefore, you can easily click it to run your code.

How do you open the VBA editor in MS Word 2013? [duplicate]

How do I get to the VBA editor in MS Word 2013? All my searches return information on Excel.

2 Answers 2

First you need to enable the Developer button in Word.

Click File , then Options , Customize Ribbon .

Then Select the Developer tab checkbox.

Developer will appear is a menu option now. Select it and you can the select Visual Basic to open the VBA editor.

Visual Basic Dev Button

I think you can access it via the visual basic button under the Developer tab as above

The Microsoft Office suite is one of the most popular office solutions. From Word and Excel to PowerPoint – the practical applications for writing, calculating, and presenting have been proving their worth for years. But many users are not even aware of the range of features that they provide. For instance, MS Office programs include a function that allows you to create, save, and execute macros in order to automate certain tasks and processes. Especially for regularly recurring workflows, this feature lets you save valuable time and effort.

But what role do macros play in Word and how do these command sequences work exactly? Also find out how to create macros in Word and integrate them into your documents.

Contents

  1. What is a Word macro?
  2. How Do Macros Work in Word?
  3. Creating Macros in Word: How it Works
    1. Recording Word Macros Executed by Button
    2. Creating Word Macros Executed via Key Combination
    3. Executing Word Macros
    4. Adding macro buttons to a Word tab ribbon
    5. Importing Existing Macros in Word
  4. Word Macros: Examples of Useful Command Sequences
    1. Word Macro for Automatically Determining the Average Sentence Length
    2. Word Macro Example: Correcting Transposed Letters
    3. Macro Code for Removing All Hyperlinks

What is a Word macro?

Macros are considered a standard feature in programming as well as the use of computer programs. They are basically subprograms that developers and users can create to save sequences of commands or operating instructions, which can then be started with a simple action. When faced with many regularly recurring tasks, Microsoft Word is typically an application that benefits from the option to automate work steps.

For this reason, the Editor (as well as Access, Excel or PowerPoint, for example) include an integrated tool for recording and executing custom Word macros. The script language Visual Basic for Applications (VBA), likewise developed by Microsoft, serves as the recording language here.

Note

The event-driven programming language VBA replaced the mutually incompatible macro languages of the various Microsoft Office programs in the mid-1990s. Thanks to this standardized solution, cross-program command chains can also be created and run to transfer data from a Word document into an Excel table with a single click, for example.

How Do Macros Work in Word?

Creating macros in Word may sound like a task for experienced programmers at first. After all, writing new program code in a special programming language is not often among the core skills of the average Word user. However, the fact that creating and using Word macros is possiblewithout any programming knowledge at all is thanks to the integrated macro tool. Known as a macro recorder, this tool makes writing code completely unnecessary. To create a new macro, all the user has to do is start recording, perform the desired operating steps and commands, and then stop the recording again when the sequence is finished. The VBA code for Word macros is generated automatically; no further action is required on the part of the user.

Note

With the Visual Basic Editor, the Microsoft Office tools have integrated their own VBA development environment that allows the code of recorded macros to be viewed and edited.

You can also assign each new macro in Word with a unique key combination or button that allows the corresponding command sequence to be run at any time. In the case of buttons, these can be added to the toolbar for quick access, enabling you to start a macro with a single click. If the Word macro created is to be available in all documents, save it in the template file Normal.dotm, which is used by Word as the general template for all new text projects.

Creating Macros in Word: How it Works

If you would like to configure your own automated sequences for your Word documents, but lack the technical knowledge in the programming language VBA, you should use the option to create macros in Word using the recorder. You can find the macro tools as standard under the “Macros” section in the “View” tab. In order to make it easier to create and manage Word macros, however, it is advisable to activate the developer tools which provide you access to the Visual Basic Editor, among other features. This optional toolbox can be unlocked as follows:

  1. Open the “File” tab.
  2. Click on “Options”.
  3. Switch to the “Customize the Ribbon” section and place a checkmark in the “Developer” box (under “Main Tabs”).
Word 2016: “Customize the Ribbon” in the Word options
You can remove the developer tools from the Word ribbon at any time by deactivating the checkbox.

As soon as you have activated the “Developer” tools in the Word options, the tabunder the same name will automatically be added to the user interface. When you click on it, you will see the section for creating and managing macros in the far left of the toolbar. Find out in the following step-by-step guide how to create your own macros in Word using this quick-access menu and how you can run them at a later time.

Recording Word Macros Executed by Button

The typical solution for a new macro in Word is one that can be run by clicking a button. If you would like to create such a macro using the developer tools, first click on the “Developer” tab and then select the option “Record Macro”:

Word 2016: “Developer” tab
To open the menu for recording new Word macros, you can also press the [Alt], [L], and [R] keys consecutively.

Now enter a name for the macro and select the document you wish to create the macro for under “Store macro in:”. If you would like to create a cross-program macro, simply select the option “All Documents (Normal.dotm)”. To enable the macro to be executed in Word using a button, finally click on “Button”:

The “Record Macro” menu in Word 2016
You have the option to add individual information about the Word macro you want to create under “Description”.

Select the macro you wish to create in the left window and click on “Add”, making it also selectable in the right window. Click on it here again and then press the “Change” button. You can now assign your new Word macro any icon you wish:

Adjusting the quick-access toolbar in Word 2016
When assigning the button for the new macro, you also have the option of adding it to all Word documents (standard) or for a certain document only.

Confirm your desired button by clicking on “OK” twice. Now perform the actions you want to save in the macro. Word will record your mouse clicks as well as key strokes. However, the recorder will not save any movements or highlighting with the mouse. So if you want to select some text, for example, you will need to use the keyboard (hold down the [Shift] key and use the arrow keys).

Once you have finished the sequence for the macro, you can stop the recording via “Stop Recording”:

The “Developer” tab in Word 2016
You can also pause the recording for the Word macro temporarily: Simply use the “Pause Recording” button in the toolbar.

The key for the new Word macro will automatically be added to the toolbar for quick access:

Microsoft Word 2016: Quick-access toolbar
The quick-access toolbar also contains as standard the buttons for saving the current Word document or reversing changes.

Creating Word Macros Executed via Key Combination

You can create a Word macro that can be started using a unique key combination in essentially the same way as for macro buttons: Start the process using the “Record Macro” button in the macro menu under “Developer”, enter a suitable name, and decide whether you want to make the macro available in all documents or only in a certain project. In the final step before beginning the recording, you should choose the “Keyboard” option in order to open the menu for new hotkeys:

The “Record Macro” Word menu
If you choose a name for a new macro which already exists, Word will ask whether you wish to replace the original macro.

In the “Customize Keyboard” menu that then appears, select the macro under “Commands”. Next, click on the “Press new shortcut key” field with the left mouse button and press your desired key combination. Here it is important to press these keys simultaneously. In the following Word macro example, we used the [Ctrl], [Shift], and [#] keys.

Word 2016: “Customize Keyboard” menu
Word will tell you whether a chosen key combination already exists or is not yet assigned – as in this example.

Under “Save changes in”, you can define whether the key combination should be valid universally (“Normal”) or only for a certain Word document; to confirm your choice, click on the “Assign” button. As soon as you close the menu, the macro recording will start.

Executing Word Macros

Once you have created a macro, you can start it at any time using the defined execution option. You only need to use the define key combination or click on the corresponding button in the quick-access menu (in the top left). You can also run your Word macros using the macro list by following the steps below:

  1. Click on the “Macros” button under the “Developer” tab or alternatively on “Macros” in the menu under the “View” tab, and then on “View Macros”.
  2. Select the desired macro from the list using a left mouse click.
  3. Now press “Run”.
List of available macros in Word 2016
Once you have activated the developer tools, you can open the selected Word macro in the Visual Basic Editor by clicking on “Edit”.

Adding macro buttons to a Word tab ribbon

If the macro icon is too small for you in the quick-access toolbar or if you would like to add a button for a macro executable via key combination, you can also integrate a macro button into the ribbon of any tab. To do so, simply create a user-defined group for the tab and add the desired macro to it. You can do both in the “Customize Ribbon” menu in the Word options (accessibly via “File” -> “Options”).

Creating a user-defined group:

In the right window under “Customize Ribbon”, select the tab in which you wish to create the new group and then click on the “New Group” button. In addition to the standard groups here, the entry “New Group (User-Defined)” will now appear. Click on this and then press “Rename” to give the group a suitable name as well as an icon:

Word 2016: Customizing the ribbon for the “Start” tab
World will only display the selected symbol if the width of the program window is reduced and the group is collapsed for this reason.

Integrating a Word macro into a user-defined group:

After creating a user-defined group for a tab’s ribbon, you can now add any number of Word macros to this group. First click on the “Macros” option in the left menu section under “Choose commands from:” and then on the Word macro you wish to integrate. In the right menu window, now click on the user-defined group before finally pressing the “Add” button:

Adding Word macros to a tab
If you would like to remove a macro from a user-defined ribbon group again, select it in the right menu window and then click on the “Remove” button.

Close the Word options and open the tab you added the macro button to. You should now see this in the group you created in the ribbon:

Word 2016: Customized ribbon
In standard tabs, new groups are typically added to the right of existing groups. But you can also adjust the order and position the button elsewhere.

Importing Existing Macros in Word

You may also find yourself in the situation in which you already have VBA macros, but they are not available in your Word installation. This could be the case, for instance, if you created these macros on another device or received them from another person. Plus, there are various online sources where you can find a wide range of macros. Microsoft Word offers you the option to import ready-made command chains for use in your program. The tool for this is the Visual Basic Editor mentioned earlier. It can be opened via the key combination [Alt] + [F11] or alternatively using the “Developer” tab.

The next steps depend on whether you are importing a finished macro file or pure macro code.

Importing ready-made macro files (.frm, .bas, .cls):

If you have a complete macro as a file, you can import it into your Word installation in just a few steps. First, select the document you want to add the macro to in the Project Explorer. If you can’t find the explorer in the code editor, you can open it by selecting the “View” tab and pressing the “Project Explorer” menu item. In the file manager, now click on your current or another Word project if the macro should only be applied to one document. If you select the project “Normal”, the Word macro will be imported for all documents:

Project Explorer in the Visual Basic Editor
When you no longer need the Project Explorer, you can close it at any time by clicking on the cross in the top right.

To import the macro, click on “File” and then “Import File”. Specify the storage location for the macro file and press “Open” to start the import process.

The “Import File” option in the Visual Basic Editor
If you have created a Word macro, you can also export it as a file here using the button under the same name.

Importing macro code:

If you only have the code for a certain macro, other steps have to be taken for importing. Here too, you start by selecting the document you wish to add the automatic command sequence to. Open the explorer and double-click on the entry “ThisDocument” (in the subfolder “Microsoft Word Objects”) for the respective document or under “Normal” (to save the macro in the universal template):

Microsoft Word 2016: Project Explorer
When you select the project “Normal”, Word will save the macro in the universal template Normal.dotm.

In the code window that appears next, copy the macro code and then click on the “Save” button. If you selected a specific Word document in the previous step, you will be notified that you will have to save it as a “Word document with macros”. Then click on “No” and select the corresponding entry under the “File Type” list. Finally, click on “Save” to generate the new file format:

Changing the Word file type
To save an imported macro in the current Word document, you will need to change the format of your current document. When saving in the universal template, however, this is not necessary.

Word Macros: Examples of Useful Command Sequences

Now that you know what a Word macro is and how to use macros in Word (creating, running, and importing macros), we can explain the purpose of these command chains for automation in Microsoft’s text editor using a number of specific Word macro examples.

Word Macro for Automatically Determining the Average Sentence Length

There are various web tools you can use to analyze your texts – such as to ascertain the average length of your sentences. Using the right macro, you can also find this out directly in Word. The following code automatically looks at all the sentences in your document and divides the total word count by the number of sentences. The macro then displays the average length in a text popup (“Average Words Per Sentence:”):

Sub CountWords()
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))
End Sub

Word Macro Example: Correcting Transposed Letters

Anyone who regularly types texts on a computer will know just how quickly typos like transposed letters occur. But these errors can be quickly corrected. Using the following macro, you only need to place the marker in front of the erroneous pair of letters. Running the macro via key combination or button then allows the letters to be swapped around automatically:

Sub TransposeCharacters()
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Cut
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.Paste
End Sub

Macro Code for Removing All Hyperlinks

Whenever you include website addresses in your texts, Word will automatically change them into clickable hyperlinks to the corresponding sites. But if you don’t want your document to contain these hyperlinks, you usually have to delete the links individually. The following Word macro example saves you a whole lot of work by automatically removing the first hyperlink found in the document. You can therefore run the macro as often as necessary until all the hyperlinks are gone:

Sub RemoveHyperlinks()
'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

Click here for important legal disclaimers.

I’m hoping a VB/VBA expert can help me out. Consider the following:
The user opens a document in Word 2003, and within the Normal.dot AutoOpen macro, we look at current document, and if it has been opened by clicking on a link on a webpage, and meets certain other application specific criteria, close the streamed ‘copy’ and open the source document (found on a shared drive we can assume the user has access to):

Documents.Open origDoc
Documents(ActiveDocument.FullName).Close SaveChanges:=wdDoNotSaveChanges
Documents(origDoc).Activate

With ActiveDocument
    ''# Do work
End With

My thought was that I needed to call Activate to ensure that the original document was the ActiveDocument, but I’m getting a 4160 ‘Bad file name’ error on the .Activate call. If I comment out the call to .Activate, it appears that ActiveDocument is set to the origDoc document, even if there were other documents already opened (I’m not really sure how the Documents Collection is managed, and how Word determines what next ActiveDocument would be if you programatically close the current ActiveDocument)

So, does calling .Open on a document explicitly set the Document to be the ActiveDocument? Also, does calling .Activate on the already active document cause an error?

I haven’t really been able to find much documentation about this, so thanks in advance for any suggestions and insight!

Понравилась статья? Поделить с друзьями:
  • Action words word search
  • Action word что это
  • Action word with pictures
  • Action word for talking
  • Action word for talk