none
PowerShell - Run C# to use Open Office XML assemblies? RRS feed

  • Question

  • I have a requirement to carry out a text replacement (multiple values) on a Word (docx) document as part of an automation workflow.  The text replacement will need to update text values within text boxes that have all been tagged with an easily identifiable "<'string value'>".

    This needs to be done via PowerShell which would be simple enough on a client with Office installed for example:

    Function OpenWordDoc($filename)
    {
        $word = New-Object -ComObject Word.Application
        Return $Word.documents.open($filename)
    }
    
    Function SaveAsWordDoc($Document,$Filename) 
    {
        $Document.Saveas([REF]$Filename)
        $Document.close()
    }
    
    Function ReplaceTag($Document, $FindText, $ReplaceWithText)
     {
        $FindReplace=$Document.ActiveWindow.Selection.Find
        $matchCase = $false;
        $matchWholeWord = $true;
        $matchWildCards = $false;
        $matchSoundsLike = $false;
        $matchAllWordForms = $false;
        $forward = $true;
        $format = $false;
        $matchKashida = $false;
        $matchDiacritics = $false;
        $matchAlefHamza = $false;
        $matchControl = $false;
        $read_only = $false;
        $visible = $true;
        $replace = 2;
        $wrap = 1;
    
        $FindReplace.Execute($findText, $matchCase, $matchWholeWord, $matchWildCards, $matchSoundsLike, $matchAllWordForms, $forward, $wrap, $format, $replaceWithText, $replace, $matchKashida ,$matchDiacritics, $matchAlefHamza, $matchControl) | Out-Null
     }
    
    
    $WordTemplate =  'C:\Temp\PS_Word_test.docx'
    $OutputFile =  'C:\Temp\PS_Word_test_Demo.docx'
    
    
    $Doc = OpenWordDoc -Filename $WordTemplate
    
    # Replace text tags
    ReplaceTag –Document $Doc -FindText ‘<Name_1>’ -replacewithtext “name”
    ReplaceTag –Document $Doc -FindText ‘<ID_1>’ -replacewithtext “userid”
    ReplaceTag –Document $Doc -FindText ‘<Remark_1>’ -replacewithtext “Some text”
    ReplaceTag –Document $Doc -FindText ‘<Name_2>’ -replacewithtext “name 2”
    ReplaceTag –Document $Doc -FindText ‘<ID_2>’ -replacewithtext “userid”
    ReplaceTag –Document $Doc -FindText ‘<Remark_2>’ -replacewithtext “some text 2”
    
    # Save FInal Report
    SaveAsWordDoc –document $Doc –Filename $OutputFile

    That is what I need to do, but on a 2016 Server with no Office installed and therefore no assemblies.

    Having "googled" and "binged" away as usual and I believe the only way to do this is using the "Open Office XML" via C# (I secretly pray that there is a way to do this within PowerShell natively and just don't know it yet!! )

    Assuming my prayers are unanswered then... My C# is extremely....... but I have found some samples that would appear to achieve what I require.

    Text Replacement with C#(source) :

    using ( WordprocessingDocument doc =
                        WordprocessingDocument.Open(@"yourpath\testdocument.docx", true))
                {
                    var document = doc.MainDocumentPart.Document
    
                    foreach (var text in document.Descendants<Text>()) // <<< Here
                    {
                         if (text.Text.Contains("text-to-replace"))
                         {
                              text.Text = text.Text.Replace("text-to-replace", "replaced-text");
                         }
                    } 
                }

    Execution of C# within PowerShell:

    $Assem = ( 
    
    
    ...add referenced assemblies here...
        ) 
    
    $Source = @" 
    ...add C# source code here...
    "@ 
    
    Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp 

    What I need to know:

    • How do I get the assembly information for Open Office XML (v2.5 installed to default path)?
    • Is the C# samples adequate for my needs?
    • Am I over complicating this and is there a better way?

    • Moved by Bill_Stewart Wednesday, September 4, 2019 6:53 PM This is not "design solution for me" forum
    Tuesday, March 19, 2019 4:15 PM

All replies

  • Since this is a PowerShell forum you will need to ask in a C# forum or an Open Office forum.


    \_(ツ)_/

    Tuesday, March 19, 2019 10:33 PM