Sqlserver-dba.com

Subscribe

Subscribe to RSS feed  Follow @jackvamvas - Twitter

Enjoy this post? Enter your email address for updates on new posts:

Delivered by FeedBurner

Email +Jack Vamvas at jack@sqlserver-dba.com

SQLServer-DBA.com Links

Subscribe to newsletter

Dba_db2_button

Powered by TypePad
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();

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

Enjoy this post? Enter your email address for updates on new posts:

Delivered by FeedBurner

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


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