[英]Using VBA, open an OpenXML format (.docx) document in Word 2003 where the extension is not .docx or .docm
I have a Word add-in that writes and reads Word documents in OpenXML format - ie .docx. 我有一个Word加载项,它以OpenXML格式(即.docx)编写和读取Word文档。
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). 但是,为了使我可以轻松识别“我的”文档,而不是我使用另一个扩展名的普通Word .docx文件,将其称为.myx(对于启用了宏的文档,则称为.mym)。
This all works wonderfully in Word 2007 and Word 2010, and I thought it would work in Word 2003 (with the Compatibility Pack installed). 所有这些都可以在Word 2007和Word 2010中很好地工作,我认为它可以在Word 2003中工作(安装了兼容性包)。
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. 但是Word 2003在打开带有.docx扩展名的文档时,将不会打开.myx文档,除非我将其重命名为扩展名是.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. 当我在VBA上进行尝试时,我使用了一些看起来很明显的WdOpenFormat枚举值,但似乎无济于事。
Do I really have to change my extension to .docx to get Word 2003 to open it? 我是否真的必须将扩展名更改为.docx才能使用Word 2003打开它?
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. 答案是Office 2007兼容包只是Word的另一个“文本转换器”,而.docx和.docm扩展名似乎是Word用来调用该文本转换器的工具。
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. 但是,这篇文章这篇文章 ,这与节约作为Word 2003中的.docx环境交易,给出一个提示,如何去打开文件。 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: 使用该链接中定义的宏作为指南,这是在C#中如何找到用于打开文档的正确“ OpenFormat”值的本质:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.