Word document programming… simplified…

12 Jan

Fixing things for people around the office is often time consuming and frankly problematic.  Sometimes you don’t have administrative privileges on everyone’s machine and you also don’t have time to personally go around and fix things. You write a script … but unfortunately the company email system has an apoplectic fit and refuses to send around your script. 

So… you put the script on a share – or your rename it to a zip or some other file name and force everyone to rename it and then run it. 


Open your Microsoft Word (I’m using 2007 for this) and let’s get to your Developer tab.  Don’t have a Developer tab??  Here’s how you get one… first  Open Word and click on the Office Button and select imageimageWord Options. Which will open up the Options menu screen – and off the Popular menu you can click on the check box for “Show Developer”  like it shows here.  

Now the cool thing is that if my directions are not that good  – you can find them off the Microsoft web site here… – so feel free to use them.  But the goal is to get a developers tab – so you can … well … develop.  Now this is a quickie tip so we won’t get too involved but using the Developer tab you’re able to put into a document any VBA command you can think of so you’re not limited.  There is very little you can’t do from programmatically changing things on the document to changing the registry settings. 

Let’s start by typing a short description of what it is the document does, and letting the user know that it is not a standard word document – and even telling the user that they’re going to need to turn on the scripts and macros for the document to make it run. 

It’s important that if you do any scripting on a word, excel or other document you inform the user they are using a document that has macros or scripted capabilities in it.  Sure – you could just have it trigger an event and not warn the user but to do so is very unprofessional and it imposes a level of credibility imagewith the user’s sense of safety and privacy.    So type a short paragraph and tell them what you’re about to do before you do it. 

Buttons or other mechanisms that the user can click on – that provide a kind of tactile feel let the user know this is not a standard document and that by clicking on the button something will happen.

So select a button from the legacy tool tools list – my prefered one is the standard ActiveX control Command Button that we see all the time.  You can find it off the Legacy tools menu imageshown under the ActiveX commands.  Once you’ve set your commandbutton up right – double click on it to reveal the vba code blocks.

I know… I know… this is VBA and VBA supposedly has a reduced instructions and it’s not as good as real code… and blah-blah-blah. 

Yes, a lot of that is very true.  VBA is not as robust as (although it’s pretty close) and it’s certainly not up to C# and nothing like the degree of power you get from unmanaged code.  But the point of VBA is that the average person can – with a bit of trial and error create pretty much anything they need and for the most part there is very little that you would want to try with a document that you need the power of unmanaged code or even the power of full blown Visual Basic or C#.  But if you wanted to – you very definitely can.  It really all depends on what you need to do, but for this quickie we’re just going to show something simple.

imageSo here’s a button … we’ve double clicked on it to reveal the visual basic editor – which looks like this… and I’ve dropped a little code in there which will open (silently) two command windows run the command “ipconfig /release” and “ipconfig /renew”. 

Now – I’ll do one more thing here, which is to disable the button the user clicks to make sure they only click it once.  Ordinarily I’d drop in some error code to check to see if the code completely properly and so on – you get the idea. 

The code to turn off the button looks like this:

ThisDocument.CommandButton1.Enabled = False

And you want to do that because if you don’t disable the button – then people will keep clicking it because your code may not display any visual indicator that the code was actually executed.  There are literally dozens of ways to provide feed back to the user to give them status and I really recommend you do.  The number of things you can do with a document are amazing.

You can add in there code, functions, anything you really need and now it’s all self contained in a document file that should allow you to send it to people and the only thing they have to do – is enable macros for the document and click on the button. 


To enable macros – if someone hasn’t gotten the standard warning asking them to enable them it imagelooks like the one shown here – so you may want to warn people they’re going to see this and to turn it on in any emails you include the document with.  Once again it’s all about trust – and if people start mistrusting your emails then you’ve got a problem.  Make people know that if you do send them something with a macro or code in it … it’s okay by telling them why they got the message. 

You can… of course download the code for this … here… it is of course a “docm” format – meaning that it’s clearly a word document with scripted or programmed (macros even) capabilities.  But I can save this in my groups SharePoint – and even most email programs will allow it to pass to someone since it’’s very openly a macro or scripted object.   Hopefully your company is one of those whose email systems do allow this since it requires the user to manually turn on the document’s macro capabilities.  If yours is one this is a great time saver you can send out to a group to fix issues.

Just my 2 cents and a quickie for the week….


HEY… Special note… congrats to Tommy Z out there on the birth of his son Zachary Z – many many blessings to you and mom.  He’s a cutie. 

Leave a comment

Posted by on January 12, 2009 in Design, Development, Work


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: