El TecnoBaúl de Kiquenet

Kiquenet boring stories

Archive for 20 febrero 2014

Convert DOC (Office 97-2003) to DOCX (Office 2010) programatically or Bulk

Posted by kiquenet en 20 febrero 2014

Option 1) OMPM – Using  Office File Converter (OFC) and Version Extraction Tool

Download Office Migration Planning Manager (OMPM) to the machine that hosts the files.

Office Migration Planning Manager (OMPM): Office Compatibility
The Office Migration Planning Manager (OMPM) 2010 is a group of tools designed to help administrators during the planning and testing phases of a Microsoft Office 2010 or Office 365 deployment.
http://www.microsoft.com/en-us/download/details.aspx?id=11454

The installer extracts all tools and resource files by default to C:\OMPM

If your machine does not have Office 2003 installed, which by now it probably does not, then you will also need to install the Office compatibility pack to convert the files.

Módulo de compatibilidad de Microsoft Office para formatos de archivos de Word, Excel y PowerPoint
http://support.microsoft.com/kb/923505


Once that is installed, open the C:\OMPM\Tools folder that contains tools the Office File Converter (OFC) and Version Extraction Tool (VET)

Using OMPM tools
Use the Office File Converter (OFC) to do bulk conversions of specific Office files that are in version 97 through 2003 file formats to the Office 2010 file formats.
http://technet.microsoft.com/en-us/library/cc179179(v=office.14).aspx

Steps:

Edit OFC.INI file.

For the OFC.exe to work there must be a [FoldersToConvert] entry in the OFC.INI file.

Fldr=C:\OMPM\TOOLS\EXAMPLE FOLDER\ (in this case I put all the files I wanted to convert into the “EXAMPLE FOLDER”)

You can put multiple folders under this folder,


OFC.exe will follow this path a convert all DOC and XLS files into DOCX and XLSX files.

It will leave the old DOC file in the folder and create the new DOCX file right alongside.

The date created field will NOT change for the new DOCX or XLSX files


All the new files will be created in the appropriate folder, even if there are multiple layers of folders under the original location

In this case, the SourcePathTemplate and the DestinationPathTemplate listings in the OFC.INI file are just:

SourcePathTemplate=*\*\*\*\

DestinationPathTemplate=*1\*2\*3\*4\


Option 2) PowerShell

Requires Office 2010 installed

The main:

$CompatMode = [Enum]::Parse([Microsoft.Office.Interop.Word.WdCompatibilityMode], "wdWord2010")
$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat],"wdFormatDocumentDefault");

Clear-Host
$word = new-object -comobject word.application
$word.Visible = $False

Get-ChildItem -path $folderpath -recurse -include $fileType | % {

    SaveDocumentWithCompatibilityMode $word $_
    ConvertDocument $word $_
}

#Clean up
$word.quit()
$rc = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($word)
$word = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()

Save Document with Compatibility

Function SaveDocumentWithCompatibilityMode($word, $document)
{
    $opendoc = $word.documents.open($document.FullName)
    $savename = ($document.Fullname).substring(0,($document.FullName).lastindexOf("."))
   
    $savename = $savename + "_test1.docx"
   
    $opendoc.saveas([ref]"$savename", [ref]$saveFormat);
    $opendoc.close();
    
    $converteddoc = Get-ChildItem -Path $savename
    $opendoc = $word.documents.open($converteddoc.FullName)
    $opendoc.SetCompatibilityMode($CompatMode);
    $opendoc.save()
    $opendoc.close()
}


I prefer Convert Document

Function ConvertDocument($word, $document)
{
    $opendoc = $word.documents.open($document.FullName)
    #$opendoc = $word.documents.open($document.FullName,$false,$true);
   
    $savename = ($document.fullname).substring(0,($document.FullName).lastindexOf("."))
    $savename = $savename + "_test2.docx"
    
    $opendoc.Convert()
    $opendoc.saveas([ref]"$savename", [ref]$saveFormat);
    $opendoc.close();
   
   
    # Stop Winword Process
    #$opendoc.quit()
    #$rc = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($opendoc)
       
}

Referencias:
http://mongell.blogspot.com.es/2013/11/using-ompm-or-how-to-bulk-convert-pre.html

http://mykbit.blogspot.com.es/2011/10/bulk-convert-office-972003-files-to.html

http://blogs.msdn.com/b/ericwhite/archive/2008/09/19/bulk-convert-doc-to-docx.aspx

http://social.msdn.microsoft.com/Forums/sharepoint/en-US/04718fc5-ab83-45d9-a413-e3cd9d18926f/how-to-convert-doc-files-to-docx-in-a-sharepoint-library-programmatically?forum=sharepointdevelopmentprevious

http://row5is.wordpress.com/2012/07/06/convert-doc-to-docx-using-powershell/

http://stackoverflow.com/questions/16534292/basic-powershell-batch-convert-word-docx-to-pdf

http://blogs.technet.com/b/heyscriptingguy/archive/2010/06/22/hey-scripting-guy-how-can-i-use-windows-powershell-2-0-to-convert-doc-files-to-docx-files.aspx

Posted in Herramientas, Productividad, Scripts | Etiquetado: , , , , | Leave a Comment »