From cd26450a1032a3204976f92c7e8103f59bd8f6d1 Mon Sep 17 00:00:00 2001 From: Mikael Karlsson <43226266+Micke-K@users.noreply.github.com> Date: Tue, 11 Nov 2025 18:59:35 +1100 Subject: [PATCH] 3.10.1.6 Issues 375 - Add support for Strict Open XML document --- Extensions/DocumentationWord.psm1 | 54 ++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/Extensions/DocumentationWord.psm1 b/Extensions/DocumentationWord.psm1 index 5352ffc..c50ce07 100644 --- a/Extensions/DocumentationWord.psm1 +++ b/Extensions/DocumentationWord.psm1 @@ -63,8 +63,17 @@ function Add-WordOptionsControl $global:spWordCustomProperties.Visibility = (?: ($global:cbWordDocumentationProperties.SelectedValue -ne "custom") "Collapsed" "Visible") $global:txtWordCustomProperties.Visibility = (?: ($global:cbWordDocumentationProperties.SelectedValue -ne "custom") "Collapsed" "Visible") - $global:cbWordDocumentationFormat.ItemsSource = ("[ { Name: `"Docx`",Value: `"docx`" }, { Name: `"PDF`",Value: `"pdf`" }]" | ConvertFrom-Json) - $global:cbWordDocumentationFormat.SelectedValue = (Get-Setting "Documentation" "WordDocumentFormat" "docx") + $global:cbWordDocumentationFormat.ItemsSource = ("[ { Name: `"Word default document (docx)`",Value: `"wdFormatDocumentDefault`" }, { Name: `"Strict Open XML document`",Value: `"wdFormatStrictOpenXMLDocument`" }, { Name: `"PDF`",Value: `"wdFormatPDF`" }]" | ConvertFrom-Json) + $currentFormat = (Get-Setting "Documentation" "WordDocumentFormat" "wdFormatDocumentDefault") + if($currentFormat -eq "pdf") + { + $currentFormat = "wdFormatPDF" + } + elseif($currentFormat -eq "docx") + { + $currentFormat = "wdFormatDocumentDefault" + } + $global:cbWordDocumentationFormat.SelectedValue = $currentFormat $global:cbWordDocumentationLevel.ItemsSource = ("[ { Name: `"Full`",Value: `"full`" }, { Name: `"Limited`",Value: `"limited`" }, { Name: `"Basic`",Value: `"basic`" }]" | ConvertFrom-Json) $global:cbWordDocumentationLevel.SelectedValue = (Get-Setting "Documentation" "WordDocumentationLevel" "full") @@ -405,6 +414,17 @@ function Invoke-WordPostProcessItems $script:doc.TablesOfFigures | ForEach-Object -Process { $_.Update() | Out-Null } $script:doc.TablesOfAuthorities | ForEach-Object -Process { $_.Update() | Out-Null } + Write-Log "Using document format: $($global:cbWordDocumentationFormat.SelectedValue)" + try { + $format = [Microsoft.Office.Interop.Word.WdSaveFormat]$global:cbWordDocumentationFormat.SelectedValue + } + catch + { + Write-LogError "Document validation failed" $_.Exception + $format = [Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatDocumentDefault + } + + <# if($global:cbWordDocumentationFormat.SelectedValue -eq "pdf") { $format = [Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatPDF @@ -412,6 +432,7 @@ function Invoke-WordPostProcessItems else { $format = [Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatDocumentDefault } + #> $fileName = $global:txtWordDocumentName.Text if(-not $fileName) @@ -436,17 +457,28 @@ function Invoke-WordPostProcessItems Write-LogError "Failed to save file $fileName" $_.Excption } - if($global:chkWordOpenDocument.IsChecked -eq $true -and $global:hideUI -ne $true) - { - $script:wordApp.Visible = $true - $script:wordApp.WindowState = [Microsoft.Office.Interop.Word.WdWindowState]::wdWindowStateMaximize - $script:wordApp.Activate() - [Console.Window]::SetForegroundWindow($script:wordApp.ActiveWindow.Hwnd) | Out-Null + try { + if($global:chkWordOpenDocument.IsChecked -eq $true -and $global:hideUI -ne $true) + { + $script:wordApp.Visible = $true + $script:wordApp.WindowState = [Microsoft.Office.Interop.Word.WdWindowState]::wdWindowStateMaximize + $script:wordApp.Activate() + [Console.Window]::SetForegroundWindow($script:wordApp.ActiveWindow.Hwnd) | Out-Null + } + else + { + $script:doc.Close([Microsoft.Office.Interop.Word.WdSaveOptions]::wdDoNotSaveChanges) + $script:wordApp.Quit() + } } - else + catch { - $script:doc.Close([Microsoft.Office.Interop.Word.WdSaveOptions]::wdDoNotSaveChanges) - $script:wordApp.Quit() + Write-LogError "Failed to close the Word application" $_.Exception + } + finally { + [void][Runtime.InteropServices.Marshal]::ReleaseComObject($script:doc) + [void][Runtime.InteropServices.Marshal]::ReleaseComObject($script:wordApp) + [GC]::Collect(); [GC]::WaitForPendingFinalizers() } }