|
| 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