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


Open in new window



Could you help me please?

Thank you inadvance for your help

Christophe





ASKER CERTIFIED SOLUTION
Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 4 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 4 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004