Fixed issue with Word Interop not loading
Added support for PDF output
This commit is contained in:
Mikael Karlsson
2025-05-25 21:07:57 +10:00
parent e1328ac7dd
commit 5984acfca0
3 changed files with 42 additions and 3 deletions

View File

@@ -422,7 +422,7 @@ function Add-CDDocumentCustomProfileValue
$value = $obj.startMenuAppListVisibility
if($value.IndexOf(", ") -eq -1)
{
$value = $value -replace ",",", " # Option values in json file has space afte , but value in object don't
$value = $value -replace ",",", " # Option values in json file has space after , but value in object don't
}
Invoke-TranslateOption $obj $prop -PropValue $value
return $false

View File

@@ -10,15 +10,32 @@ function Invoke-InitializeModule
{
if(!("Microsoft.Office.Interop.Word.Application" -as [Type]))
{
$loaded = $false
try
{
Add-Type -AssemblyName Microsoft.Office.Interop.Word
$loaded = $true
}
catch { }
try
{
$wordFile = Get-ChildItem -path "$($env:windir)\assembly\GAC_MSIL" -Filter "Microsoft.Office.Interop.Word.dll" -Recurse
if($wordFile -and $wordFile.Exists)
{
Add-Type -Path $wordFile.FullName
$loaded = $true
}
}
catch
{
Write-LogError "Failed to add Word Interop type. Cannot create word documents. Verify that Word is installed properly." $_.Exception
return
}
if(-not $loaded) {
Write-LogError "Word Interop type not found. Cannot create word documents. Verify that Word is installed properly." $_.Exception
return
}
}
Add-OutputType ([PSCustomObject]@{
@@ -46,6 +63,9 @@ 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:cbWordDocumentationLevel.ItemsSource = ("[ { Name: `"Full`",Value: `"full`" }, { Name: `"Limited`",Value: `"limited`" }, { Name: `"Basic`",Value: `"basic`" }]" | ConvertFrom-Json)
$global:cbWordDocumentationLevel.SelectedValue = (Get-Setting "Documentation" "WordDocumentationLevel" "full")
@@ -114,6 +134,7 @@ function Invoke-WordPreProcessItems
{
Save-Setting "Documentation" "WordExportProperties" $global:cbWordDocumentationProperties.SelectedValue
Save-Setting "Documentation" "WordCustomDisplayProperties" $global:txtWordCustomProperties.Text
Save-Setting "Documentation" "WordDocumentFormat" $global:cbWordDocumentationFormat.SelectedValue
Save-Setting "Documentation" "WordDocumentTemplate" $global:txtWordDocumentTemplate.Text
Save-Setting "Documentation" "WordDocumentName" $global:txtWordDocumentName.Text
@@ -384,6 +405,14 @@ function Invoke-WordPostProcessItems
$script:doc.TablesOfFigures | ForEach-Object -Process { $_.Update() | Out-Null }
$script:doc.TablesOfAuthorities | ForEach-Object -Process { $_.Update() | Out-Null }
if($global:cbWordDocumentationFormat.SelectedValue -eq "pdf")
{
$format = [Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatPDF
}
else {
$format = [Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatDocumentDefault
}
$fileName = $global:txtWordDocumentName.Text
if(-not $fileName)
{
@@ -392,7 +421,10 @@ function Invoke-WordPostProcessItems
$fileName = Expand-FileName $fileName
$format = [Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatDocumentDefault
if($format -eq [Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatPDF -and $fileName -notlike "*.pdf")
{
$fileName = [IO.Path]::ChangeExtension($fileName, ".pdf")
}
try
{

View File

@@ -57,6 +57,13 @@
</StackPanel>
<TextBox Text="" Name="txtWordCustomProperties" Margin="0,5,5,5" Grid.Row="1" Grid.Column="1"/>
<StackPanel Orientation="Horizontal" Margin="0,5,5,0" Grid.Row="2">
<Label Content="Document format" />
<Rectangle Style="{DynamicResource InfoIcon}" ToolTip="Specify the output format eg Docx or PDF" />
</StackPanel>
<ComboBox Name="cbWordDocumentationFormat" Margin="0,5,0,0" MinWidth="250" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Left"
DisplayMemberPath="Name" SelectedValuePath="Value" />
</Grid>
<Grid Grid.Row="1" Grid.ColumnSpan="2">