Question Code to get original version of MS Word file

mach_9

Member
Joined
May 24, 2016
Messages
20
Programming Experience
5-10
Our organization has moved to Office 2013 and we use MS Word to generate documents which are saved in our DB. With the new version of Word 2013, MS added a new 'file block' security feature which our security department will not allow us to use Word 97 and older documents or templates, which in some cases causes a problem for us as we do have older Word documents and templates in the DB. We will be converting our documents and templates to Word 2013, however, I was wondering if there is code out there that will determine the original version of a MS Word file before converting it to a new version.

There might be one problem, it is currently designed that in the table, the BLOB column will only contain Word documents and does not store the file extension when it was originally added to the DB, so it is a possibility that there are already .docx files there. Since I currently don't have code that checks to see what version of Word the document was originally created in, I pull the document out of the DB, save it as .DOC and then convert to .DOCX. I would like to remove a step where I don't have to convert an already .DOCX file.

I was hoping to find some code that would give me the original version so I would not have to do this.

Hope this makes sense.

Any help is much appreciated.

Thank you.
 
If you open document for automation the Document.SaveFormat property should reveal the file format version, WdSaveFormat enumeration (Microsoft.Office.Interop.Word).

It should also be possible to read the binary content as plain text and search for identifiers like "Word.Document.8" for Word 97-2003 format and possibly "word/document.xml" for Word 2007+ xml format.
 
John,

As the Word documents are stored in the DB as BLOBS, what I was thinking was to pull the document out, give it a .TXT file extension and then read the file and look for identifiers like "Word.Document.8" for Word 97-2003 format and "word/document.xml" for Word 2007, would that work or is there another way to do this?

I will research the INET for code but could you please provide it if you have it, I am currently working in C#.

Thank you.
 
A blob is a byte array, once you have retrieved it from database you can convert to string directly with GetString method of a System.Text.Encoding (f.ex UTF8), no need to put it in a file first.
 
John,

That will work but if I can simplify it, that would be much easier.

I am looking at the mydoc object and trying to find a property or function that will give me the current version of the document, but it doesn't seem to have it, see code below:

_Application myWordApp = new Application();
_Document myDoc = myWordApp.Documents.Open(FileName: filename, ReadOnly: false);

//This will give me the version of Word on my PC, 12.0, looking for the current version of the document.
string format = myWordApp.Version;

//This gives me "doc"
string docformat = myDoc.Application.DefaultSaveFormat;

myDoc.Close();
myWordApp.Quit();

I've gone through myDoc. looking for current_version or something that relates to the version of the document but can't seem to find anything.

I will keep looking.

Thanks
 
Did you read post 2?
 
Yes, I tried what you said in post # 2 and I cannot find a property for Document.SaveFormat in the object.

_Application myWordApp = new Application();
_Document myDoc = myWordApp.Documents.Open(FileName: filename, ReadOnly: false);

//I do not see a property of 'Document.SaveFormat' for the myDoc object
string version= myDoc.document.SaveFormat;

myDoc.Close();
myWordApp.Quit();

I tried this property but it gives me "doc", not what I am looking for.
string docformat = myDoc.Application.DefaultSaveFormat;

I also found below, however it doesn't give me the exact version of the document:

https://msdn.microsoft.com/en-us/library/bb238158(v=office.12).aspx

If ActiveDocument.SaveFormat = wdFormatDocument97 then
// your code that you want to execute
else
// your code that you want to execute
end if


I am looking for something like this:
string version= myDoc.document.OriginalVersion;
or
string version= myDoc.document.CurrentVersion;

version = 15, 14, 12, 11

Thanks

 
I cannot find a property for Document.SaveFormat in the object
Documentation says there is since Office 2010: _Document.SaveFormat Property (Microsoft.Office.Interop.Word)
I also found below, however it doesn't give me the exact version of the document:

If ActiveDocument.SaveFormat = wdFormatDocument97 then
That is the same but for VBA. The enumeration values should enable you to see the different types of documents loaded, for example wdFormatXMLDocument for docx.
 
John,

Thanks for your response but I am a bit perplexed as to why there isn't a property that indicates what version the current document is in.

My requirement is, I need to know if the Word file is 95, 97, 2000, 2003, 2007, 2010 or 2013.

By using a range of values doesn't help me.

As mentioned above, if I do a file read and find in a .txt format or a string array, could I find the exact version #?

For example, I know Word 97 is Word.Document.8, would the other versions follow suite in the same manner, Word.Document.9 = 2000, Word.Document.11 = 2003?

For 2007 and above it is PK or word/document.xml as the identifier, would 2010 and above be any different or would it still be identified as word/document.xml or would it be something similar to word/document.xml.2010, word/document.xml.2013?

Thanks
 
I guess I will have to use the text editor to get this information.

Once I have developed the code, I will post it.

Thanks for all your help John.
 
What I do below, is open the file in notepad and go line by line, when I find the key word, I move to the next file (as I am doing a batch check here).

StreamReader file = new StreamReader(FileName);
while ((line = file.ReadLine()) != null)
{
if (line.Contains("Word.Document.6"))
{
//
file.Close();
file.Dispose();
Word6++;
break;
}
else if (line.Contains("Word.Document.7"))
{
file.Close();
file.Dispose();
Word95++;
break;
}
else if (line.Contains("Word.Document.8"))
{
file.Close();
file.Dispose();
Word97orLater++;
break;
}
else if (line.Contains("word/document.xml"))
{
file.Close();
file.Dispose();
Word2007orLater++;
break;
}
else
{
file.Close();
file.Dispose();
Other++;
break;
}
}
 
Back
Top Bottom