Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving Built In Properties from MS Office Documents

COM and CORBA Objects

Retrieving Built In Properties from MS Office Documents

by  Glowball  Posted    (Edited  )
Microsoft Office documents have meta information associated with them and you can use ColdFusion and COM to retrieve that information. You can view this information in an Office product by going to File | Properties. This example puts all of the properties and their values in a structure for one Office document (Word).

Code:
<!--- Set this variable to the full path of your file --->
<cfset thisFile = "C:\test.doc">

<!--- ------------------------------- --->

<!--- Get the file's extension --->
<cfset fileExt = LCase(ListLast(Trim(thisFile), "."))>

<!--- Get the correct class based on the file's extension --->
<cfif VARIABLES.fileExt IS "doc" OR VARIABLES.fileExt IS "dot" OR VARIABLES.fileExt IS "rtf">
	<cfset thisClass = "Word.Application">
<cfelseif VARIABLES.fileExt IS "xls">
	<cfset thisClass = "Excel.Application">
<cfelseif VARIABLES.fileExt IS "ppt">
	<cfset thisClass = "PowerPoint.Application">
</cfif>

<!--- If necessary, create the COM object and put it in the Application scope --->
<cftry>
	<!--- First try to connect to an existing object --->
	<cfobject type="COM" name="fileObj" class="#thisClass#" action="CONNECT"> 
<cfcatch>
	<!--- There is no existing object, create one --->
	<cfobject type="COM" name="fileObj" class="#thisClass#" action="CREATE"> 
</cfcatch>
</cftry>

<!--- Work in the COM object to get the properties --->
<cfif VARIABLES.fileExt IS "doc" OR VARIABLES.fileExt IS "dot" OR VARIABLES.fileExt IS "rtf">
	<cfset docs = fileObj.Documents()>
<cfelseif VARIABLES.fileExt IS "xls">
	<cfset docs = fileObj.WorkBooks()>
<cfelseif VARIABLES.fileExt IS "ppt">
	<cfset docs = fileObj.Presentations()>
</cfif>
<!--- Open the document --->
<cflock timeout="20" name="openDoc" type="READONLY">
	<cfset objNewDoc = docs.open(thisFile)>
</cflock>
<!--- Make this one the active application --->
<cfset activeApp = objNewDoc.Application>
<!--- Make this one the active document --->
<cfif VARIABLES.fileExt IS "doc" OR VARIABLES.fileExt IS "dot" OR VARIABLES.fileExt IS "rtf">
	<cfset activeDoc = fileObj.ActiveDocument>   
<cfelseif VARIABLES.fileExt IS "xls">
	<cfset activeDoc = fileObj.ActiveWorkbook>   
<cfelseif VARIABLES.fileExt IS "ppt">
	<cfset activeDoc = fileObj.ActivePresentation>   
</cfif>
<!--- Get the built in properties --->
<cfset docProps = activeDoc.BuiltInDocumentProperties>    

<!--- Create a structure to hold the property/value pairs --->
<cfset sProperties = StructNew()>
<!--- Loop through the properties and add them to the structure --->
<cfloop collection="#docProps#" item="property">          
	<cfset thisPropName = property.name>     
	<!--- If a value is not defined it errors out, so try/catch it --->     
	<cftry>          
		<cfset thisPropValue = property.Value>     
	<cfcatch>          
		<cfset thisPropValue = "">     
	</cfcatch>     
	</cftry>     
	<!--- Add it to the structure --->     
	<cfscript>StructInsert(sProperties, thisPropName, thisPropValue);</cfscript>
</cfloop>

<!--- Shut down the application --->
<cftry>
	<cfset fileObj.Quit(0)>
<cfcatch>
	<!--- Sometimes it doesn't work, but only rarely --->
</cfcatch>
</cftry>
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top