Hidden Text Style

J

Jules

Hi everyone, I'm after a VBA routine to delete style and its contents:

Hide (style name used for hidden text)

compatible Word 2003/2007.
 
J

Jules

What I have so far just deletes the style but not contents encased in style?

Sub Hide_StyleDeleteALL()
Dim ast As Style
For Each ast In ActiveDocument.Styles
If ast.NameLocal = "Hide" Then
ast.Delete
Exit For
End If
Next
End Sub
 
C

Cindy M.

Hi Jules,
I'm after a VBA routine to delete style and its contents:

Hide (style name used for hidden text)

compatible Word 2003/2007.
Delete or hide? And what do you mean with "its contents"?
You mean you want to delete the text formatted with that
style?

You can do this using Find/Replace. Find the text formatted
with the style, then replace with "Nothing". Try it out in
the Word UI, then record a macro when making the settings
to get the basic syntax.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update
Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any
follow question or reply in the newsgroup and not by e-mail
:)
 
J

Jules

Hi, I need to incorporate into a clean macro so it is not the only routine
that runs - don't want to use bookmarks either.
 
C

Cindy M.

Hi Jules,
I need to incorporate into a clean macro so it is not the only routine
that runs - don't want to use bookmarks either.
Are you answering my suggestion? I don't understand the connection...

I'm suggesting you use Word's Find/Replace functionality - in your macro
- to find the sections of text formatted with the style and delete them.
If that's what you really want to do - that wasn't clear from your
original question.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
J

Jules

Yes I'll use find/replace thank you.
Cindy M. said:
Hi Jules,

Are you answering my suggestion? I don't understand the connection...

I'm suggesting you use Word's Find/Replace functionality - in your macro
- to find the sections of text formatted with the style and delete them.
If that's what you really want to do - that wasn't clear from your
original question.


Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)


This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
S

StevenM

To: Jules,

Is the following close to what you wanted?

Sub DeleteHiddenText()
ActiveDocument.Range(0, 0).Select
ActiveDocument.ActiveWindow.View.ShowHiddenText = True
Do
With Selection.Find
.ClearFormatting
.Forward = True
.Format = True
.Font.Hidden = True
.Text = ""
.Execute
End With
If Selection.Find.Found Then
Selection.Delete
Else
ActiveDocument.ActiveWindow.View.ShowHiddenText = False
Exit Sub
End If
Loop
End Sub

'Sub FixShowHiddenText()
' ActiveDocument.ActiveWindow.View.ShowHiddenText = False
'End Sub

FixShowHiddenText is commented out and only necessary if one stops the macro
before letting it finish (which I did a number of times in order to watch how
it was working).

Steven Craig Miller
 
G

Gordon Bentley-Mix

Steven,

I think the name of the style was a bit of a red herring. As I understand
it, Jules is looking to delete all content from the document that has a
particular style applied to it - and that style just happens to be called
"Hide" in Jules' situation. Cindy is recommending using the Find object to do
this. I believe this can be accomplished with the following:

Sub DeleteMyStyleText()
With ActiveDocument.Content.Find
.ClearFormatting
.Forward = True
.Style = "MyStyle"
.Text = ""
With .Replacement
.ClearFormatting
.Text = ""
End With
.Execute Replace:=wdReplaceAll
End With
ActiveDocument.Styles("MyStyle").Delete
End Sub

This has a few advantages over your solution in that:
1) it doesn't require a loop - Word is quite happy to replace everything in
a Range in one go; and
2) it also doesn't use the Selection object so it's quicker and the cursor
doesn't jump around all over the place.

Because all we're concerned about is removing the content that has a
particular style applied to it, we don't need to worry about viewing hidden
text or even checking to see if the font is hidden when we do the find.

Finally, Jules wants to delete the style after removing any content using
that style - thus the reason for:

ActiveDocument.Styles("Hide").Delete

And of course my usual rant about not using ActiveDocument would apply if I
planned on using this in a production environment...

BTW, this wasn't just an exercise in clever coding for me. I've actually
been trying to achieve this same goal in several of my templates for some
time now. However, I've been going through the Paragraphs collection and
deleting any paragraphs with a particular style. It didn't always work, but
this looks like it should. Thanks Jules, Cindy and Steven giving me the
inspiration and impetus to work out a better solution.
--
Cheers!
Gordon
The Kiwi Koder

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
J

Jules

The text is hidden text and it is located in a style at this point called
Hide. But a user may or may not have the Hidden Options turned on.
The main thing is to delete the style Hide and its contents which happens to
be hidden text. No herrings involved thanks.
 
G

Gordon Bentley-Mix

Jules,

You're missing the point. The definition of the style and whether hidden
text is visible or not don't matter at all. It just _happens_ that your
style is called "Hide" and uses hidden font - and may or may not be visibile
- in your particular situation. This is what I meant about the style name and
definition being a red herring: Steven wrote code to accommodate hidden text
but it's not necessary. Simply finding and replacing content using a
particular style is enough - regardless of the details of the style. My code
does this and will work for your needs simply by replacing "MyStyle" with the
name of your style; e.g. "Hide". I even tested it with a style ("MyStyle")
using a hidden font and it worked perfectly - not a fish in sight.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
G

Gordon Bentley-Mix

My apologies to Jules and Steven; it does make a difference if hidden text is
visible or not. When it's not visible, all that happens is the style gets
deleted - although why this should be so makes no sense to me.

However, the solution is simple. Insert the following as the first line of
the procedure.

ActiveWindow.View.ShowHiddenText = True

Then all works as expected.

Note that no changes to the find are required - and in fact, setting
..Font.Hidden to True still doesn't make it work when hidden text isn't
visible.

(Good practice tip: If I used this in a production environment, I would
record the user's current "hidden text visibility" setting first and set it
back to what it was after I ran my procedure thusly:

Sub DeleteMyStyleText()
Dim boolCurrentHiddenStatus As Boolean
boolCurrentHiddenStatus = ActiveWindow.View.ShowHiddenText
ActiveWindow.View.ShowHiddenText = True
With ActiveDocument.Content.Find
.ClearFormatting
.Forward = True
.Style = "MyStyle"
.Text = ""
With .Replacement
.ClearFormatting
.Text = ""
End With
.Execute Replace:=wdReplaceAll
End With
ActiveDocument.Styles("MyStyle").Delete
ActiveWindow.View.ShowHiddenText = boolCurrentHiddenStatus
End Sub

I don't like to permanently change settings unless I have to - and then only
after letting the user know about it.)
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
G

Gordon Bentley-Mix

AHA! I *knew* there had to be an answer!

Set the IncludeHiddenText property of the TextRetrievalMode property of the
Range object of the ActiveDocument to True as follows:

Sub DeleteMyStyleText()
With ActiveDocument.Range
.TextRetrievalMode.IncludeHiddenText = True
With .Find
.ClearFormatting
.Forward = True
.Style = "MyStyle"
.Text = ""
With .Replacement
.ClearFormatting
.Text = ""
End With
.Execute Replace:=wdReplaceAll
End With
End With
ActiveDocument.Styles("MyStyle").Delete
End Sub

No loops. No Selection objects. No changing View options. No worries about
whether the text is hidden or not. Just deletes all content with the
specified style in one clean shot! Maybe the fact that the style uses a
hidden font isn't _exactly_ a red herring, but it can be easily accommodated
and its impact minimised without a lot of fuss.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
J

Jules

Nice Gordon thank you for being inspired to work so hard. It does leave
behind the pilcrows on end of style? Is it possible to incorporate the
pilcrow to be deleted also at same time?
 
J

Jules

Thanks Steven that works well.
StevenM said:
To: Jules,

Is the following close to what you wanted?

Sub DeleteHiddenText()
ActiveDocument.Range(0, 0).Select
ActiveDocument.ActiveWindow.View.ShowHiddenText = True
Do
With Selection.Find
.ClearFormatting
.Forward = True
.Format = True
.Font.Hidden = True
.Text = ""
.Execute
End With
If Selection.Find.Found Then
Selection.Delete
Else
ActiveDocument.ActiveWindow.View.ShowHiddenText = False
Exit Sub
End If
Loop
End Sub

'Sub FixShowHiddenText()
' ActiveDocument.ActiveWindow.View.ShowHiddenText = False
'End Sub

FixShowHiddenText is commented out and only necessary if one stops the
macro
before letting it finish (which I did a number of times in order to watch
how
it was working).

Steven Craig Miller
 
G

Gordon Bentley-Mix

Jules,

I am unable to replicate the behaviour you describe. When I run this code,
it deletes all of the paragraphs using the 'MyStyle' style - including the
pilcrows. Perhaps the problem is that the style hasn't been applied to the
whole paragraph? If you would care to email me a sample document I'd be happy
to investigate further. Otherwise, it appears to work fine, with the
exception of content in parts of the document other than the main body
(headers, footers, textboxes, etc.) of course.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
J

Jules

Village idiot moment on my part Gordon - with new CCs in 2007 one has to
include a paragraph return even if you don't want one for the BBCCs to hold
style and insert properly - hence an extra pilcrow - going a little crazy
with battle of extra pilcrows just to hold style in BBs inserted into BBCCs.
Also into abbreviating Building Block Content Controls (BBCCs) - otherwise
I'm not too loopy at all most of the time!!!

Thanks again everyone both macros fit my needs perfectly.
 
G

Gordon Bentley-Mix

No worries Jules. If I had known you were using 2007 it might have just as
easily been me in the "village idiot" roll. I'm still working with the
ancient technology of 2003 - and from everything I've heard about 2007, I'm
hoping not to have to upgrade any time soon!
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
J

Jules

It's not bad! Lots of control and setup (which I like) and so so easy for
users. Just lots of setup time. I expect you to multitask Gordon 2003/2007
(I do!). I've VBA'd my contentcontrol collection it seems MS forget to
automate title/placeholder text together so VBA is the quicker route.

You'll love it...
 
G

Gordon Bentley-Mix

Office 2003 & 2007 at the same time!?!?

~closes eyes & clicks heels together repeatedly~

There's no place like home... there's no place like home... there's no place
like home...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top