troubleshooting Question

Questions about Powershell HTML and HTML to Word

Avatar of Christophe
ChristopheFlag for France asked on
Powershell
4 Comments1 Solution19 ViewsLast Modified:
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




Could you help me please?

Thank you inadvance for your help

Christophe





Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros