Document Properties in Microsoft Office Word and Excel

Document properties, both built-in and custom, are a Microsoft Office-wide feature. The Word 2003 and Microsoft Office Excel expose document properties and provide the same way to work with them. These properties provide a powerful way to add and track specific information for a document.

The code below show how to modify, add and retrieve document properties for Word and Excel.

This is sample code. Add error handling and adjust to your requirements as necessary.

CLEAR
 
#DEFINE ccWord
 
#IFNDEF ccWord
	#DEFINE ccExcel
#ENDIF
 
#DEFINE msoPropertyTypeNumber	1		&& Integers
#DEFINE msoPropertyTypeBoolean	2		&& Logical	
#DEFINE msoPropertyTypeDate		3		&& Datettime
#DEFINE msoPropertyTypeString	4		&& String/text
#DEFINE msoPropertyTypeFloat	5	    && Real numbers
 
#IFDEF ccWord
	LOCAL oWord AS "Word.Application"
	oWord = CREATEOBJECT("Word.Application")
	oDoc = oWord.Documents.Add()
#ELSE	
	LOCAL oExcel AS "Excel.Application"
	oExcel = CreateObject("Excel.Application")
	oDoc = oExcel.Workbooks.Add()
#ENDIF
 
* Reference to Custom Document Properties collection
oCDP = oDoc.CustomDocumentProperties
 
* Add a few diffrent type of properties
oCDP.Add("My Property 1",.F.,msoPropertyTypeString , "Some text")
oCDP.Add("My Property 2",.F.,msoPropertyTypeBoolean , .T.)
oCDP.Add("My Property 3",.F.,msoPropertyTypeNumber, 1234)
oCDP.Add("My Property 4",.F.,msoPropertyTypeFloat, 1234.56)
oCDP.Add("My Property 5",.F.,msoPropertyTypeDate, DATETIME())
 
* Change value of newly created property
oCDP.Item("My Property 3") = 89798789
 
* Display created properties
FOR i=1 TO oCDP.Count
	oProp = oCDP.Item(i)
	? oProp.Name, "=", oProp.Value
ENDFOR
 
 
* Built-in properties
oBDP = oDoc.BuiltInDocumentProperties
* Set one of the properties
oBDP.Item("Comments") = "Testing Custom Document Properties"
 
* Display built-in properties
?
FOR i=1 TO oBdp.Count
	oProp = oBdp.Item(i)
	IF TYPE("oProp.Value") <> "U"
		? oProp.Name, "=", oProp.Value
	ELSE	
		? oProp.Name, " - Unknown value" 
	ENDIF	
ENDFOR
 
oDoc.Saved = .T.
 
#IFDEF ccWord
	oWord.Quit()
#ELSE	
	oExcel.Quit()
#ENDIF
Your rating: None Average: 5 (1 vote)