|
| 1 | +Add-Type -AssemblyName "System.Windows.Forms" |
| 2 | + |
| 3 | +function Show-OpenFileDialog { |
| 4 | + $dialog = New-Object System.Windows.Forms.OpenFileDialog |
| 5 | + $dialog.Filter = "DOCX Documents (*.docx)|*.docx" |
| 6 | + $dialog.Title = "Select DOCX 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 = "HTML File (*.html)|*.html" |
| 16 | + $dialog.Title = "Save HTML File" |
| 17 | + if ($dialog.ShowDialog() -eq "OK") { |
| 18 | + return $dialog.FileName |
| 19 | + } |
| 20 | + return $null |
| 21 | +} |
| 22 | + |
| 23 | +# Select input DOCX file |
| 24 | +$inputFile = Show-OpenFileDialog |
| 25 | +if (-not $inputFile) { |
| 26 | + Write-Host "No DOCX file was selected. The script will be canceled." |
| 27 | + exit |
| 28 | +} |
| 29 | + |
| 30 | +# Select output HTML file |
| 31 | +$outputFile = Show-SaveFileDialog |
| 32 | +if (-not $outputFile) { |
| 33 | + Write-Host "No HTML file was selected. The script will be canceled." |
| 34 | + exit |
| 35 | +} |
| 36 | + |
| 37 | +# Verify that the input file exists |
| 38 | +if (-not (Test-Path $inputFile)) { |
| 39 | + Write-Error "The input file does not exist." |
| 40 | + exit |
| 41 | +} |
| 42 | + |
| 43 | +try { |
| 44 | + $word = New-Object -ComObject Word.Application |
| 45 | + $word.Visible = $false # Hides the Word application window |
| 46 | + $doc = $word.Documents.Open($inputFile) |
| 47 | + $doc.SaveAs([ref] $outputFile, [ref] 8) # 8: wdFormatHTML |
| 48 | + Write-Host "File saved in: $outputFile" -ForegroundColor Green |
| 49 | +} catch { |
| 50 | + Write-Error "Error processing the file: $_" |
| 51 | +} finally { |
| 52 | + if ($doc -ne $null) { $doc.Close() } |
| 53 | + if ($word -ne $null) { $word.Quit() } |
| 54 | + [System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null |
| 55 | +} |
0 commit comments