locked
How to use PowerShell to insert Quick Parts in document footer RRS feed

  • Question

  • Hello,

    I want to insert a document property in document footer use powershell, the codes:

    $docPath = "D:\zfiles\Test.docx"

    $Word = New-Object -Com Word.Application

    $Word.Visible = $true
    $Doc = $Word.Documents.Open($docPath)
    $Section = $Doc.Sections.Item(1);
    $Footer = $Section.Footers.Item(1);
    $cntrl = [Enum]::Parse([Microsoft.Office.Interop.Word.WdContentControlType],"wdContentControlRichText")
    $ccc = $Footer.Range.ContentControls.Add($cntrl)
    [string]$str = "ns0:properties[1]/documentManagement[1]/ns3:Author[1]"
    $ccc.XMLMapping.SetMapping($str)

    but I always got error: can't convert "ns0:properties[1]/documentManagement[1]/ns3:Author[1]" type string to string

    Can anyone help me?

    Thursday, November 17, 2016 3:12 AM

Answers

  • SetMapping takes an XML string as an argument.

    You string must resolve to an XML node in the document.

    What is it you are trying to do?

    If you are trying to add the author string to the documents just create a field that is the author property.


    \_(ツ)_/


    • Edited by jrv Thursday, November 17, 2016 4:06 AM
    • Marked as answer by SF2016 Thursday, November 17, 2016 6:16 AM
    Thursday, November 17, 2016 4:05 AM

All replies

  • SetMapping takes an XML string as an argument.

    You string must resolve to an XML node in the document.

    What is it you are trying to do?

    If you are trying to add the author string to the documents just create a field that is the author property.


    \_(ツ)_/


    • Edited by jrv Thursday, November 17, 2016 4:06 AM
    • Marked as answer by SF2016 Thursday, November 17, 2016 6:16 AM
    Thursday, November 17, 2016 4:05 AM
  • Thanks for your reply.

    I want to insert a document property into footer, I can do this when I open a document, go to insert->quick parts->document properties, how could I do this use powershell?

    Thursday, November 17, 2016 4:23 AM
  • Here is how to retrieve the properties of the document.: https://blogs.technet.microsoft.com/heyscriptingguy/2012/08/02/use-powershell-to-find-specific-word-built-in-properties/

    For more detailed help post in the Word Developers Forum.

    You can just retrieve the property value and insert it in the footer.

    Here  is how to insert a footer field: http://stackoverflow.com/questions/4316157/powershell-add-field-codes-to-ms-word-footer


    \_(ツ)_/

    Thursday, November 17, 2016 4:42 AM
  • Thanks a lot. I solved the issue now, just use SetMappingByNode instead of SetMapping.

    The following are source codes, I'm not sure whether have any bug because I'm beginner for PowerShell.

    The custome column "ApproveLogs" was created in sharepoint library

    function TESTinsertCC(){
    $DocumentPath = "D:\zfiles\Test.docx" # Must exist
    $Word = New-Object -Com Word.Application
    $Word.Visible = $true
    $ActiveDocument = $Word.Documents.Open($DocumentPath)
    # Get the first Section of the Document object
    $Section = $ActiveDocument.Sections.Item(1);
    # Get the footer from the Section object
    #$Footer = $Section.Footers.Item(1);
    $Range = $Section.Footers.Item(1).Range

    $ContentControl = $Range.ContentControls.Add()
    $ContentControl.Title = "ApproveLogs"
    $CustomXMLNode = getDIPPropXPath $Range.Document "ApproveLogs"
    $ContentControl.XMLMapping.SetMappingByNode($CustomXMLNode)
    $ActiveDocument.save()
    $ActiveDocument.close()
    $Word.quit()
    }

    function getDIPPropXPath($TheDocument,[String] $ContentTypeItemName){
    $SchemaPartNamespaceURI = "http://schemas.microsoft.com/office/2006/metadata/contentType"
    $DataPartNamespaceURI = "http://schemas.microsoft.com/office/2006/metadata/properties"

    $cxpsSchema = $TheDocument.CustomXMLParts.SelectByNamespace($SchemaPartNamespaceURI)
    if($cxpsSchema.Count -eq 0){
    #MsgBox "Error: Schema CXP not found or does not have the expected Namespace URI"
    }else{
    if($cxpsSchema.Count > 1){
    #Debug.Print "Warning: more than one CXP with the expected Schema Namespace URI." & _" Using the first."
    }else{

    }

    $cxpSchema = $cxpsSchema[1]
    $strSchemaXPath = "//xsd:element[@ma:displayName='" + $ContentTypeItemName + "']"
    $cxnsSchemaElement = $cxpSchema.SelectNodes($strSchemaXPath)

    if($cxnsSchemaElement.count -eq 0){
    #MsgBox "Error: Could not find the schema element that defines the element."
    }else{
    if($cxnsSchemaElement.Count > 1){
    #Debug.Print "Warning: more than one Schema Element that defines the element."
    }

    $cxnSchemaElement = $cxnsSchemaElement[1]

    $strElementName = $cxnSchemaElement.SelectSingleNode("@name").Text

    $strElementNamespaceURI = $cxnSchemaElement.ParentNode.SelectSingleNode("@targetNamespace").Text

    $cxnSchemaElement = ""
    }

    $cxnsSchemaElement = ""
    $cxpSchema = ""

    # Now look for the item in the Custom XML part that contains the data
    $cxpsData = $TheDocument.CustomXMLParts.SelectByNamespace($DataPartNamespaceURI)

    if($cxpsData.Count -eq 0){
    #MsgBox "Error: Data CXP not found or does not have the expected Namespace URI"
    }else{
    if($cxpsData.Count > 1){
    #"Warning: more than one CXP with the expected Data Namespace URI." & _" Using the first."
    }
    $cxpData = $cxpsData[1]
    #write $cxpData.XML
    #Get the prefix for the Element's namespace.
    #Note that this may be different from any prefix
    #you may see if you inspect the Part's XML
    $strPrefix = $cxpData.NamespaceManager.LookupPrefix($strElementNamespaceURI)

    if($strPrefix -eq ""){
    $strDataXPath = "//" + $strElementName
    }else{
    $strDataXPath = "//" + $strPrefix + ":" + $strElementName
    }

    $cxnsDataElement = $cxpData.SelectNodes($strDataXPath)

    if($cxnsDataElement.Count -eq 0){
    #MsgBox "Error: Could not find the data element."
    }else{
    if($cxnsDataElement.Count > 1){
    #Debug.Print "Warning: more than one Data Element. Using the first."
    }

    $cxnDataElement = $cxnsDataElement[1]

    $cddd = $cxnDataElement

    $cxnDataElement = ""
    }

    $cxpData = ""

    $cxnsDataElement = ""
    }

    $cxpsData = ""
    }

    $cxpsSchema = ""

    return $cddd
    }


    TESTinsertCC


    Thursday, November 17, 2016 6:15 AM