Your strategic advantage starts here

Get and Set Microsoft Word Custom Document Properties

Get and Set Microsoft Word Custom Document Properties

Here’s a quick snippet that details how to get and set custom document properties in Microsoft Word 2010 documents:

#Create Word application
$wordApplication = New-Object -ComObject word.application

#Get reference to word doc
$document = $wordApplication.documents.open("C:\temp\alkane.docx");

#set up binding flags for custom properties
$binding = "System.Reflection.BindingFlags" -as [type];        
$customProperties = $document.CustomDocumentProperties

[Array]$propertyName = "ExampleProperty"
[Array]$propertyValue = "Example Value"

#Get property value
$myProperty = [System.__ComObject].InvokeMember("Item", $binding::GetProperty, $null, $customProperties, $propertyName)
$myPropertyValue  = [System.__ComObject].InvokeMember("value",$binding::GetProperty,$null,$myProperty,$null);

#Set property value
[System.__ComObject].InvokeMember("Value",$binding::SetProperty,$null,$myProperty,$propertyValue)

#Update all fields
$document.Fields.Update() | Out-Null

#save and close document

#required for O365
$document.Saved = $false;

$document.save();
$wordApplication.Quit();

 

Related Posts
4 Comments

I’ve been trying to get this to work on two different Windows 10 computers, one running Office 2013 (msi) and the other running Office 2016 (msi).  I get an error stating “Object reference not set to an instance of an object” on line 10 ($customPropertiesType = $customProperties.GetType()).  

Interestingly, this problem does not occur on a Windows Server 2012 R2 machine running Office 2013.  I’ve tried similar scripts from the scripting guys and other sites, but this same problem keeps arising.  I don’t know enough about theses objects and methods to understand why this would work in Windows Server 2012R2 but not on Windows 10.  Any ideas?

hi,

I have the same issue…do you have a workaround ?

 

thanks

Alex

Reallear

Hi,

I solved this by disabling line 10. And replace every “$customPropertiesType” by “[System.__ComObject]”, line 16 and 21.

Ray Backer

I’m also having the same issue with line

$customPropertiesType =$customProperties.GetType()

Windows 10.

As suggested I’ll replace it with [System.__ComObject]

 

Leave a Reply
Give us a call

Available from 9am to 8pm, Monday to Friday.

Send us a message

Send your message any time you want.

Our usual reply time: 1 Business day
Give us a call

Available from 9am to 8pm, Monday to Friday.

Send us a message

Send your message any time you want.

Our usual reply time: 1 Business day