Password-Protect VBA Macros & Other VB Editor Tricks

Blog 8.6.2012 No Comments

The lowly Visual Basic Editor is sometimes looked down upon in comparison to the feature-robust design-environment available with .NET macros and add-ins. Its true, the VB Editor is rather lowly in comparison. When it comes to churning out macros at break-neck speed, however, VBA macros can’t be beat. Since most of you create VBA macros using the VB Editor, I thought I’d share with you a few tricks I’ve accumulated over the years.

Password protect your source code

By default, the code in a VBA macro is unprotected and visible to anyone who simply opens the .swp file by going to Tools–>Macro–>Edit. Changing the .swp file to a protected state is very simple.

  1. Open the macro in the VB Editor
  2. In the Project Explorer on the left, right click “ Properties”
  3. Click the Protection tab
  4. Click the “Lock project for viewing” check box, choose a password, and click OK

Now you can distribute the macro to your heart’s content with no concern about your code being viewed.

Quickly return to SolidWorks with Alt+F11

In most of my videos, to return from the VB Editor to SolidWorks I use Alt+Tab. I do this mostly out of habit. This shortcut, however, sometimes brings up the wrong window if you aren’t careful. If you want to avoid any hassle with Alt-Tab and just go right back to the SolidWorks application, then either hit Alt+F11 or press this button in the top left of the VB Editor.

Quickly bring up parameter info with Ctrl+I

As you know from watching Lesson 2.2 in our VBA course, once you’ve early bound your objects you can start using Microsoft’s Intellisense technology to see a SolidWorks API function’s parameter info during design time. Later, if you need to modify an argument, the parameter info listing does not re-appear unless you delete out and reinsert a comma or parenthesis. Using the Ctrl+I keyboard shortcut, however, you can bring up the parameter info once your cursor is within argument text.

Turn off auto syntax check

Often times while programming, I decide not to finish typing the current line and instead move to another line. The result is a rather annoying message box that looks like this.

Fortunately, the automatic syntax checking that causes these dialog boxes can be turned off. Simply go to Tools–>Options, and then un-check “Auto Syntax Check”. No more annoying dialogs.

Change the code window colors

Many times programmers will change the code window background to black and the text to bright green, white, or yellow in an effort to either 1) reduce the monitor’s brightness, which saves battery life and is a little easier on the eyes, or 2) set themselves apart from the crowd in an effort to be rogue and cool. (As rogue and cool as a programmer can be, anyway.)

So if you’re like me and fall into this camp for all of the reasons listed (though mostly to be rogue and cool) then feel free to modify the colors by going to Tools–>Options, Editor Format tab, and changing the foreground and background of the following Code Colors to your liking: Normal Text, Syntax Error Text, Comment Text, Keyword Text, and Identifier Text. Requires a little bit of setup, yes, but the end result is that your co-workers will surely pass your desk, notice your custom-colored code window, and think, “That guys clearly knows his stuff.” Clearly.

Now its your turn: what VB Editor tips and tricks make you a more savvy coder?

Thanks for tuning in,

Want to keep up with new content? Sign up for our newsletter.

Questions and Comments

You must be logged in to post a comment.