Function OpenWordDoc($Filename) { $Word=NEW-Object –comobject Word.Application Return $Word.documents.open($Filename) }With the document opened, I could perform all of the necessary changes, save the file and use the following function:
Function SaveAsWordDoc($Document, $FileName) { $Document.Saveas([REF]$Filename) $Document.close() }But what about the document changes? I found two challenges: how to replace a text tag (like <TAG_YOUR_NAME>) and how to add an image on a specific location. Here's how I did it: Replacing a text tag: This one was simple. I just wanted to identify specific text in the document and replace all the instances with other text. In order to achieve that I used the following function.
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 }Adding an image: I spent quite some time trying to find a way to achieve this. Basically, I wanted to add an image from a file located on my computer, in a specific place. I tried to create a text tag and replaced it, but it wasn't working. So I found a way to add images to a Word document using the method $Document.ActiveWindow.Selection.InlineShapes.AddPicture(). This partially solved my problem. With this method I could add an image in the document, in the location of the pointer. How did I solve this? As creating text tags were not the way to go, I learned that I could achieve this by creating bookmarks (in Word, go to Insert->Bookmarks); this way I could move the pointer to a bookmark and then add the image. Here is the function to do this:
Function AddImage($Document, $BookmarkName, $ReplaceWithImage) { $FindReplace=$Document.ActiveWindow $FindReplace.Selection.GoTo(-1,0,0,$Document.Bookmarks.item("$BookmarkName")) $FindReplace.Selection.InlineShapes.AddPicture("$replacewithImage") }Note that the bookmark won't be removed once you add the image. If you try to add multiple images on the same bookmark, it will work. Now that we have the main functions, how can we make this work altogether? This is simple:
$TemplateFile = "C:\reports\Template_Report.docx" $FinalFile = "C:\reports\FinalReport.docx" # Open template file $Doc=OpenWordDoc -Filename $TemplateFile # Replace text tags ReplaceTag –Document $Doc -FindText '<client_name>' -replacewithtext "Pythian" ReplaceTag –Document $Doc -FindText '<server_name>' -replacewithtext "WINSRV001" # Add image AddImage –Document $Doc -BookmarkName 'img_SomeBookmark' -ReplaceWithImage "C:\reports\img.png" # Save FInal Report SaveAsWordDoc –document $Doc –Filename $FinalFile
[bctt tweet="With automation we can achieve more than purely technical tasks." username="pythian"] With automation we can achieve more than purely technical tasks. As shown, we can take advantage of it and use this time to focus on what matters by leaving the repetitive and easy work to our "personal robot". With a template and a few PowerShell lines, we can automate reports, which will help standardize our work and provide a more in-depth report because there is more time for technical input and analysis. Looking for ways to resolve recurring problems in your business through the strategic use of automation? Pythian can help! Click here to learn more.
Ready to optimize your Oracle Database for the future?