Skip to content

Commit 2accd8f

Browse files
authored
Add files via upload
1 parent b3203df commit 2accd8f

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Add-Type -AssemblyName "System.Windows.Forms"
2+
3+
function Show-OpenFileDialog {
4+
$dialog = New-Object System.Windows.Forms.OpenFileDialog
5+
$dialog.Filter = "HTML Files (*.html)|*.html"
6+
$dialog.Title = "Select HTML File"
7+
if ($dialog.ShowDialog() -eq "OK") {
8+
return $dialog.FileName
9+
}
10+
return $null
11+
}
12+
13+
function Show-SaveFileDialog {
14+
$dialog = New-Object System.Windows.Forms.SaveFileDialog
15+
$dialog.Filter = "DOCX Documents (*.docx)|*.docx"
16+
$dialog.Title = "Save DOCX File"
17+
if ($dialog.ShowDialog() -eq "OK") {
18+
return $dialog.FileName
19+
}
20+
return $null
21+
}
22+
23+
$inputFile = Show-OpenFileDialog
24+
if (-not $inputFile) {
25+
Write-Host "No HTML file selected. The script will be canceled."
26+
exit
27+
}
28+
29+
$outputFile = Show-SaveFileDialog
30+
if (-not $outputFile) {
31+
Write-Host "No DOCX file selected. The script will be canceled."
32+
exit
33+
}
34+
35+
try {
36+
$word = New-Object -ComObject Word.Application
37+
$word.Visible = $false
38+
$doc = $word.Documents.Add()
39+
$doc.Content.InsertFile($inputFile)
40+
$doc.SaveAs([ref] $outputFile, [ref] 16) # 16: wdFormatXMLDocument
41+
} catch {
42+
Write-Error "Error processing the file: $_"
43+
} finally {
44+
if ($doc -ne $null) { $doc.Close() }
45+
if ($word -ne $null) { $word.Quit() }
46+
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null
47+
}

0 commit comments

Comments
 (0)