Forum Discussion
Convert compatibility legacy doc to docx
I'm looking for a PS script to convert a batch of legacy Word documents that are in compatibility mode. Unfortunately, the script I found online; https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fsteveknutson.blog%2F2021%2F06%2F09%2Fconverting-word-document-format-with-powershell%2F%23%3A~%3Atext%3DThe%2520script%2520can%2520be%2520run%2CPowerShell%2520and%2520Microsoft%2520Word%2520installed&data=05%7C01%7Co365su14%40microsoft.com%7C60f80e7cb2b84250b92008daddfed574%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C638066383869109819%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=MbIQnUds%2Fk0cg%2F4lJGyigWwOZrVYoO%2F5Coc1zeRcHjE%3D&reserved=0, only changes the documents extension from doc to docx. Reopening the document still shows it in Compatibility Mode.
4 Replies
- AndySvintsIron Contributor
Hello M_sqd,
Script that you are referring to is using SaveAs method, but Microsoft.Office.Interop.Word also contains SaveAs2 method which has CompatibilityMode parameter.
This parameter is based on WdCompatibilityMode Enum (WdCompatibilityMode Enum (Microsoft.Office.Interop.Word) | Microsoft Learn) and has the following values:
wdCurrent 65535 Compatibility mode equivalent to the latest version of Word.
wdWord2003 11 Word 2010 is put into a mode that is most compatible with Word 2003. Features new to Word 2010 are disabled in this mode.
wdWord2007 12 Word 2010 is put into a mode that is most compatible with Word 2007. Features new to Word 2010 are disabled in this mode.
wdWord2010 14 All Word 2010 features are enabled.
wdWord2013 15 Default. All Word 2013 features are enabled.
So to fit your specific use case, you need to adjust your $document.SaveAs line to something like this:
$document.SaveAs2([ref]$docx_filename, [ref]$Format, [ref][Microsoft.Office.Interop.Word.WdCompatibilityMode]::wdCurrent)
Hope that helps.
- EvanSullivan37Copper Contributor
I am trying to use the same code the OP is using and making your changes. However I get the error. For the record line 10 is just "$document.Close()"
Parameter value was out of acceptable range
At line:10 char:5
+ $document.SaveAs2([ref]$docx_filename, [ref]$Format, [ref][Micros ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException- AndySvintsIron Contributor