Page 1 of 1

Read and Set MS Word document properties using CoMatePlus.

Posted: Sun Feb 12, 2012 12:13 pm
by Ajm
Hi,

I have looked at all the demos that come with CoMatePlus and using those have managed to work with a document by reading the text etc.

Does anyone know how or if it is possible to access the document properties / metadata? I am at a loss and don't even know where to start.

Please can anyone point me in the right direction?

Re: Read and Set MS Word document properties using CoMatePlu

Posted: Sun Feb 12, 2012 8:22 pm
by Ajm
After several hours of pure trial and error I've managed work it out... I think...

Code: Select all

;/////////////////////////////////////////////////////////////////////////////////
;***COMate***  COM automation through iDispatch.
;*===========
;*
;*Retrieve and set document properties of a Word document using COMate_GetObject().
;*
;*Built by trial and error from (Demo_retrieve text from Word doc using GetObject.pb)
;*
;*By Andy Marshman
;*
;*Properties I have found to work for a Word document are:-
; Title
; Subject
; Author
; Keywords
; Comments
; Template
; Last Author
; Revision Number
; Application Name
; Last Print Date
; Creation Date
; Last Save Time
; Total Editing Time
; Number of Pages
; Number of Words
; Number of Characters
; Security
; Category
; Format
; Manager
; Company
; Number of Bytes
; Number of Lines
; Number of Paragraphs
; Number of Slides
; Number of Notes
; Number of Hidden Slides
; Number of Multimedia Clips
;*
;/////////////////////////////////////////////////////////////////////////////////

MyWordDoc$ = "c:\test2.doc"

IncludePath "..\"
XIncludeFile "COMatePLUS.pbi"


Define.COMateObject AppDocObject, WordDocObject, BuiltInProperties

;Using COMate_GetObject() allows us to obtain a Word document object directly.
;The alternative is to use COMate_CreateObject("Word.Application") and then use the "Documents\Open" object\method etc.
;In the following method call you can replace "Word.Document" by an empty string in this case as the MyWordDoc$ file is
;enough for COM to locate the correct object file.
AppDocObject = COMate_CreateObject("Word.Application")
If AppDocObject
   If AppDocObject\Invoke("Documents\Open('" + MyWordDoc$ + "')") = #S_OK
      WordDocObject = AppDocObject\GetObjectProperty("ActiveDocument")
      If WordDocObject
         BuiltInProperties = WordDocObject\GetObjectProperty("BuiltInDocumentProperties")
         If BuiltInProperties
            Title$ = BuiltInProperties\GetStringProperty("Item('Title')")
            Subject$ = BuiltInProperties\GetStringProperty("Item('Subject')")
            Author$ = BuiltInProperties\GetStringProperty("Item('Author')")
            Company$ = BuiltInProperties\GetStringProperty("Item('Company')")
            
            Created$ = BuiltInProperties\GetStringProperty("Item('Creation Date')")
            WordCount.i = BuiltInProperties\GetIntegerProperty("Item('Number of Words')")
            CharacterCount.i = BuiltInProperties\GetIntegerProperty("Item('Number of Characters')")
            Debug COMate_GetLastErrorCode()
            Debug COMate_GetLastErrorDescription()
            Revision.i = BuiltInProperties\GetIntegerProperty("Item('Revision Number')")
            Debug COMate_GetLastErrorCode()
            Debug COMate_GetLastErrorDescription()
            PageCount.i = BuiltInProperties\GetIntegerProperty("Item('Number of Pages')")
            Debug COMate_GetLastErrorCode()
            Debug COMate_GetLastErrorDescription()
            
            text$ = "Title: " + Title$ + Chr(13)
            text$ = text$ + "Subject: " + Subject$ + Chr(13)
            text$ = text$ + "Author: " + Author$ + Chr(13)
            text$ = text$ + "Company: " + Company$ + Chr(13)
            text$ = text$ + "Created: " + Created$ + Chr(13)
            text$ = text$ + "PageCount: " + Str(PageCount) + Chr(13)
            text$ = text$ + "WordCount: " + Str(WordCount) + Chr(13)
            text$ = text$ + "CharCount: " + Str(CharacterCoun) + Chr(13)
            text$ = text$ + "Revision: " + Str(Revision)
            
            MessageRequester("COMate -Get text from word document!", text$)
            
            ;Change a few of the properties
            
            If BuiltInProperties\SetProperty("Item('Title') = 'Changed Title'") <> #S_OK
               Debug COMate_GetLastErrorCode()
               Debug COMate_GetLastErrorDescription()
            EndIf
            
            If BuiltInProperties\SetProperty("Item('Subject') = 'This is the changed subject'") <> #S_OK
               Debug COMate_GetLastErrorCode()
               Debug COMate_GetLastErrorDescription()
            EndIf
            
            If BuiltInProperties\SetProperty("Item('Company') = 'New Company'") <> #S_OK
               Debug COMate_GetLastErrorCode()
               Debug COMate_GetLastErrorDescription()
            EndIf
            
            ; Save the document
            If WordDocObject\Invoke("Save()") <> #S_OK
               Debug COMate_GetLastErrorCode()
               Debug COMate_GetLastErrorDescription()
            EndIf
            
            ; Close the application
            If AppDocObject\Invoke("Quit()") <> #S_OK
               Debug COMate_GetLastErrorCode()
               Debug COMate_GetLastErrorDescription()
            EndIf
            
            ; Free up the COM Objects
            BuiltInProperties\Release()
         Else
            MessageRequester("COMate -Get text from word document!", "Couldn't create the BuiltInProperties object!")
         EndIf
         ; Free up the COM Objects
         WordDocObject\Release()
      Else
         MessageRequester("COMate -Get text from word document!", "Couldn't create the WordDocObject object!")
      EndIf
   Else
      MessageRequester("COMate -Get text from word document!", "Couldn't Open Document!")
   EndIf
   ; Free up the COM Objects
   AppDocObject\Release()
Else
   MessageRequester("COMate -Get text from word document!", "Couldn't create the AppDocObject object!")
EndIf