Word macro to remove formatting behaves differently when implemented as VB Script
Cross posting this from SO as I can use all the help I can get here, thanks!
There is an existing macro used to remove highlight formatting from a document:
Sub RemoveShading()
Selection.Font.Shading.Texture = wdTextureNone
Selection.Shading.BackgroundPatternColor = wdColorWhite
Selection.Shading.ForegroundPatternColor = wdColorWhite
End Sub
This has been working fine; when I select all text and run the macro with changes tracked in the document, I see the document updated with changes like:
Formatted: Pattern Clear
and
Formatted: Pattern: Solid (100%) (White)
in the locations where the highlighting previously existed in the document.
However, now, for reasons, I need to implement this as a VB Script (VBS). When I apply what I understand to be the exact same code:
' Open the current file
Set WordDoc = Word.Documents.Open(objFile.path)
' select and de-highlight the whole document
Word.Selection.WholeStory
Set WordText = Word.Selection
WordText.Font.Shading.Texture = wdTextureNone
WordText.Shading.BackgroundPatternColor = wdColorWhite
WordText.Shading.ForegroundPatternColor = wdColorWhite
' save in a new file to preserve original
newPath = objFSO.BuildPath(objFolder.path, objFSO.GetBaseName(objFile.path)&"_done.docx")
WordDoc.SaveAs(newPath)
WordDoc.Close
Instead, the entire document has tracked changes like:
Formatted: Font: Default (Arial)
Formatted: Font: Not Bold
Formatted: Font: (Default) Arial, 11pt
Formatted: Font: Not Bold, Not Italic
In addition, the entire document now has a white background on all the text, which I only noticed because I have a dark theme on office on my machine (but not the one on which I originally ran the macro). However when I run that same macro on my machine, the background remains blank instead of turning white.
What am I doing wrong here? How could the same code do such different things to the same document?
The only thing I can think of is that I'm doing the selection wrong, as this is of course not present in the original macro, but that doesn't really make any sense to me.
Many thanks!