简体   繁体   English

使用VBA,在Word 2003中打开扩展名不是.docx或.docm的OpenXML格式(.docx)文档

[英]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.

相关问题 MS Word vba 将 .docm 保存为 .docx 而不转换活动文档 - MS Word vba to save .docm to .docx WITHOUT converting active document 是否可以使用OpenXml将RTF文本片段插入Word文档(.docx)? - Is it possible to insert pieces of RTF text into a Word document (.docx) using OpenXml? 无法打开用 VBA 保存的 Word .docm 文档 - Cannot open Word .docm document saved with VBA 使用OpenXML将Word docx转换为Excel - Convert Word docx to Excel using OpenXML 具有.docm扩展名的word文档中的vba代码的说明 - Explanation of vba code in a word document having .docm extension 将活动文档格式(docm)附加到Outlook中的电子邮件时,是否可以将其转换为docx - Is there a way to convert the active document format (docm) to a docx when attaching it to an email in outlook 如何使用.Net-Core 将信息从(.docx 格式)传输到(.docm 格式)? - how to transfer information from (.docx format) to (.docm format) using .Net-Core? 使用Word 2003打开docx时,用户将获得“未知文件类型” - The user gets “Unknown file type” when open docx with Word 2003 使用xml数据的Word Docx mailmerge和使用xslt OPENXML合并 - Word Docx mailmerge using xml data and merging using xslt OPENXML 使用 Open XML SDK 将 .docx Word 文档添加到文字处理文档 - Adding a .docx Word document to a word processing document using Open XML SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM