none
PowerShell Word Script - Automate F9 to Update Properties

    Question

  • Hello,
    *FIRST TIME DOING WORD SCRIPTING, please dont bash!!*
    Hoping someone can point me in the right direction. I'm trying to bulk create Word documents based on a list of server names in a CSV file.
    CSV
    Server2003,Server2008
    DC01,DC10
    DC02,DC12
    DC03,DC13
    I created a Word document Template using the two of the built in properties since they were easier to work with and the script I found on the web helped out with this.
    *Modified it a bit so it would use a .dotx and save as .docx file.
    After I got that working and moved onto creating custom properties and updating those.
    HERE is the problem:
    The two custom properties are updated, but when I open the documents they have not changed. I have to press F9 on each one to get the value to change from server2003 to the new property value, and sever2008 to the new property value.

    How can I have Word update all the custom properties to the new value before saving the file? Here is the step I have to do manually AFTER the script successfuly creates the new word documents..

    Thanks
    #Import-CSV List
    $servers = Import-Csv C:\WordScript\ServerListv2.csv
    ForEach ($server in $servers)
    {
    #CustomProperties name and new value
    $propertyName1 = "Server2003"
    $value1 = $server.Server2003
    $propertyName2 = "Server2008"
    $value2 = $server.Server2008
    $fileName = 'C:\WordScript\CustomPropertyTest.dotx'
    $binding = "System.Reflection.BindingFlags" -as [type]
    #Start Word and Get Custom properties
    $word = New-Object -Com Word.Application
    $word.Visible = $false
    $document = $word.Documents.Open($fileName)
    $CustomProperties = $document.CustomDocumentProperties

    #Sets first Property/Value
    [Array]$getArgs = $propertyName1
    $CustomPropertiesType = $CustomProperties.GetType()
    $CustomProperty = $CustomPropertiesType.InvokeMember("Item", $binding::GetProperty,$null, $CustomProperties, $getArgs)
    $CustomPropertyType = $CustomProperty.GetType()
    [Array]$setArgs = $value1
    $CustomPropertyType.InvokeMember("Value",$binding::SetProperty,$null, $CustomProperty,$setArgs)

    #Sets Second Property/Value
    [Array]$getArgs = $propertyName2
    $CustomPropertiesType = $CustomProperties.GetType()
    $CustomProperty = $CustomPropertiesType.InvokeMember("Item", $binding::GetProperty,$null, $CustomProperties, $getArgs)
    $CustomPropertyType = $CustomProperty.GetType()
    [Array]$setArgs = $value2
    $CustomPropertyType.InvokeMember("Value",$binding::SetProperty,$null, $CustomProperty,$setArgs)

    #Save File based on new server name
    $path = "C:\WordScript\" + $server.Server2008 + ".docx"
    $wdFormatDocument = 16 
    $document.SaveAs([ref]$path,[ref]$wdFormatDocument)
    $word.Quit()
    }
    • Moved by Bill_Stewart Thursday, November 21, 2013 2:28 PM Moving to more appropriate forum
    Tuesday, February 28, 2012 10:01 AM

Answers

  • Yea this isnt working for me. The property of the document value is updated...BUT the Word document has 10 Entries through out the document using that custom property which still has the old value. Unless I do a F9 to update the fields, they have the old value. Word 2010 x64 bit.


    Since you started this onother forums I have tun this code almost 20 times. It works.  There must be aproblem in  your system.  We cannot fix your system because we cannot see it.  You need to spend time running diagnistic scripts to fighure out waht is going wrong.

    I am sorry but I don't see how we can make your code work.


    ¯\_(ツ)_/¯

    Wednesday, February 29, 2012 2:07 AM

All replies

  • I have set this up and run it and the document properties get updated as expected.

    I suspect you are having some other issue.

    There is no reason why, after saving the document, that just using F9 would cause the properties to magically become updated.  There is something you may be missing perhaps in the way you have designed the template.


    ¯\_(ツ)_/¯

    Tuesday, February 28, 2012 11:13 AM
  • Please explain what you did, as this worked fine when using built in properties, but not custom properties. I just tried it using a new template i created, then tried using a new word document too either updated, with out going in and press F9.

    New Word Document

    File - Info - Properties - Advanced Properties

    Custom

    Name: Server2003

    Value: S2003

    Add

    Name: Server2008

    Value: S2008

    Add

    Insert tab - Quick Parts - Field

    Categories: Document Information

    Field Names: DocProperty

    Property: Server2003

    Ok

    repeat for 2008

    File Save as Dotx

    Tuesday, February 28, 2012 2:57 PM
  • Here is pretty much the exact code from teh article although I have cleaned it up to make it more understandable. It works every time and does not reqiore the F9 key or any other sort of playing around.

    $template = "e:\test2\Test.dotx"
    $filename='e:\test2\test.docx'
    $word = New-Object -ComObject word.application
    #$word.Visible = $true
    $document = $word.documents.open($template)
    $binding = "System.Reflection.BindingFlags" -as [type]
    $customProperties = $document.CustomDocumentProperties
    $typeCustomProperties = $customProperties.GetType()
    $CustomProperty = "MyCustomProperty1"
    $Value = "CustomValue1"
    [array]$arrayArgs = $CustomProperty,$false, 4, $Value
    Try{
        $typeCustomProperties.InvokeMember('add',$binding::InvokeMethod,$null,$customProperties,$arrayArgs)
    }
    Catch{
      $propertyObject = $typeCustomProperties.InvokeMember('Item', $binding::GetProperty,$null,$customProperties,$CustomProperty)
      $typeCustomProperties.InvokeMember('Delete', $binding::InvokeMethod,$null,$propertyObject,$null)
      $typeCustomProperties.InvokeMember('add',$binding::InvokeMethod,$null,$customProperties,$arrayArgs)
    }
     
    $document.SaveAs([ref]$filename,[ref]16)
    $word.quit()
    Remove-Variable word
    [gc]::collect()
    [gc]::WaitForPendingFinalizers()


    ¯\_(ツ)_/¯

    Tuesday, February 28, 2012 8:21 PM
  • Yea this isnt working for me. The property of the document value is updated...BUT the Word document has 10 Entries through out the document using that custom property which still has the old value. Unless I do a F9 to update the fields, they have the old value. Word 2010 x64 bit.


    Wednesday, February 29, 2012 1:15 AM
  • Yea this isnt working for me. The property of the document value is updated...BUT the Word document has 10 Entries through out the document using that custom property which still has the old value. Unless I do a F9 to update the fields, they have the old value. Word 2010 x64 bit.


    Since you started this onother forums I have tun this code almost 20 times. It works.  There must be aproblem in  your system.  We cannot fix your system because we cannot see it.  You need to spend time running diagnistic scripts to fighure out waht is going wrong.

    I am sorry but I don't see how we can make your code work.


    ¯\_(ツ)_/¯

    Wednesday, February 29, 2012 2:07 AM
  • Could you please try to set $word.ActiveDocument.Fields.Update() before Save
    Thursday, November 21, 2013 10:39 AM