Word macro to remove formatting behaves differently when implemented as VB Script

SelAromDotNet 1 Reputation point
2023-03-24T18:01:25.7566667+00:00

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!

Word
Word
A family of Microsoft word processing software products for creating web, email, and print documents.
660 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,569 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,488 questions
{count} votes