Return to site

Add a Blog Post Title

broken image

 

 

 

Applies to all versions except Word 2008. Word 2008 does not support VBA. 

*Vba For Macros Excel Tutorial

*Vba For Mac Download

If someone on a newsgroup gave you a macro or some VBA to fix a problem, this is what you do with that text. Macros in Word are written in a coding language called VBA, or Visual Basic for Applications. This article explains how to install a macro step by step. It assumes no prior knowledge, but does presume that you have the text of the macro ready to copy. 

As always, some specific library may not exist in the Mac version but VBA is there. (Office 2016 is NOT Office 2008!) VBA improvements in Office 2016.

Go to Tools>Macro>Macros…. You will get a dialog box that lists all existing macros, if you have any. In the box for Macro Name, type a name for your macro—the name must not contain spaces. Click Create.

Naming Macros: It is conventional to express VBA names with each word capitalized and the spaces taken out. For example: 'ToggleWebToolbar.' If you do use capitalization as suggested, Word will expand the name into a tooltip (yellow balloon) when you place a button for the macro on the toolbar. For example, the tooltip will read 'Toggle Web Toolbar.'

After clicking Create, Word will dump you into the Visual Basic Editor (VBE, the environment where one can write and edit macros written in VBA). It may look very strange to you. Basically, it’s a three-pane layout, and the code you are about to work on should be in the right-hand (largest) pane.

You will see something like:

Sub MyMacroName()

'

' MyMacroName Macro

' Macro created 03/08/05 by Me

System mechanic activation serial key. [cursor will be here]

End Sub

You can paste the provided macro code right where the cursor is.

Lines that begin with an apostrophe ' are comments. When you run the macro, the comment lines are ignored. All macros created by Word include these two lines of information. It’s a very good idea to update them or add more information so that in a few years time you will know where this macro came from and what it was supposed to do.

In most cases, the code you have been given will paste without any problems or any red text, and you can test it. However, you may need to make some trivial fixes. 

Common Problems: All macros must start with a Sub statement and end with an End Sub. If the code someone gave you already had the Sub/End Sub in it, you will need to delete the extra Sub lines. It is conceivable that a sophisticated piece of code may also contain Function() and End Function statements: for the purposes of this article, treat them as equivalent.

Lines that show up in red have errors. This is most likely because there are line breaks in the wrong place, since newsgroups and email can force shorter line breaks. To fix these, place your cursor at the end of the first red line, and start tapping Delete. When you delete all the invisible spaces and carriage returns, the next line will move up and that may fix the problem. Do not delete any visible characters. Be aware that the statement in red may continue over three or four lines. You may need to re-enter a space that you delete.

Laptop users: the delete key usually behaves as a backspace key. You could also backspace from the beginning of the lines in red, to let the VBE wrap the lines naturally.

Also, if you copied the macro from a web browser and pasted directly into the VBA editor, you are likely to run into the 'non-breaking-space bug.' In the web browser, all the spaces were transposed into non-breaking spaces to preserve the layout, because HTML rules crunch multiple spaces into one. The cure is to replace all the spaces in front of the red lines with 'real' spaces, and the problem will go away. If you have to do a lot of them, paste the text first into a blank Word document, then use Find/Replace to replace the non-breaking spaces with ordinary spaces. 

To Test the Macro: in the VBE, click on the W button or use Word>Close And Return To Microsoft Word to go back to Word. Go to Tools>Macro>Macros…, select the macro name in the list, and click Run. 

If you expect to run the macro regularly, you can assign it to a keyboard shortcut, toolbar, or menu. See Useful Articles for explanations on how to do so. To learn more about the Visual Basic Editor and organizing Macros, click here.More Complex Errors: 

If the test run fails, return to Tools>Macro>Macros…, select the name of your macro, and click Edit. This will take you back to the Visual Basic Editor (VBE) and show you the code of your macro. Go to the Debug menu and choose Compile Normal (by default, the macro will have been saved in the Normal template).

It should appear as though nothing happened. If there is still a problem, Word will pop up an error and highlight the statement that contains it in yellow. You can look up the error messages in the VBA Help. However, for the purposes of this article, we can assume that there are only three causes:

*In your fixing of the code, you have damaged one of the statements.

*The macro you have been sent was created for WinWord and requires some changes to run on a Mac.

*The macro you have been sent is incomplete and never did work, even for the author!

