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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

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

Comments

Hi, i have read your blog and wondered if you could assist me with a small code
i am trying to find a code that will after asking me which folder location
will display the file name and custom properties of word documents within that folder into an excel spreadsheet

i would be greatful for any assistance you could give, i am new to vb and macros

thanks

Darren

hi;
How can recognize printer on/off with vfp6 .
thank you.

gandomi.