Getting code rom listfilesproperties to return a property of the active document

L

Larry

I've installed the ListProps template and Dso.exe (which seems to have installed "DS OLE document propreties 1.4 Object Library" in Tools, References), and it works. Using the listfilesproperties macro in the ListProps template I can create a table of the chosen properties of the documents in a folder, like this:

List of files from C:\Documents\Tests\

File name
Version

aaaaa.doc
6/95

Test.doc
6/95

testing delete.doc
6/95




But what I want to do is to dispense with the dialog box that comes with listfileproperties and run a simple macro to displays a message box showing the Version property just for the current document, not for all the documents in a folder. One of the MVPs, I think it was Jonathan West, suggested I could dig into the code in the ListProps template (or rather the code connected with the form that listfilesproperties opens) and create my own macro. But it's tricky, as the original macro is extremely long and complicated with many variables and with much of the code related to running the listfileproperties dialog box which browses to a folder, chooses multiple properties, and so on, none of which I need. So it's hard to figure how to work this into a simple code that returns just the Version property of the current document. I copied the lines of code from the macro that seem to be related to the task I want to achieve. Can anyone give me an idea of how to work these fragments into the code I want? It seems that what I really want to get at is that oDocProp.Version. I didn't copy the whole macro into this message as it is very long.

Thanks very much.
Larry



Option Explicit
Private oFilePropReader As DSOleFile.PropertyReader
Private oDocProp As DSOleFile.DocumentProperties

Dim strPropText As String

Set oDocProp = oFilePropReader.getdocumentproperties( _

WordBasic.FileNameInfo$(.FoundFiles(iFileIndex), 1))


strPropText = oDocProp.Version
 
L

Larry

I've gotten closer, but am not there yet. It's something like what I
have below. But I keep getting various error messages. The most recent
one is that "The file you have selected has no document properties."


Option Explicit
Private oFilePropReader As DSOleFile.PropertyReader
Private oDocProp As DSOleFile.DocumentProperties

Sub VersionNumber()

' trying to extract the code from ListProps.dot that will just
' give the version number for the current document.

' Dim myFileName As String
myFileName = ActiveDocument.Path

Set oFilePropReader = DSOleFile.PropertyReader
'Set oDocProp = oFilePropReader.GetDocumentProperties(myFileName)
Set oDocProp =
oFilePropReader.GetDocumentProperties(ActiveDocument.Path)
MsgBox "This document's file format is version " & oDocProp.Version
 
A

Astrid

Hi Larry,

The document you're trying to read the properties from has to be closed in order for the code to work. That's why the code in the example template checks if a file is locked (opened) before it reads the properties:

Sub ReadVersion()
Dim oFilePropReader As DSOleFile.PropertyReader
Dim oDocProp As DSOleFile.DocumentProperties
Dim sDocName As String

sDocName = "C:\Windows\Desktop\Doc1.doc"
If Not FileLocked(sDocName) Then
Set oFilePropReader = New DSOleFile.PropertyReader
Set oDocProp = oFilePropReader.GetDocumentProperties _
(sfilename:=sDocName)

MsgBox "Word version " & oDocProp.Version & vbCr & _
"Revision " & oDocProp.RevisionNumber
Else
MsgBox "Document " & sDocName & " is open" & vbCr & _
"Close it and retry"
End If
Set oDocProp = Nothing
Set oFilePropReader = Nothing

End Sub

Function FileLocked(strFileName As String) As Boolean
On Error Resume Next
Open strFileName For Binary Access Read Lock Read As #1
Close #1
' If an error occurs, the document is currently open.
If Err.Number <> 0 Then
FileLocked = True
Err.Clear
End If
End Function

Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post all follow-ups to the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/
 
L

Lars-Eric Gisslén

Larry,

I think you should try with ActiveDocument.FullName instead of .Path.. The
property .Path should give you only the path to the document without the
actual file name.

Regards,
Lars-Eric
 
L

Larry

Hi Astrid,

That was a great help. I never could have figured that out. It's funny
that for this to work the document has to be closed. I made some
further modifications and here's the result. Instead of testing if the
document is closed, I make assign some variables based on the active
document, then close it, then display the message box with the
information, then open the document again.

Thanks!

Larry

Sub ReadVersion()

Dim oFilePropReader As DSOleFile.PropertyReader
Dim oDocProp As DSOleFile.DocumentProperties
Dim sDocFullName As String, sDocName As String, sDoc As Document
Dim myVersion As String

Set sDoc = ActiveDocument
sDocName = ActiveDocument.Name
sDocFullName = ActiveDocument.FullName

sDoc.Close wdDoNotSaveChanges

Set oFilePropReader = New DSOleFile.PropertyReader
Set oDocProp = oFilePropReader.GetDocumentProperties _
(sFileName:=sDocFullName)

If oDocProp.Version = "6/95" Then myVersion = "WORD 6"
If oDocProp.Version = "97/2000" Then myVersion = "WORD 97"

MsgBox "Document: " & WordBasic.FileNameInfo(sDocName, 4) & _
vbCr & "File format: " & myVersion, _
vbOKOnly, "Word Version of Document"

Set oDocProp = Nothing
Set oFilePropReader = Nothing

Documents.Open sDocFullName
Application.GoBack

End Sub
 
J

Jonathan West

Larry said:
Hi Astrid,

That was a great help. I never could have figured that out. It's funny
that for this to work the document has to be closed. I made some
further modifications and here's the result. Instead of testing if the
document is closed, I make assign some variables based on the active
document, then close it, then display the message box with the
information, then open the document again.

Hi Larry,

If you want to get at the properties while the document is open, they are
available in the ActiveDocument.BuiltInDocumentProperties collection and the
ActiveDocument.CustomDocumentProperties collection. You don't need to use
dsofile.dll for that.

The whole idea bout dsofile.dll and code which uses it is that you can get
access to the properties while the document is closed!

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
 
L

Larry

Hi Jonathan,

The built in properties collection does not seem to include the Word
version of the file. I had asked a lot of questions about this earlier
and Cindy then directed me to the DSO file as the only way to get at
this information programmatically.

Below is the list from Word 97 VBA Help (without line breaks) of the
built-in properties. The only item on this list that might fit the bill
is "Format." However, when I run

Selection.TypeText ActiveDocument.BuiltInDocumentProperties("Format")

nothing happens.


TitleSubjectAuthorKeywordsCommentsTemplateLast AuthorRevision
NumberApplication NameLast Print DateCreation DateLast Save TimeTotal
Editing TimeNumber of Pages Number of WordsNumber of
CharactersSecurityCategoryFormatManagerCompanyNumber of BytesNumber of
LinesNumber of ParagraphsNumber of SlidesNumber of NotesNumber of Hidden
SlidesNumber of Multimedia Clips

Larry
 
A

Astrid

Hi Jonathan,
If you want to get at the properties while the document is open, they are
available in the ActiveDocument.BuiltInDocumentProperties collection and the
ActiveDocument.CustomDocumentProperties collection. You don't need to use
dsofile.dll for that.

This is what I was thinking too at first, but as far as I know there is no builtin property for Version. Or have I overlooked something

Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post all follow-ups to the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top