Is this possible?

J

Jenny

Hi. I am giving my students a type of cloze test online
(a test where certain words are blanked), and I am
blanking out certain words one by one, which takes a lot
of time. Could a macro be created that could do the
following? When I click (or double-click) my mouse on a
certain word, all letters except the first and the last
are blanked. For example, let's say the
word "appropriate" in the following sentence should be
blanked.

His behavior was appropriate.

The result after clicking the mouse on the
word "appropriate" would be:

His behavior was a__________e.

How could such a macro be created?
 
J

Jezebel

Dim pWord As String

pWord = Selection.Words(1)
Selection.Words(1) = Left(pWord, 1) & String(Len(pWord) - 2, "_") &
Right(pWord, 1)
 
J

Jenny

Hi. I tried this but the macro only deletes the first
letter of the word, and not the last. Am I doing
something wrong?
 
J

Jonathan West

Jenny said:
Hi. I tried this but the macro only deletes the first
letter of the word, and not the last. Am I doing
something wrong?

No, you haven't done anything wrong. unfortunately jezebel didn't quite get
the code completely right. It didn't allow for the fact that
Selection.Words(1) as defined in VBA can include trailing spaces.

Try this instead

Sub BlankWord()
Dim rngWord As Range

Set rngWord = Selection.Words(1)
Do Until Asc(Right(rngWord, 1)) > 32
rngWord.MoveEnd Unit:=wdCharacter, Count:=-1
If rngWord.End <= rngWord.Start Then Exit Sub
Loop
rngWord.Text = Left(rngWord.Text, 1) & _
String(Len(rngWord.Text) - 2, "_") & Right(rngWord.Text, 1)

End Sub
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Jenny > écrivait :
In this message, < Jenny > wrote:

|| Hi. I tried this but the macro only deletes the first
|| letter of the word, and not the last. Am I doing
|| something wrong?
||

This is because a word (according to Word) includes the following spaces, if
any.
Try this modified code:

'_______________________________________
Dim pWord As String
Dim TrimmedpWord As String
Dim WordRange As Range

With Selection
pWord = .Words(1)
TrimmedpWord = Trim(pWord)
Set WordRange = .Words(1)
End With

'If true then selecteted word is followed by a space
If Len(TrimmedpWord) < Len(pWord) Then
WordRange.MoveEnd wdCharacter, -1
End If

WordRange.Text = Left(TrimmedpWord, 1) & _
String(Len(TrimmedpWord) - 2, "_") _
& Right(TrimmedpWord, 1)
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jonathan West

Jean-Guy Marcil said:
Bonjour,

Dans son message, < Jenny > écrivait :
In this message, < Jenny > wrote:

|| Hi. I tried this but the macro only deletes the first
|| letter of the word, and not the last. Am I doing
|| something wrong?
||

This is because a word (according to Word) includes the following spaces, if
any.

Snap! :)
Try this modified code:

Different way of doing the same thing <g>
 
G

Gurnam Singh

-----Original Message-----
Message unavailable
I am creating a very simple macro and want to save it for
future use. I can use the macro as long as I have not
turned off my computer. Once I have turned off my
computer the macro disappers. What am I not doing right?
Any suggestions?

Thanks,
Gurnam
 
J

Jonathan West

Unless you make a specific effort to save your macro elsewhere, it is almost
certainly placed in the normal.dot template. If this template is not saved
when you close Word then it is lost.

It is possible that if you are using Word at work, then the system
administrator has set normal.dot to be read-only. Speak to him or her about
this.
 

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