Need to refine simple code ending in Wend

D

Dave Neve

Hello

Helmet gave me some code as usual and I have managed to create a second
macro from the original to handle characters instead of words (one small
step for man, one gaint leap for nevekind)

The macro is a follows
_____________________________________________

Sub GOL()

' GOL Macro (Go one letter)
' Macro créée le 11/02/2006 par Dave Neve
'
Dim l As Long
Application.ScreenUpdating = False
ActiveWindow.View.ShowHiddenText = True
l = 1
With Selection.Cells(1).Range
While .Characters(l).Font.Hidden = False
l = l + 1
Wend
.Characters(l).Font.Hidden = False
End With
ActiveWindow.View.ShowHiddenText = False
Application.ScreenUpdating = True
End Sub

_____________________________________________________________________________________________

The problems comes when I reach the end of the last word but don't
necessarily know as I can't remember how many words are in the cell.

I click on the macro again and nothing happens, probably cos there is a
space character.

But the next click is 'fatal'.

There is an error that the 'collection object'(not sure of exact term in
English) can't be found and worse still, this seems to trigger Word to
activate 'show masked text' in the whole table which I then have to untick
manually.

Can anyone help please?

Thanks
 
H

Helmut Weber

Hi Dave,

the question is, what you want to do.
If you change words to characters,
then the macro would unhide the first hidden character
and exit afterwards.
Whereby it is assumed, that there is at least
one hidden character somewhere in the cell!

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
D

Dave Neve

Hi

It's hard to be clear but the macro seems to find the next character every
time I activate it and so the word is gradually revealed and this is fine.

The problem comes when I activate the macro twice more 'by accident'
thinking that there might be another word in the cell.

This causes an error which says that the 'collection object'(not sure of
exact term in
English) can't be found and worse still, this seems to trigger Word to
activate 'show masked text' in the whole table which I then have to untick
manually.

This is the error I need to stop.

Hope this is clear.

Regards

Dave Neve
 
H

Helmut Weber

Hi Dave,

one method would be to check,
whether there is hidden text at all.

Public Sub test5056()

Dim l As Long
Dim rTmp As Range
Set rTmp = Selection.Cells(1).Range
rTmp.End = rTmp.End - 1
Application.ScreenUpdating = False
ActiveWindow.View.ShowHiddenText = True
l = 1
' ------
With rTmp.Find
.Font.Hidden = True
If Not .Execute Then
ActiveWindow.View.ShowHiddenText = True
Exit Sub
End If
End With
' ------
With rTmp
While .Characters(l).Font.Hidden = False
l = l + 1
Wend
.Characters(l).Font.Hidden = False
End With
ActiveWindow.View.ShowHiddenText = False
Application.ScreenUpdating = True
End Sub

As Tony told us in the previous thread,
a range has a property TextRetrievalMode,
which could be useful, too, if you prefer
a solution without using
ActiveWindow.View.ShowHiddenText.

Which I had used anyway,
if I had known about it.


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
H

Helmut Weber

Hmm..., one of these days,

If Not .Execute Then
ActiveWindow.View.ShowHiddenText = false
' which is what you want, probably
Exit Sub
End If

HW
 
D

Dave Neve

Hi

I took out

rTmp.End = rTmp.End - 1

which made the macro jump a line down to reveal the wrong words (one cell
down) and I even managed to 'convert' your macro again to get it to work on
'words' as well as characters.

Unfortunately, I have to admit that I can't really follow the logic of this
macro with all its true and falses, hide and show and the conditions set
inside one another.

But thanks a million

Dave Neve
 
H

Helmut Weber

Hi Dave,

1st:
create a range and set it to the cell
2nd:
move the range's end one character to the left
so that the end-of-cell mark is not included,
which is always a good idea for a couple of reasons
3rd:
show hidden text
4th:
check whether there is hidden text at all
search in the range for hidden text
if not .execute means:
if no hidden text was found
then hide hidden text and exit

if hidden text was found then

5th
set up a counter and check character by character
as long as the character with the number of the
counter is not hidden.
If checking character by character finds a hidden
character, then the loop ends.
6th: The font of the actual character is set to not hidden

end

FAR BETTER would be to use TextRetrievalMode ("thanks Tony")

Use a sample paragraph and forget about cells first:

The quick brown fox jumps over the lazy dog¶

Sub HideEverySecondWord()
Dim rTmp As Range
Dim lCnt As Long
Set rTmp = Selection.Paragraphs(1).Range
lCnt = 0
With rTmp
.TextRetrievalMode.IncludeHiddenText = True
For lCnt = 2 To .Words.Count Step 2
.Words(lCnt).Font.Hidden = True
Next
End With
End Sub
' -----------------------------------------
Sub ShowFirstHiddenWord()
Dim rTmp As Range
Set rTmp = Selection.Paragraphs(1).Range
With rTmp
.TextRetrievalMode.IncludeHiddenText = True
With .Find
.Font.Hidden = True
.MatchWholeWord = True
If .Execute Then
rTmp.Words(1).Font.Hidden = False
End If
End With
End With

The difficulty seems to be,
that there are so many ways to do it.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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