HOWTO: Determine the Version of a Microsoft Word DocumentLast reviewed: September 23, 1997Article ID: Q174140 |
The information in this article applies to:
SUMMARYThis article shows you how to determine the version of a Microsoft Word document.
MORE INFORMATIONMicrosoft 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 } 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 } } REFERENCESFor more information about OLE Compound files and Structured Storage, search the MSDN or Microsoft Visual C++ online help on "Structured Storage." For more information about the Microsoft Word file format, and/or the FIB structure, search the MSDN for "Microsoft Word 97 Binary File Format."
|
Additional query words: word97 winword
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |