Using VBA, open an OpenXML format (.docx) document in Word 2003 where the extension is not .docx or .docm

StackOverflow https://stackoverflow.com/questions/9014629

  •  14-11-2019
  •  | 
  •  

Question

I have a Word add-in that writes and reads Word documents in OpenXML format - i.e. .docx.

But so that I can easily recognize "my" documents as against normal Word .docx files I have used another extension, call it .myx (or .mym for macro-enabled documents).

This all works wonderfully in Word 2007 and Word 2010, and I thought it would work in Word 2003 (with the Compatibility Pack installed).

But Word 2003, while it opens documents with .docx extension, will not open a .myx document, unless I rename it so that the extension is .docx.

When I try it with VBA, I have used a number of what look like obvious values for the WdOpenFormat enumeration, but nothing seems to help.

Do I really have to change my extension to .docx to get Word 2003 to open it?

Was it helpful?

Solution

The answer is that the Compatibility Pack for Office 2007 is just another "text converter" to Word, and the .docx and .docm extensions appear to be what Word uses to invoke that text converter.

However, this article this article, which deals with saving as .docx in the Word 2003 environment, gives a hint as to how to go about opening the documents. Using the macro defined in that link as a guide, here's the essence (in C#) of how you go about finding the correct "OpenFormat" value for opening a document:

using MSWord = Microsoft.Office.Interop.Word;
...
private object GetOpenFormat(bool macroEnabled) {
  object result = MSWord.WdOpenFormat.wdOpenFormatAuto; /* default in case can't find the real value */
  string formatName = (macroEnabled? "Word 2007 Macro-enabled Document": "Word 2007 Document");
  foreach(MSWord.FileConverter converter in _msWordInstance.FileConverters) {
    if(string.Compare(converter.FormatName, formatName, true) == 0) {
      if(converter.CanOpen) {
        result = converter.OpenFormat;
        break;
      }
    }
  }
  return result;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top