Jump to content
Sign in to follow this  
mohan93

Check for custom Properties in MS Word 2010

Recommended Posts

mohan93

Hey Guys,

I have hundreds of word document, each word file have 10 Custom properties added (all document are in same template )

I need to check and ensure in every document, no custom properties are left blank. 

This manual work is more time consuming, am planning to make this task to be a robust and time saving. 

Planning to write a script to check for the 10 Custom properties in MS Word file and alert me if any field of that is left blank.

Will autoit script the above task, please guide me with your inputs.

Thanks in Advance.

Cheers,

Edited by mohan93

Share this post


Link to post
Share on other sites
water

Sure this can be done using AutoIt.

Have a look at the Word UDF that comes with AutoIt and the wiki how to access the custom properties.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
mohan93

Thanks for you reply,

I had a look at Word UDF, Seems it used to Set or Get Built In properties.

Global $oWord = _Word_Create()
Global $oDoc = _Word_DocOpen($oWord, @ScriptDir & "test.doc")
Global $wdPropertyAuthor = 3
Global $sAuthor = $oDoc.BuiltInDocumentProperties($wdPropertyAuthor).Value ; Retrieves the Author of the document

I have custom properties in my WORD file, (Sample.docx)

eg:

PACKAGE_NAME

PACKAGE_ID

PACKAGE_VERSION

how can i get

1. input for the word file (Sample.docx)

2. read the above custom properties. 

3. display if any NULL values.

Please suggest.

Share this post


Link to post
Share on other sites
junkew

Better to do it thru VBA (for speed and its native in your environment easy to debug)

Just a quick starter (you have to built the part to scan / open all documents you are interested in)

Sub ScanProperties()

 Set myDoc = ActiveDocument
 
 iBuiltIn = myDoc.BuiltInDocumentProperties.Count

 For i = 1 To iBuiltIn
 tName = myDoc.BuiltInDocumentProperties(i).Name
 On Error Resume Next
    tValue = myDoc.BuiltInDocumentProperties(i).Value
    If Err.Number <> 0 Then
        tValue = "Undefined value"
        Err.Clear
    End If
 On Error GoTo 0
 
    Debug.Print i & ";" & tName & ";" & tValue
 Next
 
 iBuiltIn = myDoc.CustomDocumentProperties.Count

 For i = 1 To iBuiltIn
 tName = myDoc.CustomDocumentProperties(i).Name
 On Error Resume Next
    tValue = myDoc.CustomDocumentProperties(i).Value
    If Err.Number <> 0 Then
        tValue = "Undefined value"
        Err.Clear
    End If
 On Error GoTo 0
 
    Debug.Print i & ";" & tName & ";" & tValue
 Next
 
 
End Sub
  • Like 1

Share this post


Link to post
Share on other sites
water

So which route do you want to go?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
jguinch

Something like this ?

#Include <Array.au3> ; Just for _ArrayDisplay
#Include <Word.au3>

Local $sFile = "test.docx"
Local $oWord = _Word_Create()
Local $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\" & $sFile)

; For one property, set its name as second parameter
Local $sPACKAGE_NAME = _WordDocGetCustomProperties($oDoc, "PACKAGE_NAME")
If $sPACKAGE_NAME = "" Then MsgBox(16, "Error", "PACKAGE_NAME property not set in " & $sFile)

; For all properties, do not set the second parameter
Local $aProperties = _WordDocGetCustomProperties($oDoc)
_ArrayDisplay($aProperties)



Func _WordDocGetCustomProperties($oDoc, $sPropertyName = "")
    If NOT IsObj($oDoc) Then Return SetError(1, 0, 0)

    Local $sName, $sValue
    Local $iCount = $oDoc.CustomDocumentProperties.count
    If $iCount = 0 Then Return SetError(1, 0, 0)
    
    Local $aResult[$iCount][2]
    If $sPropertyName <> "" Then Redim $aResult[1][2]

    For $i = 1 To $iCount
        $sName = $oDoc.CustomDocumentProperties($i).name
        $sValue = $oDoc.CustomDocumentProperties($i).value
        
        If $sPropertyName = "" Then
            $aResult[$i - 1][0] = $sName
            $aResult[$i - 1][1] = $sValue
        ElseIf $sPropertyName = $sName Then
            Return $sValue
        EndIf
    Next
    
    If $sPropertyName <> "" Then Return SetError(1, 0, 0)
    Return $aResult
EndFunc
  • Like 1

Share this post


Link to post
Share on other sites
mohan93

 

Something like this ?

#Include <Array.au3> ; Just for _ArrayDisplay
#Include <Word.au3>

Local $sFile = "test.docx"
Local $oWord = _Word_Create()
Local $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\" & $sFile)

; For one property, set its name as second parameter
Local $sPACKAGE_NAME = _WordDocGetCustomProperties($oDoc, "PACKAGE_NAME")
If $sPACKAGE_NAME = "" Then MsgBox(16, "Error", "PACKAGE_NAME property not set in " & $sFile)

; For all properties, do not set the second parameter
Local $aProperties = _WordDocGetCustomProperties($oDoc)
_ArrayDisplay($aProperties)



