Search code examples
ms-wordole-automation

Fast search and replace of styles in MS Word


I have the need to automate conversion of html files into MS Word files, and as part of it I want to remove all ocorances of style Normal (Web) and replace them with just Normal.

I'm using the following

$find = $word.Selection.Find
$find.Style = $word.ActiveDocument.Styles.Item("Normal (Web)")
$find.Forward = $True
$find.Format = $True
while ($word.Selection.Find.Execute())
{
    $word.Selection.Style = $word.ActiveDocument.Styles.Item("Normal")
    $null = $word.Selection.EndKey(5)
}

which works but is slow. Is there a faster way?


Solution

  • Found an approch which doesn't replace, but has the desired effect:

    $style = $word.ActiveDocument.Styles.Item("Normal (Web)")
    $style.Delete()