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
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0
... 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
 
Upvote 0
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.
 
Upvote 0
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...
 
Upvote 0
You are welcome.
Hope reinstalling will fix that problem, good luck! :)
 
Upvote 0

Forum statistics

Threads
1,223,366
Messages
6,171,659
Members
452,416
Latest member
johnog

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top