Whatever the cause, unless you know VBA well, it will be much quicker to go back to the newsgroup or source of the code and ask. Suggest that the author send you the code again, and this time formats his/her message in HTML. HTML encoding is frowned upon in newsgroups, but it prevents these problems with broken line endings.

If the author of the code has created it in Windows, post the text of the code into the MacWord newsgroup and ask; the more sophisticated the macro, the more likely it is that it will require changes to run in MacWord. The person using Windows will not be able to help, because it is very difficult for even an experienced coder to tell what will work on the Mac and what won’t: the only reliable way is to compile the macro in MacWord and try it. In general, anything that will run in WinWord 97 will work on the Mac; anything that requires VBA6 will require tricky workarounds on the Mac.

If you suspect number 3, you might delicately ask if the author tested the code before sending it to you. Unfortunately, the Internet being what it is, there are people out there who for some reason sometimes send you code they haven’t tested that doesn’t work.More Useful Articles About Macros

These articles were written with WinWord in mind, but all will work for MacWord. The exact location of menus might be a little different, but the terms will be the same and changes should be self-evident. Some of the articles give keyboard shortcuts to open dialogs, however, which will not work on the Mac; you will have to use the menus. If you have problems, post the article link and the exact text that confuses you on the Microsoft Answers site.

Attention Safari Users: These links will not work immediately—you will need to hit Reload a few times, or use a different browser.Basics:

Creating a macro with no programming experience using the recorder

How to assign a macro to a toolbar

How to assign a macro to a keyboard shortcut

Understand that a macro and how you access it are separate items. When you record a macro, Word offers you the chance to assign it to a toolbar or keyboard shortcut. Nevertheless, that toolbar/keyboard assignment is not part of the macro. It is a separate setting in Word, layered on top of the macro. If you send the text of a macro to a friend, for instance, the toolbar/keyboard assignment will not travel with it.Slightly More Advanced:

How to modify a recorded macro

Running a macro automatically when Word starts or quits

Running a macro automatically when a document is created, opened or closed

The first option on the View menu of the Office 2011 for Mac Visual Basic Editor (VBE) is the Code option, which is grayed out until you make at least one Code window visible in the VBE. A common way to open a new code window is to use Insert→Module. A module is like a new word processing document window — it’s blank. Nothing interesting happens until you start writing. The same VBE features apply to Excel, Word, and PowerPoint.Exploring the Office 2011 VBE Code Window

Notice that horizontal lines separate the declarations section and each of the two macros. The lines are drawn automatically by the VBE. Each macro section is called a procedure. More about Code windows:

*

Object pop-up menu: This is the pop-up menu on the left side of the window; it provides a list of shortcuts to objects.

*

Procedure pop-up menu: This is the pop-up menu on the right side of the window; it provides a list of shortcuts to procedures and declarations. When an object is chosen in the Object pop-up menu, the procedures that are applicable to the selected object are displayed.

*

Declarations section: (Optional) Public declarations appear at the top of a Code module.

*

Macro modules: Each macro, or procedure, is separated by a line.

*

Procedure view button: Click to limit the display to the declaration or procedure section that has the insertion cursor.

*

Full Module view button: Click to display all Code window contents.Writing VBA code in Office 2011 for Mac

As with a word processor, in a Code window the editor knows which commands are legal and which are not, and the editor gives you feedback as you type. New for VBA (Visual Basic for Applications) in Office 2011 is Intellisense, a feature that helps you automatically complete your code as you type. To see how Intellisense works, declare a public variable in an empty Code window:

*

Choose Insert→Module to display a blank Code window.

*

Type the following code exactly as you see it here and press Return at the end of the line:Vba For Macros Excel Tutorial

Notice that after you type as, a pop-up menu appears. This is Intellisense at work. If you know what you want to type, you can keep right on typing. But if you want to see all the possible options, use the pop-up menu.Getting help with VBA code in Office 2011 for Mac

You can rely on the following five principal sources of code examples, syntax help, and general programming assistance:Vba For Mac Download

*

Macro recorder: In Excel and Word, you can record your actions and then look at the code that was recorded inside the modules.

*

Object browser: A handy treasure trove of information about the objects, methods, and properties of Office.

*

Help: The Help system within the VBE is minimal. Microsoft plans to provide VBA help for Excel only.

*

Internet: Search the Internet for VBA code examples. 

*

Books on VBA: You can find plenty of books about VBA. 

 

 

 

 

broken image