Editing a macro in a Word 2003 document using Word 2010 WITHOUT converting doc to 2010

I have a government client that is still using Word 2003.  They have a few hundred Word 2003 documents containing macros that pull data from a database.  I need to edit the macros but keep the documents as Word 2003.

When I open the 2003 version--without running the macros, which is important--I can't find the macro code in Visual Basic.  If I open the 2003 version and run the macros--which destroys the letter content--then the macro code shows up in Visual Basic.

I can't use a template because these letters get individually loaded into a separate database and somehow pull XML data out of it.  Every letter must contain the macro code.

In the body of the letter, there are placeholders for the data fields in the database, for example:


The macro contains the code to pull the data and replace the placeholders in the body of the document, like this:

Private Sub CustomerName()
    ReplacePlaceHolder CUSTOMER_NAME, GetTemplateValue("CustomerName")
End Sub

Because I am not attached to their real world system, when I run the macros, the macros replace the placeholders with blanks (which is actually correct in this scenario).  But then I have to recreate all the darn placeholders.

I did not write this code and, frankly, I don't know how or why it works, but it does.  I just need to add another field in the macro and in the letter.

What am I doing wrong?
Who is Participating?
In order to programmatically change the macros you will have to trust access to the VBA project object model. In addition, your macro security settings will have to allow macros in the documents to run. That will be a problem if the documents include an auto open macro because AFAIK you cannot block that macro from running and also have access to the macros in the document.

To use MSXML2.DOMDocument, you must include a reference to Microsoft XML. You add that reference manually from the Tools->References in the VB Editor. You can include a reference to Microsoft XML 6.0 in VBA as follows:
ActiveDocument.VBProject.References.AddFromGuid "{F5078F18-C551-11D3-89B9-0000F81FE221}", 6, 0

Open in new window

Are you sure that the macros are in the documents? Since macros were originally intended to help with document design, the code normally stays in the document's original template.

Check the attached template name in the 'Templates and Add-ins' dialogue (developer tab)
LisaWhiteAuthor Commented:
Hi GrahamSkan:

I am sure that the macros are in the documents.  When I originally worked on these documents using Word 2003, I had to edit the VB macro in every file separately.

There is no template attached.

After I posted this question, I went to the Trust Center Settings and checked "Trust access to the VBA project object model."  And now I get this Microsoft Visual Basic for Application error:

Compile error:
User-defined type not defined
On this code line:

Dim msxml As MSXML2.DOMDocument
I went back and unchecked "Trust access to the VBA project object model" but I'm still getting the compile error.

I'm not sure if I've made progress or if I've slid backwards.  :)
LisaWhiteAuthor Commented:
By adding the XML reference in Visual Basic, I was able to edit the macros without running them.  Thanks much!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.