Hiding text from a macro

J

Jonathan Sachs

I've written a macro which visits each paragraph in a table cell and
hides it or unhides it depending on its style. The macro works
correctly, but is very slow. Each iteration of the per-paragraph loop
appears to take a large fraction of the second, and the fractions add
up.

I'm trying to be rewrite the macro to eliminate the per-paragraph
loop. One step in the rewritten macro is to hide or unhide the entire
text of the cell. It isn't working. I can set the text's Font.Hidden
property, but it has no effect. That is, VBA neither changes the
text's Hidden property nor changes the contents of the window. (Nor
does it gives me an error.)

Here's an extract of the code:

Dim msgRange As Range
' . . .
Set msgRange = Selection.Cells(1).Range ' Select the cell.
msgRange.MoveLeft unit:=wdcharacter,count:=1,extend:=wdextend '
Just the text in the cell.
msgRange.Font.Hidden = True ' Hide
everything.

I tried hiding the text by hand (which works) and recording the
operation in a macro. The macro does exactly the same thing I do,
except that it operates on Selection instead of a Range variable. I
tried hiding Selection.Range in the immediate window (executing the
exact statement from the recorded macro) and THAT has no effect.

Does anybody know what Word is doing to me, and how to work around it?

My mail address is jsachs177 at earthlink dot net.
 
D

Denise Z

I've noticed that Word XP doesn't have a lot of the same
really good VB help that it did in 97. I found the
following way back when in Word 97 VBA help. It iterates
through all of the cells in a table in record time. The
only additional code you need is to check for the style at
the [your style code] location below.

Dim oTable As Object, oCell As Object

'Substitute the appropriate table number (i.e., Table 2 =
Tables(2).Range... in next statement

Set oTable = ActiveDocument.Range.Tables(1)

'For each cell in the table, if text is the ____ style,
turn on hidden text

For Each oCell In oTable.Range.Cells
oCell.Select
'insert your code to check the style - don't bother with
your msgRange stuff
If [your style code] = True then
Selection.Font.Hidden = True
End if
Next oCell

This really should do the trick for you. Good luck.

Denise
-----Original Message-----
I've written a macro which visits each paragraph in a table cell and
hides it or unhides it depending on its style. The macro works
correctly, but is very slow. Each iteration of the per- paragraph loop
appears to take a large fraction of the second, and the fractions add
up.

I'm trying to be rewrite the macro to eliminate the per- paragraph
loop. One step in the rewritten macro is to hide or unhide the entire
text of the cell. It isn't working. I can set the text's Font.Hidden
property, but it has no effect. That is, VBA neither changes the
text's Hidden property nor changes the contents of the window. (Nor
does it gives me an error.)

Here's an extract of the code:

Dim msgRange As Range
' . . .
Set msgRange = Selection.Cells(1).Range ' Select the cell.
msgRange.MoveLeft
unit:=wdcharacter,count:=1,extend:=wdextend '
 
D

Doug Robbins - Word MVP

Hi Jonathon,

Use the following

Dim i As Integer, j As Integer, k As Integer, l As Integer
For i = 1 To ActiveDocument.Tables.Count
For j = 1 To ActiveDocument.Tables(i).Rows.Count
For k = 1 To ActiveDocument.Tables(i).Columns.Count
For l = 1 To ActiveDocument.Tables(i).Cell(j,
k).Range.Paragraphs.Count
If ActiveDocument.Tables(i).Cell(j,
k).Range.Paragraphs(l).Style = "your style" Then
ActiveDocument.Tables(i).Cell(j,
k).Range.Paragraphs(l).Range.Font.Hidden = True
End If
Next l
Next k
Next j
Next i

Avoid using the selection like the plaque!

If you want to act upon just the text in a cell.

use:

Set myrange = ActiveDocument.Tables(n).Cell(rownum, colnum).Range
myrange.End = myrange.End - 1

myrange will then just contain the text.

Please respond to the newsgroups for the benefit of others who may be
interested.

Hope this helps
Doug Robbins - Word MVP
 
J

Jonathan Sachs

Doug, thanks for your quick response. Please forgive me if I question
you before trying that out; carpal tunnel syndrome severely limits my
coding ability, and I want to be sure there isn't a misunderstanding
before I pursue your suggestion.

