473,789 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4739
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
"Applicatio n 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.OleStora ge;

Storage file;
PropertySetStor age propSet;
PropertyStorage propStg;

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

//get Property Set Storage
propSet = file.PropertySe tStorage;

//properties are divided in 3 groups.
//3 groups are predefined:
//SummaryInformat ion, DocSummaryInfor mation and UserProperties
//in this case we want the SummaryInformat ion
propStg = propSet.Open(Pr opertySetStorag e.FMTID_Summary Information);

//read the Application Name-Entry
//the PropertyStorage .SummaryPropert y-Enum has to be
//explicitly casted in C#
//
//results on my machine in "Microsoft Office Word"
Debug.WriteLine (
"Applicatio n Name: "
+
propStg[(int)PropertySt orage.SummaryPr operty.Applicat ionName]);

//clean up!
propStg.Close() ;
propSet.Close() ;
file.Close();
In your implementation you could simply check if the
propStg[(int)PropertySt orage.SummaryPr operty.Applicat ionName] 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
3538
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 the moment the printed output is usually going to Word. It's turning into an unholy mess, because I'm having to prepare umpteen different Word templates, and the queries that drive them, depending on what events a course has.
3
6131
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 done this? Does it seem possible? I followed the instructions from a sample on the Microsoft knowledge base but it only seems to work when creating a VB.NET Windows .EXE, not an VB.NET ASP app.
2
13535
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 execute a MailMerge to create mailing labels or customized letters. A label name known to MS Word MailMerge mailing label wizard may be used or a template file containing the field names Line1 thru Line5 for each record to be printed. If a...
7
6251
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 user and populated (with info from an Access database) at run-time, then saved as Word documents. The program I have coded works fine -- it does what I need it to do. But it has two problems: (1) it runs very slowly, and (2) it does not seem to...
4
12443
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 this: can Access create the document and place it as an OLE object to the relevant table? Any help is greatly appreciated. Ricky
1
3013
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 <stdio.h> #include <ctype.h> #include <string.h> #include <stdlib.h> struct word { struct word *left; /* tree to the left */ struct word *right; /* tree to the right */ char *WORD;
2
4965
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> #include <ctype.h> #include <string.h> #include <stdlib.h> struct word { struct word *left; /* tree to the left */ struct word *right; /* tree to the right */ char *WORD;
0
1928
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 PreviousToken(Previous word) and frequency and probability From text file and put each one in cell in table For example if the text file content is "Every man has a price. Every woman has a price." First Token(word) is "Every" PreviousToken(Previous...
5
2540
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 PreviousToken(Previous word)) from multube files and get frequency of both. it can get for single word but double word give error line 50, in most_frequant_word word1+= ' ' + word_list IndexError: list index out of range import...
1
4399
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 text) or a subsequent addition of text, insert, to the document. as this is going to be used along side other com opened applications Excel power point etc. it needs to work in the self.X orientation as i can get it to work normally see code 2 ...
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9499
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10177
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4076
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.