# VBA To Delete Hidden Text in Word



## scott123 (Aug 18, 2020)

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.


```
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
```


----------



## ZVI (Aug 20, 2020)

Hi, try this:

```
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 (Aug 21, 2020)

ZVI said:


> 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 (Aug 21, 2020)

To work with hidden text the ActiveDocument.ActiveWindow.View.ShowHiddenText should be True.
Checking of the hidden font is added to the 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 (Aug 21, 2020)

ZVI said:


> 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 (Aug 21, 2020)

scott123 said:


> ... 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 (Aug 22, 2020)

ZVI said:


> 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 (Aug 22, 2020)

scott123 said:


> 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...


----------



## scott123 (Aug 23, 2020)

ZVI said:


> For me, macro security might not be the culprit in this case.
> No ideas in the real reason for this strange behavior...



Thanks for you all your help. I really appreciate it.


----------



## ZVI (Aug 23, 2020)

You are welcome.
Hope reinstalling will fix that problem, good luck!


----------