The code I'm trying to replace (which I didn't describe before) is not
too different from what you wrote, except that I use the "For Each x
in y.Range.Paragraphs" construct instead of treating the collection of
paragraphs as an array. (I only have to process one cell at a time,
which eliminates the nested loops.) It appears to me that the delays
which are troubling me are in the statement which sets Hidden
attribute, not in the loop construct. That suggests to me that if I
follow your instructions, the new code will have exactly the same
problem as the old code did. Do I misunderstand?

Denise, I'm puzzled by your suggestion, because the operative part of
it appears to be exactly the same as my example, except for using
Selection instead of a Range variable. Perhaps you misunderstood the
problem, which is: how to either eliminate the slowness of the
"x.Hidden = True" operation, or set the Hidden attribute for all of
the text in a cell in one step, eliminating the need for the loop.

My mail address is jsachs177 at earthlink dot net.
 
D

Doug Robbins - Word MVP

Hi Jonathon,

Running my macro on a 25 x 3 table, each cell of which contained two
paragraphs took about 4 seconds.

If the Style is not used anywhere else in the document, then

ActiveDocument.Styles("your style").Font.Hidden = True

is virtually instantaneous.

Running Denise's code on the same table, took about 8 seconds.

Please respond to the newsgroups for the benefit of others who may be
interested.

Hope this helps
Doug Robbins - Word MVP
 
K

Klaus Linke

I've written a macro which visits each paragraph in a table cell
and hides it or unhides it depending on its style. The macro works
correctly, but is very slow.


Hi Jonathan,

Have you tried to select the table and use "Find/Replace"?

Should be much faster, than working on each table cell, I think.

Regards,
Klaus
 
J

Jonathan Sachs

Klaus Linke said:
Have you tried to select the table and use "Find/Replace"?

Should be much faster, than working on each table cell, I think.

There seems to be a lot of misunderstanding about what I'm trying to
do. Let me give the entire context.

I have a document which contains many tables, each representing the
history of one auction I have conducted. Each row in a table
represents one email message sent or received. One of the cells in
each row contains (a) the entire text of the message, and (b) a
one-line summary of the message. Text and summary are distinguished by
different paragraph styles.

Normally each message's summary is unhidden and its text paragraphs
are hidden. This makes the table very compact and allows me to
identify any message of interest. To display the contents of a
message, I run a macro which hides its summary and unhides its text.
When I am done with it, I run another macro which does the reverse.

I designed these macros to hide (or unhide) the entire contents of
cell, then unhide (or hide) the summary. That would have been almost
instantaneous, which, given the macro's purpose, it needs to be. But
for some reason I do not understand, trying to set the Hidden
attribute for the entire text of the cell has no effect when done from
a macro, although it works fine when done from the keyboard. (In case
it matters, I am using Word 2000.)

Now the macros iterate through the cell and hide or unhide each
individual paragraph. This becomes very slow when the message is long.
The 101-paragraph example which I mentioned is unusual, but real.
Messages with a couple of dozen paragraphs are common.

I'm trying to find a way to make the macros run much faster. I think
it would be most productive to focus on how to set the Hidden
attribute for all of the text in the cell.

My mail address is jsachs177 at earthlink dot net.
 
J

Jay Freedman

Hi, Jonathan,

Since you have the text and the summary in different styles, assuming those
styles are applied consistently and not applied to anything else in the
document, you can flip almost instantly just by changing the .Hidden property in
the styles. These two macros behave as expected for me (in Word 2003 beta),
using styles named Summary and Message:

Sub ShowMessages()
With ActiveDocument
.Styles("Message").Font.Hidden = False
.Styles("Summary").Font.Hidden = True
End With
End Sub

Sub ShowSummaries()
With ActiveDocument
.Styles("Message").Font.Hidden = True
.Styles("Summary").Font.Hidden = False
End With
End Sub

If you want a single macro so you can have a toggle button on a toolbar, it
would look like this:

Sub ToggleMessages()
With ActiveDocument
If .Styles("Summary").Font.Hidden = False Then
.Styles("Message").Font.Hidden = False
.Styles("Summary").Font.Hidden = True
Else
.Styles("Message").Font.Hidden = True
.Styles("Summary").Font.Hidden = False
End If
End With
End Sub
 
K

Klaus Linke

