@@ -304,9 +304,23 @@ Write-Host "Saving document..."
304304$oldFilePath = " "
305305try {
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 ()
0 commit comments