I need to extract the “Last Author” field from several thousand Word files and have put this code together to get the values needed, but am having a difficult time getting the output to CSV. For a single file, it works for outputting the content on the screen.
I’ve tried Out-File and that gets me a single column of the $VarPropValue content.
I work with Custom Objects frequently, but am stuck on getting this converted to Key = $Var, Value = $VarPropValue, such that the key value is what-ever the content at the time the $Var variable passed through the foreach loop.
Using fixed key names, I often create dynamic value “values”, but an not sure how to create a loop with unique key values each time.
Key | Value
Title | My Title
Author | Someone Else
etc…
Title: My Title
Subject:
Author: Someone Else
Keywords:
Comments:
Template: Notebook Template
Last author: Harrington, Grant
Creation date: 01/08/2018 17:43:00
Last save time: 01/08/2018 17:43:00
Number of pages: 3
Company: My Company
$wordApplication = New-Object -ComObject word.application $document = $wordApplication.documents.open("C:\Users\Grant.Harrington\Desktop\TEMPWORD\Test\2018-01-08_22-43-00.DOCX"); $binding = "System.Reflection.BindingFlags" -as [type]; $builtinProperties = $document.BuiltInDocumentProperties #region Variables $WordVariables = 'Title', 'Subject', 'Author', 'Keywords', 'Comments', 'Template', 'Last author', 'Creation date', 'Last save time', 'Number of pages', 'Company' foreach ($var in $WordVariables) { # This is the Property Name we are trying to find $pVar = "$var" [Array]$VarArgs = $pVar #Get the Property item for the Comments property $VarProp = [System.__ComObject].InvokeMember("Item", $binding::GetProperty, $null, $builtinProperties, $VarArgs) #Get the value of the Comments property, so we can check if it contains a version string. $VarPropValue = [System.__ComObject].InvokeMember("value", $binding::GetProperty, $null, $VarProp, $null); Write-Output "$Var`: $VarPropValue" } #end foreach var #endregion Variables $document.Close($false) $wordApplication.Quit()