trying to set the Hidden attribute for the entire text of the
cell has no effect when done from a macro, although it
works fine when done from the keyboard. (In case
it matters, I am using Word 2000.)

Perhaps if you post the piece of code that's supposed to do that, we can
figure out what is wrong, or suggest alternatives?

Klaus
 
J

Jonathan Sachs

I'm reluctant to post the code because it is actually more elaborate
than the process I've described, for reasons that have nothing to do
with the problem at hand. I'm concerned that posting it would increase
the confusion rather than dispel it. I mentioned that carpal tunnel
syndrome drastically limits my ability to write code, and that makes
it impractical for me to develop a simplified version for posting.

The "For Each atable..." in your example indicates that you still do
not understand what I'm trying to do. Would you please read my
preceding message again? I'm sorry, I don't see how I could explain it
more clearly.

Let me clarify that I had NOT been able to use a macro to set the
Hidden attribute for the entire text of a cell without iterating
through the paragraphs. I'm sorry if anything I wrote gave you the
impression that I had.

Now, I have done some additional experimentation and have made some
new observations. When I tried to set the Hidden attribute of multiple
paragraphs in a cell, and all of them have the Normal style, it works
as one would expect.

But my actual text uses a mixture of two defined styles, one with
Hidden=True attribute and one without. When I try to do it to that
type of text, strangeness happens. Sometimes the operation affects all
of the paragraphs; sometimes some but not all; sometimes none. I have
not yet figured out the rules, if any, which govern this. And if I
start with the entire cell selected (as distinguished from the entire
text within the cell), I end up with the entire row selected!

My mail address is jsachs177 at earthlink dot net.
 
J

Jonathan Sachs

Jay Freedman said:
Since you have the text and the summary in different styles, assuming those
styles are applied consistently and not applied to anything else in the
document, you can flip almost instantly just by changing the .Hidden property in
the styles....

That will change the visibility of every entry in the document! I am
trying to change the visibility of the text in one cell.

My mail address is jsachs177 at earthlink dot net.
 
K

Klaus Linke

Hi Jonathan,

The following 2 lines of code should hide/unhide the text in the
first/second cell of the row you are in:

Selection.Rows(1).Cells(1).Range.Font.Hidden = wdToggle
Selection.Rows(1).Cells(2).Range.Font.Hidden = wdToggle

In the code you posted originally, you used MoveLeft on a Range, which
cannot work and would give you an error.
The step to exclude the end-of-cell-marker was unnecessary, anyway.

Klaus
 
D

Doug Robbins - Word MVP

Hi Jonathon,

What I read is:

There seems to be a lot of misunderstanding about what I'm trying to
do. Let me give the entire context.

I have a document which contains many tables,

That is why is suggested using "For Each atable"

If you just need to operate on the table in which the selection is located,
then all you would need would be:

For Each acell In Selection.Tables(1).Columns(3).Cells
acell.Range.Font.Hidden = True
Next acell

or, if it was just a particular cell in the row in which the selection is
located, then

Selection.Rows(1).Cells(3).Range.Font.Hidden = True

You did not give me the impression that you had been able to use a macro to
set the
Hidden attribute for the entire text of a cell without iterating through the
paragraphs. I was attempting to advise you that it was possible to do that
when you use the .Range of the cell as the object upon which you want to
perform the operation.

When using the .Range object, it is not necessay to select everything that
is located in that range to perform some action on it. All that you have to
do is specify the .Range. When you use Selection.[item].Range as in the
examples above, the .Range will encompass the [item] in which the selection
is located. In the case of

Selection.Rows(1).Cells(3).Range.Font.Hidden = True

if the selection is in column 1, the text in third column of the row would
be hidden, even though it is not selected.

The following taken from your last post:

And if I start with the entire cell selected (as distinguished from the
entire
text within the cell), I end up with the entire row selected!

indicates that you are selecting the everything on which you want to perform
the action. As mentioned above, that is not necessary if you use the .Range
object.

For that reason, it can often be a lot easier to help if we can see the code
that you are using.

Possibly Klaus's suggestion to toggle the .Hidden property will overcome
your problem. If not, I think that you will have to copy and paste the code
into a message so that we can see it.

Please respond to the newsgroups for the benefit of others who may be
interested.

Hope this helps
Doug Robbins - Word MVP
 

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