473,387 Members | 1,561 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Is a Word doc?

I would like to check if a document is a Word document. I'm currently just
checking for the "doc" extension but the extension can be changed so that's
no good.

Any examples or guidance on how to accomplish this?

Thanks.
Nov 16 '05 #1
1 4693
Hi Darin!

"Darin" schrieb
I would like to check if a document is a Word document. I'm currently just
checking for the "doc" extension but the extension can be changed so
that's
no good.

Any examples or guidance on how to accomplish this?


Of course you could use the Word Object Model to load the file. This would
require Word installed on the target machine and the scalability isn't good.
So forget it.

Another solution would be to use a binary reader and check the file-header,
every Word-file e.g. begins with "D0CF11E0". However, every Excel file as
well. So forget it.

My recommendation is:

If you right-click a Word-file in Explorer (on an NTFS-volume), click on
properties and then on the "file info"-tab you'll find an entry named
"Application Name". For a DOC-file on my machine this entry is "Microsoft
Office Word". We have to retrieve the value in this entry. This entry is
neither there if it is a txt-file (even if it has been created by Word), nor
if it is a file that only has the extension "DOC" but isn't a "Word-file".

All we need to achieve this is a custom wrapper around the OLE Structured
Storage-COM-Interfaces. The VB.NET MVP Eduardo A. Morcillo has already done
this. You can download his library here:

http://www.mvps.org/emorcillo/dotnet/grl/

The library itself is written in VB.NET, but this doesn't matter of course,
as long as it is .NET.

The rest is trivial: install the library, add a reference to "Edanmo's OLE
Storage Classes" and have a look at the following code which I have
commented for you:

using Edanmo.OleStorage;

Storage file;
PropertySetStorage propSet;
PropertyStorage propStg;

//open file
file = new Storage("c:\\test.doc");

//get Property Set Storage
propSet = file.PropertySetStorage;

//properties are divided in 3 groups.
//3 groups are predefined:
//SummaryInformation, DocSummaryInformation and UserProperties
//in this case we want the SummaryInformation
propStg = propSet.Open(PropertySetStorage.FMTID_SummaryInfor mation);

//read the Application Name-Entry
//the PropertyStorage.SummaryProperty-Enum has to be
//explicitly casted in C#
//
//results on my machine in "Microsoft Office Word"
Debug.WriteLine(
"Application Name: "
+
propStg[(int)PropertyStorage.SummaryProperty.ApplicationNa me]);

//clean up!
propStg.Close();
propSet.Close();
file.Close();
In your implementation you could simply check if the
propStg[(int)PropertyStorage.SummaryProperty.ApplicationNa me] is "Microsoft
Office Word" and you're done.

Of course this will work for all other OLE Storage Containers as well
(Excel, PowerPoint, etc.).

Cheers

Arne Janning
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
3
by: Yohancef Chin | last post by:
Hi, Being fairly new to .NET I am looking for a way to call MS Word from an event on a webform, and after the user is finished save that created document to an SQL Server database. Has anyone...
2
by: Mikey | last post by:
Sample VB .NET source code to create mailing labels or customized letters using MS Word MailMerge This VB .NET source code will start MS Word and call methods and set properties in MS Word to...
7
by: Dave | last post by:
Apologies for the newbie question. I have created a vb.net program for my company that is designed to work with Word Templates (about forty of them that we commonly use) that are selected by the...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
1
by: beanie | last post by:
i am a c programming beginner and i am trying to Create a concordance of Word Count for a text File in c programming but my code isn't working.please can u help me out.here is my code: #include...
2
by: beanie | last post by:
i am a beginer in c programming and i am trying to Create a Concordance of Word Count for a Text File but my code is not working.pls can anyone helpme out.here is my code: #include <stdio.h>...
0
by: alivip | last post by:
I write code to get most frequent words in the file I won't to implement bigram probability by modifying the code to do the following: How can I get every Token (word) and ...
5
by: alivip | last post by:
How can I get every Token (word) and PreviousToken(Previous word) From multube files and frequency of each two word my code is trying to get all single word and double word (every Token (word) and...
1
by: dittytwo | last post by:
Hi there The below code works if you don't try to do anything to the word document which is great (Not :D) I am having problems with the context command both text (i.e initial document with no...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.