Results 1 to 7 of 7
  1. #1
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts

    How to access XML info with VBA?

    For years I have used Application.Dialogs(87).Template to return the name of the template that was first used to create a document. This worked even if the original template was no longer available and Word had connected itself to "Normal" instead.

    In Word 2010, Application.Dialogs(87) returns "Normal" (the same as ActiveDocument.AttachedTemplate) if the original template is unavailable - even though the original template is listed in the "schemas.openxmlformats.org/package/2006/relationships" XML area.

    How can I access this XML information with VBA?
    Stylus

  2. #2
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    I've used Dialogs(wdDialogToolsTemplates).Template successfully in Word 2010.

  3. #3
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thanks for the reply!

    Could you please elaborate on what you mean by "successfully"?

    I've found that the Dialogs(87) WILL retrieve the original template's name in docs made with Word 2003. (This even when the macro is running in Word 2010). I surmise that Word 2003 saves the metadata in a way that the dialog will find, regardless of the environment the macro is running in.

    Word 2010 saves this data in an XML part, which the dialog can't find (at least in my experience!).

    If you give me the details of what works in your case , I may be able to find some way of replicating that.

    Thanks!
    Stylus

  4. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Hi All,

    wdDialogToolsTemplates = 87, i.e. they are the same thing.
    wdDialogToolsTemplates.JPG
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  5. #5
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Richard and RG

    Thanks for the input.

    I just ran this experiment:

    I created a word2012 test document based on a template located on the network, and then saved this test doc to my pc.

    Next, I opened the test doc and ran a macro using wdDialogToolsTemplates to show me the location of the template used to create the document. The macro returned the correct mapping and template name.

    Then, I changed the name of the template, opened the test doc again, and ran the macro. This time, the macro returned "normal" as the template name instead of the original template used.

    So, wdDialogToolsTemplates does not solve the problem. Instead of reading the word file meta data to return the original template name (as it did up to word 2003), it shows "normal" - EVEN THOUGH THE ORIGINAL TEMPLATE NAME IS STORED IN AN XML PART OF THE WORD FILE.

    The information is there in the word file. I know from creating a custom ribbon that vba can interact with xml. The question is: what is the method for reading this xml part of the word file?

    There is some information on the web about using "custom xml parts". After messing around with that for a while I realized that custom parts are different than the buillt-in mxl parts. Since it is possible to create, write to, and read custom xml parts, it "should" be possible to read a built-in xml part.

    The xml parts, by the way, can be acessed by changing the docx extention of the word file into .zip. The reveals the xml files.

    I'm really hoping that someone can help me with this, or point me towards a source of information.
    Stylus

  6. #6
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    I just ran a test. I created a document based on c:\.....\correspondence\letter.dotm. I saved and closed the document. I then renamed the correspondence folder to xxcorrespondence. I opened the document and ran my test macro. The code returned c:\....\correspondence\letter.dotm, the original attached template.

    Also, when I open the document and choose the Document Template button from the Developer tab, the attached template displays c:\....\correspondence\letter.dotm, even though that path/file does not currently exist.

  7. #7
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Richard

    Thanks for running that test - it's something I should have thought of. I now discover that wdDialogToolsTemplates does return the original template file path and name IF the original file path is a drive location. I this case "C". It also works on older Word 2003 docs that have template file paths with a virtual drive location.

    If the file path of the original folder is a network location instead of a drive location, and this file path is disrupted, then wdDialogToolsTemplates fails and returns the same as ActiveDocument.AttachedTemplate (i.e. Normal).

    Interestingly, the drive-based template file path is stored in the same xml part location as the network file path, with the beginning of the file path ("C") being the only difference.

    It looks like ActiveDocument.AttachedTemplate can't parse a file path that is a network location.

    The loose end in all this is what wdDialogToolsTemplates is reading in the Word 2003 doc. I don't think the metadata was xml back then.

    So, I'm back to my starting point: how to use vba to read the xml metadata.
    Stylus

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •