![]() |
|
#1
|
|||
|
|||
|
Hi !
(this is my 1st message ever on this forum !) I'm trying to do a simple thing, but I' missing something : I want to add an "s" at the end of the current word. I wrote a macro to do this : Code:
Sub Pluriel()
' ALT + S
Application.Selection.Words(1).Select
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="s"
End Sub
For instance : assiette >>> assiettes = fine (provided the word is in the middle of a sentence) but : assiette, >>> assiettse, = wrong How should I proceed to avoid this ? Many thanks ! |
|
#2
|
||||
|
||||
|
I think the following should do it. As you have discovered, what you might think of as a word and what VBA thinks is a word do not necessarily coincide.
Code:
Sub Pluriel()
Dim oRng As Range
Dim lngCase As Long
Set oRng = Selection.Words(1)
lngCase = oRng.Case
oRng.MoveEndWhile Chr(32) & Chr(160), wdBackward
oRng.Collapse 0
If lngCase = 1 Then
oRng.Text = "S"
Else
oRng.Text = "s"
End If
lbl_Exit:
Set oRng = Nothing
Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
#3
|
|||
|
|||
|
Many thanks, that's perfect !
(you even went beneath my expectations while managing the case) Brilliant ! And now, I have to figure out how the code works.....
|
|
#4
|
||||
|
||||
|
It is straightforward and relies on the use of a range
Code:
Sub Pluriel()
'Declare the variables used
Dim oRng As Range
Dim lngCase As Long
'set a range to the word the cursor is in
Set oRng = Selection.Words(1)
'establish the case of the word
lngCase = oRng.Case
'if the word ends in a space or non-breaking space remove that space from the range
oRng.MoveEndWhile Chr(32) & Chr(160), wdBackward
'Collapse the range to its end
oRng.Collapse 0
If lngCase = 1 Then 'its upper case
oRng.Text = "S" ' write S to the range
Else 'its not upper case
oRng.Text = "s" 'write s to the range
End If
lbl_Exit: 'set a marker - not essential
Set oRng = Nothing 'clear the range
Exit Sub 'and finish
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
#5
|
|||
|
|||
|
Thanks : very kind of you !!!
|
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Problem getting filename and directory of current word doc.
|
gazeranco | Word VBA | 8 | 10-29-2016 12:13 AM |
Where can I see the current hidden word properties values in Word 2007?
|
pstein | Word | 3 | 08-15-2012 05:08 AM |
How to call current PC date and/or current PC year
|
KIM SOLIS | Excel | 2 | 11-04-2011 06:09 PM |
What “style” is used by the current line. Word 2007.
|
persist | Word | 2 | 04-07-2010 09:52 PM |
Auto insert current month's name and current year
|
Styler001 | Word | 4 | 01-25-2010 06:40 PM |