Jump to content

Check for custom Properties in MS Word 2010


Recommended Posts

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
Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

So which route do you want to go?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

 

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,

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...