Windows 8 以降の MSXML2.DOMDocument の使用方法

皆さん、こんにちは。 BI データプラットフォームサポートチームです。
SQL Server を中心にサポートを提供していますが、データアクセスなどのテクノロジーなども対応していますので、今回は Microsoft XML Core Services (MSXML) を扱いたいと思います。

はじめに

MSXML は OS や Office などに同梱されている XML のパーサー機能です。
実ファイルとしては、DLL の形でSystem32 ディレクトリ (32bit モード用のモジュールは SysWOW64) に格納されています。
OS に同梱されており、基本的に追加インストールが不要なため、本 DLL を Office のマクロ機能などでご活用されている方も多くいらっしゃるかと思います。
最近、そういったユーザー様から、Windows 10 に移行した後から、MSXML 機能を使用するマクロが使えなくなったなどのお問い合わせを頂くことがあるため本ブログ記事にて詳細を説明したいと思います。

事象

Windows 7 環境などで問題なく稼働していた MSXML 機能を使うアプリケーション/マクロなどを、Windows 8 以降の環境で動作させようとすると下記のエラーが発生し、動作させることが出来ません。

コンパイルエラー: ユーザー定義型は定義されていません

例えば Word や Excel のマクロの参照設定で “Microsoft XML, v6.0″ を次のように選択し、マクロコード内で MSXML2.DOMDocument 型を使用している場合に本事象が発生します。
msxml6_pic1

原因

本事象の原因は、Windows 8 以降の MSXML (msxml6.dll) に MSXML2.DOMDocument型が存在しないことが原因です。
MSXML2.DOMDocument 型は、Windows 7 環境までは DLL に含まれていましたが、Windows 8 以降では含まれていません。
これは、MSXML2.DOMDocument 型は MSXML 3.0 が持つMSXML バージョンに依存しない動作を実現するための機能であり、MSXML 4.0 以降本機能は廃止されています。
英語ドキュメント、および、日本語機械翻訳しかないため少しわかりにくいですが、下記のドキュメントに記載があります。

 

What's New in MSXML
https://msdn.microsoft.com/en-us/library/ms753751.aspx (英語原文)
https://msdn.microsoft.com/ja-jp/library/ms753751.aspx (日本語機械翻訳)

What's New in MSXML 4.0

Version-Independent ProgIDs Removed

Version-independent ProgIDs were available in MSMXL and earlier releases and appeared such as the following when used in JScript code:

var xmldoc = CreateActiveXObject("MSXML2.DOMDocument")

or in Visual Basic code as:

Dim xmldoc As New MSXML2.DOMDocument

These types of version-independent ProgIDs are now removed with MSXML 4.0 and later versions.
This guarantees that MSXML 4.0 and later versions of MSXML will not interfere with any versions of MSXML (2.0, 2.6, or 3.0) previously installed.
If you use the previous examples in your code, you will not instantiate the MSXML 4.0 DOM, but instead the prior replace-mode versions of the MSXML parser (MSXML 3.0 or earlier depending on the version of Windows or other installed applications on the system that use MSXML).

If you want to use MSXML 4.0, you must create a DOMDocument object in JScript like this:

var xmldoc = CreateActiveXObject("MSXML2.DOMDocument.4.0")

Accordingly, for C++ and Microsoft Visual Basic you will create "MSXML2.DOMDocument40".
Similar changes will be necessary with all other MSXML objects in order to use the MSXML 4.0 version.

 

対処策

MSXML2.DOMDocument ではなく、MSXML2.DOMDocument60 のように明示的にバージョンを指定するよう、アプリケーションやマクロを改修します。
これは、OS (Windows 10 環境や Windows 7 環境など) や MSXML を使用するアプリケーションの種類に関わらず、MSXML 6.0 を使用する環境/アプリケーションで同様です。

 

参考情報

What's New in MSXML
https://msdn.microsoft.com/en-us/library/ms753751.aspx (英語原文)
https://msdn.microsoft.com/ja-jp/library/ms753751.aspx (日本語翻訳)

Installing and Redistributing MSXML
https://msdn.microsoft.com/en-us/library/cc507432.aspx (英語原文)
https://msdn.microsoft.com/ja-jp/library/cc507432.aspx (日本語翻訳)

List of Microsoft XML parser (MSXML) versions
https://support.microsoft.com/en-us/kb/269238 (英語原文)
https://support.microsoft.com/ja-jp/kb/269238 (日本語翻訳)