VBA To Delete Hidden Text in Word

scott123

New Member
Joined
Feb 28, 2005
Messages
13
I have a food shopping list- one item per line. Some lines are formatted as hidden (but are still visible), some aren't. I'm trying to output the non hidden lines to a new document. The problem with my existing code is that it works about 50% of the time. The other 50%, it erases the non hidden text. I'm working in Word 2003.

VBA Code:
Sub ExportDisplay()
'
' ExportDisplay Macro
' Macro recorded 8/18/2020 by user
'
    Selection.Find.ClearFormatting
    Selection.Find.Font.Hidden = True
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
     End With
     Selection.Find.Execute Replace:=wdReplaceAll
     ActiveDocument.SaveAs FileName:="D:\food shopping.doc"
End Sub
 

Some videos you may like

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

ZVI

MrExcel MVP
Joined
Apr 9, 2008
Messages
3,790
Office Version
  1. 2016
  2. 2010
  3. 2007
Platform
  1. Windows
Hi, try this:
VBA Code:
Sub ExportDisplay()
    With ActiveDocument.Content.Find
      .ClearFormatting
      .Font.Hidden = True
      .Replacement.ClearFormatting
      .Text = "^?"
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindContinue
      .Format = True
      .Execute Replace:=wdReplaceAll
    End With
    ActiveDocument.SaveAs FileName:="D:\food shopping.doc"
End Sub
 

scott123

New Member
Joined
Feb 28, 2005
Messages
13
Hi, try this:

Thank you for your reply. I gave that a shot and still no luck. When the new document is saved, I can hit control-Z and undo the changes- so I can just keep alternating between control-Z and the key to trigger the macro- no other key presses or mouse movements. For about five times straight it erases the hidden text, and, then, for about 5 times, it erases the unhidden text. Very odd.
 

ZVI

MrExcel MVP
Joined
Apr 9, 2008
Messages
3,790
Office Version
  1. 2016
  2. 2010
  3. 2007
Platform
  1. Windows
To work with hidden text the ActiveDocument.ActiveWindow.View.ShowHiddenText should be True.
Checking of the hidden font is added to the code:
VBA Code:
Sub ExportDisplay()

  Dim IsHidden As Boolean
  
  Application.ScreenUpdating = False
  IsHidden = ActiveDocument.ActiveWindow.View.ShowHiddenText
  ActiveDocument.ActiveWindow.View.ShowHiddenText = True
  
  On Error GoTo exit_
  
  With ActiveDocument.Content.Font
    If Not (.Hidden = wdUndefined Or .Hidden = True) Then
      MsgBox "No hidden text in the document"
      GoTo exit_
    End If
  End With
    
  With ActiveDocument.Content.Find
    .ClearFormatting
    .Font.Hidden = True
    .Replacement.ClearFormatting
    .Text = "^?"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .Execute Replace:=wdReplaceAll
  End With
  
  ActiveDocument.SaveAs FileName:="D:\food shopping.doc"
  
exit_:
  
  ActiveDocument.ActiveWindow.View.ShowHiddenText = IsHidden
  Application.ScreenUpdating = True
  
  If Err Then MsgBox Err.Description, vbCritical, "Error!"
  
End Sub
It works on my PC on Word 2003 and 2016
 

scott123

New Member
Joined
Feb 28, 2005
Messages
13

ADVERTISEMENT

To work with hidden text the ActiveDocument.ActiveWindow.View.ShowHiddenText should be True.

Sorry for the dumb question, but, if I'm looking at the screen, and it's in Normal mode, and the hidden text is visible, shouldn't

ActiveDocument.ActiveWindow.View.ShowHiddenText

already be 'True?'

The good news is that this most recent code doesn't flip back and forth with what it removes. The bad news is that it deletes the text with the non-hidden font attribute. I need it to delete the text with the hidden attribute- and I don't know how to achieve that.

.Font.Hidden = True and .Font.Hidden = False both seem to do the same thing.
 

ZVI

MrExcel MVP
Joined
Apr 9, 2008
Messages
3,790
Office Version
  1. 2016
  2. 2010
  3. 2007
Platform
  1. Windows
... if I'm looking at the screen, and it's in Normal mode, and the hidden text is visible, shouldn't
ActiveDocument.ActiveWindow.View.ShowHiddenText
already be 'True?'
Yes, ShowHiddenText is already True in your case (set via Tools-Options in Excel 2003).

Not sure why the not hidden text is deleted on your side.
Please repeat my testing in new document according to the below description:
1. Create new document
2. Type 4 lines of text
3. Select the 2nd and 4d lines (paragraphs) and set the hidden font for the selection
4. Run the code - hidden lines have to be deleted.
5. Run the code ones more - message "No hidden text in the document" should appear
6. Hit Clrl-Z to restore the deleted hidden lines
7. Repeat steps 4-6
 

scott123

New Member
Joined
Feb 28, 2005
Messages
13

ADVERTISEMENT

Yes, ShowHiddenText is already True in your case (set via Tools-Options in Excel 2003).

I ran this test on two machines. One's the install where I need this VBA to run, and the other is a vanilla 2003 installation. On the production install, the not hidden text was deleted, but, on the vanilla install, the hidden text was deleted. The thing is that the production setup is very vanilla. I modified the margins in Normal. dot, I have a custom dictionary and I have a few setting changes to options. I also installed the compatability executable for higher file versions of word. Would a lower macro security level cause this macro to reverse it's behavior?

It's been a long time since I installed the production Word, but I think there's a good chance it's seen updates, while the vanilla version that I just installed has not been updated.

This is so incredibly odd. If I absolutely have to re-install word on my production box, I can live with that, but I'd love to figure out what's causing this.
 

ZVI

MrExcel MVP
Joined
Apr 9, 2008
Messages
3,790
Office Version
  1. 2016
  2. 2010
  3. 2007
Platform
  1. Windows
Would a lower macro security level cause this macro to reverse it's behavior?
For me, macro security might not be the culprit in this case.
No ideas in the real reason for this strange behavior...
 

ZVI

MrExcel MVP
Joined
Apr 9, 2008
Messages
3,790
Office Version
  1. 2016
  2. 2010
  3. 2007
Platform
  1. Windows
You are welcome.
Hope reinstalling will fix that problem, good luck! :)
 

Watch MrExcel Video

Forum statistics

Threads
1,118,120
Messages
5,570,296
Members
412,318
Latest member
angoeyuan
Top