Adding vba to word

These steps are done using Office 2010 (except where noted otherwise) but should be similar enough for 2007 and 2013 as well. I’m doing it with Excel but the differences should be subtle.

The first step is to add the Developer tab to the ribbon.

In Office 2007,

  1. Click “Office Menu” -> “(Program) Options”. 
    (“Office Menu” refers to the Office button button in the top left corner.)
    This brings up a menu that has an “Excel Options”, “Word Options”, etc., button at the bottom.

  2. In the options dialog box, check “Show Developer tab in the Ribbon”:
    Office menu
    and click “OK”.

  3. Your new “Developer” tab should be showing.

In Office 2010 and 2013,

  1. Click File -> Options.

  2. Choose Customize Ribbon.

  3. On the right side, find Developer and check the box. Click OK.

  4. Your new tab should be showing, click on it and then click on Visual Basic.

A daunting screen is shown, which is the VBA code editing window.

Side note: You can also open the VBA code editing window by using the Alt+F11 keyboard shortcut (even if the Developer tab is not showing).

Inside the code editing window, on the left side you should have a pane called Project-VBAProject. In this, under the Microsoft Excel Objects (this name is different depending on which program (Word, Outlook, etc.) you are using) is a ‘thing’ called ThisWorkbook. Double click on ThisWorkbook

In this example, we’ll use the following code.

sub WarnMeOfDanger()
   MsgBox "Argh, a dragon is behind you!"
end sub 

All the above does is show open a dialog box to show a message on-screen.

Copy the code, and paste it into the white sheet in the VBA code editing window.

Now, at the top of the VBA code editor window, click the green play button and you should see a message.

So, every time you want to run the code, return to this screen and press play!

However, you may prefer to add your own button. If you want to add a button then, first, note the first line of the code has WarnMeOfDanger(). This is actually the name of our ‘subroutine’. We’ll remember this for later. Now, simply close the Window you’ve been using (the code is automatically saved).

Still in the Developer ribbon, click on Insert, and choose the first item, which is a button. Your mouse cursor becomes a little cross (not angry, I mean its appearance). Draw the size of the button. A new window appears, and it should show us the option to assign the button to a function. In this case, we should see something like ThisWorkbook.WarnMeOfDanger. Click on this and click OK.

Now, if you press your new button, the message will show. Right-click the button to get options to move it, change the text, resize etc.

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

Skip to content

Button with the word macro on it, by Matt Scott.By C. K. MacLeod

Macros can help you to identify areas in your writing that need improving. You can also use macros for formatting and editing tasks. Some word processing programs, like Microsoft Word, can handle macros. You’ll find a list of writing macros you can try in the post Improve Your Writing With Macros, and the video below will show you how to add a macro to Word 2010:

Steps for Adding a Macro to Word

  1. Go to the View tab, and click on Macros in the Window area.
  2. Name your macro in the Macro name: box. Be sure your name has no spaces between words. For example, NeedlessWords.
  3. Click Create. You will now be taken to Word’s VBA editor. This is where Word stores macros.
  4. Copy the macro script and paste it into Word’s VBA. It will show you where to paste your macro (look for the section that has the same name as the macro you just named). Delete all the text that’s there (everything from Sub to End Sub) and paste your macro script into the VBA editor.
  5. Close Word’s VBA editor by going to File, Close and Return to Microsoft Word. Your macro will be saved and you can now use it with any Word document.

Next step: How to Run a Macro in Word.

For further instructions on how to use macros, see Macros for Editors, in which Paul Beverley offers detailed instructions for understanding and running macros in various versions of Word.

Image by Matt Scott

Как установить 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

Понравилась статья? Поделить с друзьями:
  • Adding validation in excel
  • Adding underlines in word
  • Adding two columns in word
  • Adding trendline in excel
  • Adding to strings in excel