How to Convert Microsoft Word Format to HTML using Powershell

15 June,2012 by Jack Vamvas

Question : I have a list of Microsoft Word Docs , which need to be converted into html .   How can I do this with Powershell?    I've already checked ConvertTo-Html , but it's a different purpose

Answer:  Converting a  list of Microsoft Word documents  is repetitive and time consuming.  Powershell creates a new com object , which filters the Word Doc into html format.

The assembly loads  the type [Microsoft.Office.Interop.Word.WdSaveFormat]

This script reads all Word documents in the $srcFiles path and outputs them into the $htmlOutputPath.

 

[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.Office.Interop.Word.WdSaveFormat')
$docSrc="C:\word\"
$htmlOutputPath="C:\word\"
$srcFiles = Get-ChildItem $docSrc -filter "*.doc"
$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatFilteredHTML"); 
$wordApp = new-object -comobject word.application 
$wordApp.Visible = $False
          
function saveashtml
    { 
        $openDoc = $wordApp.documents.open($doc.FullName); 
        $openDoc.saveas([ref]"$htmlOutputPath\$doc.fullname.html", [ref]$saveFormat); 
        $openDoc.close(); 
    } 
      
ForEach ($doc in $srcFiles) 
    { 
        Write-Host "Converting to html :" $doc.FullName 
        saveashtml
        $doc = $null
    } 
  
$wordApp.quit();

Thanks to Bhargav Shukla for function concepts on this link

 Read More

Powershell to HTML

Powershell sql server security audit

SQL Server – Send email using Powershell - SQL Server DBA

SQL Server - Powershell and SQL Error Logs

SQL Server DBA Top 10 automation tasks

SQL Server - Top 10 DBA mistakes



Author: Jack Vamvas (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on How to Convert Microsoft Word Format to HTML using Powershell


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer