Skip to content

Commit fb882ca

Browse files
committed
Fixes for Word Templates
1 parent 1a0e45e commit fb882ca

2 files changed

Lines changed: 19 additions & 17 deletions

File tree

scripts/Build-VBA.ps1

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,23 @@ Write-Host "Saving document..."
304304
$oldFilePath = ""
305305
try {
306306
if ($officeAppName -eq "Word") {
307-
# For PowerPoint, use SaveAs with the same file name to force save
308-
$doc.SaveAs($outputFilePath)
309-
Write-Host "Document saved using SaveAs method"
307+
# For Word, check if the file name ends with .dotm.docm
308+
# If so, we need to save as .dotm
309+
if ($outputFilePath.EndsWith(".dotm.docm")) {
310+
$oldFilePath = $outputFilePath
311+
$outputFilePath = $outputFilePath -replace "\.dotm\.docm$", ".dotm"
312+
# Replace forward slashes with backslashes
313+
$outputFilePath = $outputFilePath -replace "/", "\"
314+
Write-Host "Saving document as .dotm: $outputFilePath"
315+
$doc.SaveAs($outputFilePath, 15) # 15 is the wdFormatXMLTemplateMacroEnabled file format for .dotm
316+
# Delete the .dotm.docm file
317+
Remove-Item -Path $oldFilePath -Force
318+
Write-Host "Document saved as .dotm at ${doc.Path}"
319+
} else {
320+
# We just use SaveAs since it's a normal Word document
321+
$doc.SaveAs($outputFilePath)
322+
Write-Host "Document saved using SaveAs method"
323+
}
310324
} elseif ($officeAppName -eq "PowerPoint") {
311325
# For PowerPoint, we need to check if the file name ends with .ppam.pptm
312326
# If so, we need to save as .ppam
@@ -332,7 +346,7 @@ try {
332346
# Delete the .potm.pptm file
333347
Remove-Item -Path $oldFilePath -Force
334348
Write-Host "Document saved as .potm"
335-
349+
336350
} else {
337351
$doc.Save()
338352
Write-Host "Document saved successfully"
@@ -362,18 +376,6 @@ try {
362376
# Delete the .xltm.xlsm file
363377
Remove-Item -Path $oldFilePath -Force
364378
Write-Host "Document saved as .xltm at ${doc.Path}"
365-
366-
# Check if the extension is .dotm and if so save as .dotm
367-
} elseif ($outputFilePath.EndsWith(".dotm.docm")) {
368-
$oldFilePath = $outputFilePath
369-
$outputFilePath = $outputFilePath -replace "\.dotm\.docm$", ".dotm"
370-
# Replace forward slashes with backslashes
371-
$outputFilePath = $outputFilePath -replace "/", "\"
372-
Write-Host "Saving document as .dotm: $outputFilePath"
373-
$doc.SaveAs($outputFilePath, 16) # 16 is the wdFormatXMLTemplateMacroEnabled file format for .dotm
374-
# Delete the .dotm.docm file
375-
Remove-Item -Path $oldFilePath -Force
376-
Write-Host "Document saved as .dotm at ${doc.Path}"
377379

378380
} else {
379381
$doc.Save()

tests/WordTemplate.dotm/Class Modules/Class1.cls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Public Sub ExecuteWrite()
1616
Dim fileNum As Integer
1717

1818
' Specify the path to the text file
19-
filePath = ThisDocument.Path & "\WordDocument.txt"
19+
filePath = ThisDocument.Path & "\WordTemplate.txt"
2020

2121
' Get a free file number
2222
fileNum = FreeFile

0 commit comments

Comments
 (0)