Link to home
Start Free TrialLog in
Avatar of Christophe
ChristopheFlag for France

asked on

Questions about Powershell HTML and HTML to Word

Dear Experts,

After I'am looking for solutions during days and days, I have to ask for help. I am a powershell newbie but I understand a little bit.

I writing this script to do that:
  • Explore a folder to get all subfolders and for each all files
  • Export the result in a HTML table
  • Insert this HTML table to a Word document

This table has 2 columns empty for later use.End user must be able to build a folder sctructure and get it in a word document. Then he will update empty columns.

After days and days of researches, I did not find how to to that:

  • I'd like to apply a background color for only folders and/or subfolders in the table.
  • I'd like to repeat headers on each word document
  • I'd like to change orientation of the Word document to Landscape by default.

This script must be used by any user and we can't install any powershell modules. It must be done with native powershell installation without admin rights.

Please find script below:

 $head = @"
 <Title>$Foldername - Folder structure</Title>
<style>
body { font-family:Calibri;
       font-size:11pt; }
td, th { border:1px solid #ddd; 
         border-collapse:collapse;
 }

th { color:white;
     background-color:#4286f4;
     padding-top: 12px;
     padding-bottom: 12px;
     font-size:14pt; }

table { width:100%;margin-left:5px; margin-bottom:20px;}
h2 {
 font-family:Arial;
 color:#6D7B8D;
 font-size:11pt;
}

</style>
"@

# Folder select

Add-Type -AssemblyName System.Windows.Forms
$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
$FolderBrowser.Description = 'Select the folder containing the data'
$result = $FolderBrowser.ShowDialog((New-Object System.Windows.Forms.Form -Property @{TopMost = $true }))
if ($result -eq [Windows.Forms.DialogResult]::OK){
$FolderBrowser.SelectedPath
} else {
exit
}

#Variables

$path = $FolderBrowser.SelectedPath
$Foldername = Split-Path -Path $FolderBrowser.SelectedPath -Leaf
$outputfile = "$path\FolderStructure.html"
$docfile = "$path\FolderStructure.docx"

#Request

$Data = Get-ChildItem -Recurse $path | Sort-Object fullname | Select-Object -Property @{Name = 'Dossier parent'; Expression = {$_.Parent}},@{Name = 'Nom de fichier'; Expression = {$_.Name}},@{Name = 'Commentaires'; Expression = {$_.Null}},@{Name = 'Pièce'; Expression = {$_.Null}}

#Output to HTML

$data | convertto-html -head $head | out-file $outputfile

#Converting html to docx

[ref]$SaveFormat = "microsoft.office.interop.word.WdSaveFormat" -as [type] 
$word = New-Object -ComObject word.application
$word.visible = $false
$Selection = $Word.Selection

$doc = $word.documents.open($outputfile) 

$doc.saveas([ref] $docfile, [ref]$SaveFormat::wdFormatDocumentDefault) 
$doc.close() 
$word.Quit() 
$word = $null 
[gc]::collect() 
[gc]::WaitForPendingFinalizers()

#Remove HTML file

Remove-Item $outputfile -Force -Recurse

#open Doc file

ii $docfile


Open in new window



Could you help me please?

Thank you inadvance for your help

Christophe





ASKER CERTIFIED SOLUTION
Avatar of Sam Jacobs
Sam Jacobs
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Christophe

ASKER

Thanks Sam, it is exactly what I need!
thanks again for your help.
Christophe
Thank you for your help! the result fits exactly my needs.
Yo are most welcome.