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

Posted on 2012-08-29
Last Modified: 2012-09-02
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?
Question by:LisaWhite
    LVL 76

    Expert Comment

    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)
    LVL 1

    Author Comment

    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.  :)
    LVL 10

    Accepted Solution

    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

    LVL 1

    Author Closing Comment

    By adding the XML reference in Visual Basic, I was able to edit the macros without running them.  Thanks much!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    A short article about a problem I had getting the GPS LocationListener working.
    This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now