Log in

View Full Version : Saving word document property in excel



adnan0
07-26-2008, 11:43 AM
Here is the code that I have so far to show word document property in a message box

Sub DocumentProperties()
Dim Output As String
Dim Prop As DocumentProperty

For Each Prop In ActiveDocument.BuiltInDocumentProperties
Output = Output + Prop.Name + " = " + Str(Prop.Value) + vbCrLf
Next

MsgBox (Output)
End Sub

I want to rewrite this code as a word macro so that it saves property
names and its values in a new excel spreadsheet. The property name
should go in column A and and property values should go in column B

Nelviticus
07-28-2008, 04:08 AM
I think this should work. It doesn't atually use Excel as such, it just writes the data to a comma-separated text file (aka a csv file). By default these files open with Excel.
Public Sub DocumentProperties()

Const OutPath As String = "C:\Temp\XL"
Const OutFile As String = "DocumentProperties.csv"

Dim FileNumber As Integer
Dim Output As String
Dim Prop As DocumentProperty
Dim Name As String
Dim Value As String

Output = "Name,Value" + vbCrLf

On Error Resume Next

For Each Prop In ActiveDocument.BuiltInDocumentProperties
Name = Prop.Name
Value = "MISSING"
Value = CStr(Prop.Value)
Output = Output + Name + "," + Value + vbCrLf
Next

On Error GoTo Err_DocumentProperties

FileNumber = FreeFile
Open OutPath + "\" + OutFile For Output Access Write As #FileNumber
Print #FileNumber, Output
Close #FileNumber

Exit_DocumentProperties:

Exit Sub

Err_DocumentProperties:

MsgBox Err.Description
Resume Exit_DocumentProperties

End Sub
I had to add the "MISSING" bit because in Office 2007 you get an error when you try to access the .Value of some properties. I assume this is a bug and may also happen in other versions.

Note that the output directory needs to exist - the code I've written won't create it if it's not there.

Regards