Importing a template for Word generation

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 3 months ago.

  • Author
    Posts
  • #5662

    by rambog at 2013-03-13 12:08:01

    I have created a *.docx from some information contained within VMWare (using cmdlets from PowerCLI). Rather than creating a blank document, I would rather use a word template into which to import the dynamic content. I have looked into OpenXML but can't seem to get the snapins to register. Any other suggested ways I could create the docx from a pre-existing template? I have toyed with the idea of using the convert-to-xml for the inforamtion and then somehow bringing that into the template (also in xml format) but it may be another rabbitt hole.

    Here is the pertinent section of the code I have thus far:

    $Team=$Team.ToUpper()
    $VMHashTable=@{}
    connect-VIServer $VMServer
    $MyDocuments=[Environment]::GetFolderPath("MyDocuments")
    $TempHash=Import-csv $MyDocuments\scripts\serverrefresh.csv -Header "Servername","ATL"
    foreach ($row in $TempHash) {$VMHashTable[$row.Servername]=$row.ATL}
    $ATL=@()
    $MyVMs=get-vm|where-object {$_.Folder -like "$Team"}

    #Generate Questionnaire
    $word=new-object -ComObject "Word.Application"
    $doc=$word.documents.Add()
    $selection=$word.selection
    $selection.TypeText("Date & Time: ")
    $selection.TypeText((get-date))
    $selection.TypeParagraph()
    $selection.Font.Size=16
    $selection.TypeText("Team: $Team")
    $selection.Font.Size=12
    $selection.TypeParagraph()
    $selection.TypeParagraph()
    $selection.TypeParagraph()
    $selection.TypeText("Server`t`tOperating System`t`t`t`t`tCluster")
    foreach ($VM in $MyVMs){
    #Discover the ATL for all the servers

    $ATLi=$VMHashTable[$VM.name]
    if ($ATL -notcontains $ATLi) {
    $ATL+=$ATLi
    } #end if search of ATL to see if it already contains a unique entry for ATL

    $Cluster=$VM.vmhost.parent.name
    $selection.TypeParagraph()
    $selection.Font.Color="wdColorBlack"
    if ($Cluster -match "Cluster1" -or $Cluster -match "Cluster3") {
    $selection.Font.Color="wdColorRed"
    } #end if on the color change
    $selection.TypeText("$($VM.name)`t$($VM.guest.OSFullName)`t$($VM.vmhost.parent.name)")

    } #end foreach VM

    by rambog at 2013-03-13 12:59:11

    In doing some more digging, I have discovered that the template (*.dotx) has a number of fields defined therein. If I could use that to leverage the import of information, this may be an option.

    by rambog at 2013-03-15 07:21:26

    I have figured out that formfields contained in the template can be addresses as below. The lingering issue is that I would like to modify the color of the text input into some of these fields and the original "$selection.Font.Color="wdColorRed" doesn't seem to do the trick. Below is the revised code. Note: If I simply put the template in the documents.Add parenthesis, it seems to work fine (as long as I use $doc.SaveAs([ref]"filename.docx") to save to a different filename).
    $Team=$Team.ToUpper()
    $VMHashTable=@{}
    connect-VIServer $VMServer
    $MyDocuments=[Environment]::GetFolderPath("MyDocuments")
    $TempHash=Import-csv $MyDocuments\scripts\serverrefresh.csv -Header "Servername","ATL"
    foreach ($row in $TempHash) {$VMHashTable[$row.Servername]=$row.ATL}
    $ATL=@()
    $MyVMs=get-vm|where-object {$_.Folder -like "$Team"}
    #Create the document based upon template and populate the Mnemonic
    $word=new-object -ComObject "Word.Application"
    $doc=$word.documents.Add("$MyDocuments\ServerInfo\Questionnaire.dotx")
    $selection=$word.selection
    $AppTeam=$doc.Bookmarks.Item("Text128").Range
    $AppTeam.Text="$Team"

You must be logged in to reply to this topic.