Func _WordDocGetCustomProperties($oDoc, $sPropertyName = "")
    If NOT IsObj($oDoc) Then Return SetError(1, 0, 0)

    Local $sName, $sValue
    Local $iCount = $oDoc.CustomDocumentProperties.count
    If $iCount = 0 Then Return SetError(1, 0, 0)
    
    Local $aResult[$iCount][2]
    If $sPropertyName <> "" Then Redim $aResult[1][2]

    For $i = 1 To $iCount
        $sName = $oDoc.CustomDocumentProperties($i).name
        $sValue = $oDoc.CustomDocumentProperties($i).value
        
        If $sPropertyName = "" Then
            $aResult[$i - 1][0] = $sName
            $aResult[$i - 1][1] = $sValue
        ElseIf $sPropertyName = $sName Then
            Return $sValue
        EndIf
    Next
    
    If $sPropertyName <> "" Then Return SetError(1, 0, 0)
    Return $aResult
EndFunc

Thanks for your Script. I tried running the script, am getting ERROR Undefined Functions for __Word_Create() and _Word_DocOpen()

I refered the UDF Word document too, am not able to resolve this, Please help.

Cheers,

Share this post


Link to post
Share on other sites
mohan93

 

Better to do it thru VBA (for speed and its native in your environment easy to debug)

Just a quick starter (you have to built the part to scan / open all documents you are interested in)

Sub ScanProperties()

 Set myDoc = ActiveDocument
 
 iBuiltIn = myDoc.BuiltInDocumentProperties.Count

 For i = 1 To iBuiltIn
 tName = myDoc.BuiltInDocumentProperties(i).Name
 On Error Resume Next
    tValue = myDoc.BuiltInDocumentProperties(i).Value
    If Err.Number <> 0 Then
        tValue = "Undefined value"
        Err.Clear
    End If
 On Error GoTo 0
 
    Debug.Print i & ";" & tName & ";" & tValue
 Next
 
 iBuiltIn = myDoc.CustomDocumentProperties.Count

 For i = 1 To iBuiltIn
 tName = myDoc.CustomDocumentProperties(i).Name
 On Error Resume Next
    tValue = myDoc.CustomDocumentProperties(i).Value
    If Err.Number <> 0 Then
        tValue = "Undefined value"
        Err.Clear
    End If
 On Error GoTo 0
 
    Debug.Print i & ";" & tName & ";" & tValue
 Next
 
 
End Sub

Thanks junkew

I will go with VBA also, but i have no idea on this.

Is that i need to write this VBA code in Visual Studio, Visual Basic IDE ?

or is that i can deveop this code or someother IDE

Please advice.

Share this post


Link to post
Share on other sites
mohan93

Whitch version of AutoIt are you using ?

Am using

Autoit v 3.3.6.1

Am writing script in SciTE whose version is 1.79, and running it from there.

Share this post


Link to post
Share on other sites
junkew

regarding VBA you can do it within Word itself
1. open a new document
2. press alt + f11 and you are in the VBA IDE directly
if shortcut is not working you have to turn on the opition on the Ribbon
File|Options|Customize the ribbon|on the right select 'Main tabs'|Check 'Developer'

  1. copy below to a new module
  2. Make a vba reference to Windows Scripting Runtime
  3. put some docs in %temp%testdocs
  4. Switch for help to a vba forum ;-) :bye:
'Make sure you have made a reference to microsoft scripting runtime
'Creating a FileSystemObject
Public FSO As New FileSystemObject
Sub ProcessFiles()

'Declaring variables
Dim oFolder As Folder
Dim oFile As File
Dim strPath As String

Dim NextRow As Long

'Specify the path of the folder
strDocPath = Environ("temp") & "\testdocs\"

'Create the object of this folder
Set oFolder = FSO.GetFolder(strDocPath)

'Check if the folder is empty or not
If oFolder.Files.Count = 0 Then
  MsgBox "No files were found...", vbExclamation
  Exit Sub
End If

'Loop through each file in the folder
    For Each oFile In oFolder.Files
    'List the name, size, and date/time of the current file
        Debug.Print oFile.Name & ";" & oFile.Size & ";" & oFile.DateLastModified & ";" & oFile.Type & ";" & FSO.GetExtensionName(oFile.Path)
        ScanProperties oFile.Path
    Next oFile
End Sub

Sub ScanProperties(strDoc As String)
    Dim mydoc As Document
 
 Set mydoc = Application.Documents.Open(strDoc, True, True)
 
 iBuiltIn = mydoc.BuiltInDocumentProperties.Count

 For i = 1 To iBuiltIn
 tName = mydoc.BuiltInDocumentProperties(i).Name
 On Error Resume Next
    tValue = mydoc.BuiltInDocumentProperties(i).Value
    If Err.Number <> 0 Then
        tValue = "Undefined value"
        Err.Clear
    End If
 On Error GoTo 0
 
    Debug.Print strDoc & ";" & i & ";" & tName & ";" & tValue
 Next
 
 iBuiltIn = mydoc.CustomDocumentProperties.Count

 For i = 1 To iBuiltIn
 tName = mydoc.CustomDocumentProperties(i).Name
 On Error Resume Next
    tValue = mydoc.CustomDocumentProperties(i).Value
    If Err.Number <> 0 Then
        tValue = "Undefined value"
        Err.Clear
    End If
 On Error GoTo 0
 
    Debug.Print i & ";" & tName & ";" & tValue
 Next
 
 mydoc.Close False
 
End Sub

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×