Microsoft KB Archive/174140

{|
 * width="100%"|

HOWTO: Determine the Version of a Microsoft Word Document

 * }

Q174140

-

The information in this article applies to:


 * Microsoft Visual C++, versions 2.0, 2.1, 2.2, 4.0, 4.1
 * Microsoft Visual C++, 32-bit Enterprise Edition, versions 4.2, 5.0, 6.0
 * Microsoft Visual C++, 32-bit Professional Edition, versions 4.2, 5.0, 6.0
 * Microsoft Visual C++, 32-bit Learning Edition, version 6.0
 * Microsoft Word 97 for Windows
 * Microsoft Word for Windows 95, version 7.0
 * Microsoft Word for Windows, version 6.0

-

SUMMARY
This article shows you how to determine the version of a Microsoft Word document.

MORE INFORMATION
Microsoft Word saves its data in an OLE Compound file that is made up of streams and storages. In particular, it creates a data stream called "WordDocument" where it saves the contents and a special header called a "FIB" (File information block). This header contains information about the various attributes of the file that are documented in the MSDN as well as the version of Microsoft Word that saved the document. The following Microsoft Visual C++ code demonstrates how to open and read the WordDocument stream, and return the version number.

Sample Code
  // Word's File-Information-Block (FIB) structure... typedef struct _fib { short magicNumber; // Word 6.0: 0xA5DC // Word 7.0 (95): 0xA5DC // Word 8.0 (97): 0xA5EC

short version;  // >= 101 for Word 6.0 and higher... // Word 6.0: 101 // Word 7.0 (95): 104 // Word 8.0 (97): 105+ 103

} FIB, *LPFIB;

//*   WordVersionFromFile************************************************

//* Returns //*       6 for Word 6.0 //*       7 for Word 7.0 (95) //*       8 for Word 8.0 (97) //*       Negative if an error occurs... //****************************************************  int WordVersionFromFile(char *filename) { // Translate filename to UNICODE... WCHAR wcFilename[1024]; int i = mbstowcs(wcFilename, filename, strlen(filename)); wcFilename[i] = 0;

IStorage *pStorage; HRESULT hr; FIB fib;

// Open document as an OLE compound document... hr = ::StgOpenStorage(wcFilename, NULL, STGM_READ |

STGM_SHARE_EXCLUSIVE, NULL, 0, &pStorage);

if(!FAILED(hr)) { // Open the data-stream where Word stores the data... IStream *pStream; hr = pStorage->OpenStream(L"WordDocument", NULL,         STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &pStream);

if(!FAILED(hr)) { // Read relevant FIB information...           DWORD dwCount; // bytes read... pStream->Read(&fib, sizeof(FIB), &dwCount);

// Let go of our IStream pointer... pStream->Release; }        else return -2;

// Let go of our IStorage pointer... pStorage->Release; }     else return -1;

// Determine version to return... if(fib.version < 101) return fib.version;

switch(fib.version) { case 101: return 6; case 103: // fall-through... case 104: return 7; case 105: return 8; default: return 8; // Default, return the latest }

}