[mso] Re: Word [2000]: Macro to reverse text L-R

  • From: "Jim Pettit" <j_e_pettit@xxxxxxxxxxx>
  • To: <mso@xxxxxxxxxxxxx>
  • Date: Thu, 10 Jun 2004 06:16:31 -0700

Andrew--

You seem to be mostly on track.

VBA 6.0 -- which is the version used by Office 2000 -- contains an
intrinsic (built-in) function called StrReverse, which does what your
function does, but quicker, and can be used without declaration. Just
call it where needed.

Here's a *very* simple call as you'd use it:

    Dim sForward As String
    Dim sReversed As String
    
    sForward = "The effect I want is that every line is reversed"
    sReversed = StrReverse(sForward)

When run, sReversed would be "desrever si enil yreve taht si tnaw I
tceffe ehT", the desired result.

You don't necessarily need to feed each line one at a time; you can pass
it entire paragraphs, or pages, or even a whole document. It all depends
on what effect you'd like to achieve.

NOTE: If you're using VBA 5.0 or lower, you would obviously need to use
something like your function. If so, place it in a module, and do indeed
declare it Public in scope so you can use it throughout your
application. It would be use the same way as the newer intrinsic
function. The function you showed will work, but just for fun, here's
the version I used to use:

Public Function StrReverse(Expression As String) As String
    Dim i As Integer, j As Integer, S As String
    S = Expression
    i = 1
    For j = Len(S) To 1 Step -1
        Mid$(S, i, 1) = Mid$(Expression, j, 1)
        i = i + 1
    Next j
    StrReverse = S
End Function

Hope this helps...

--Jim

-----Original Message-----
From: mso-bounce@xxxxxxxxxxxxx [mailto:mso-bounce@xxxxxxxxxxxxx] On
Behalf Of Andrew Kendon
Sent: Thursday, June 10, 2004 12:45 AM
To: Mso@Freelists. Org
Subject: [mso] Word [2000]: Macro to reverse text L-R



Could some VBA-savvy person please help me with a macro to reverse text
(Left-to-Right, character by character on a Line basis) in Word.

The effect I want is that every line is reversed
left to right, as in this example:

desrever si enil yreve taht si tnaw I tceffe ehT
:elpmaxe siht ni sa ,thgir ot tfel

I found a code snippet and I understand how it works, but I haven't the
first idea how to use it in a Sub (if that's what has to be done) to
work the way I want.  The snippet is:

Public Function ReverseString(ByVal InputString As String) _
  As String

Dim lLen As Long, lCtr As Long
Dim sChar As String
Dim sAns As String

lLen = Len(InputString)
For lCtr = lLen To 1 Step -1
    sChar = Mid(InputString, lCtr, 1)
    sAns = sAns & sChar
Next

ReverseString = sAns

End Function

I suppose that what I need the sub to do is:
Select each line in turn until the end of the document
Feed it as a parameter to ReverseString
Replace the line with ReverseString (which now contains the reversed
text) Loop

(Once upon a time I used to know how to write pseudo-code). I could
probably jog the brain cells to produce something that works on a text
file in Basic or Pascal but I never kept up with the new commands
required by Windows or Word, so, though I know what ReverseString does
as a function, I am at a loss to wrap it up so it does anything useful!
I'm not even sure whether it needs to be declared Public.

TIA

Andrew Kendon
(IT Trainer)
----------------------------------
Skills Training UK Ltd
Cherry Hinton Hall Learning Centre
Cherry Hinton Road
CAMBRIDGE CB1 8DW
----------------------------------
Tel.: 01223 458257
Mobile: 07815 301113
----------------------------------

*************************************************************
You are receiving this mail because you subscribed to mso@xxxxxxxxxxxxx
or MicrosoftOffice@xxxxxxxxxxxxxxxx

To send mail to the group, simply address it to mso@xxxxxxxxxxxxx

To Unsubscribe from this group, send an email to 
mso-request@xxxxxxxxxxxxx with the word "unsubscribe" (without the
quotes) in the subject line.

Or, visit the group's homepage and use the dropdown menu.  This will
also allow you to change your email settings to digest or vacation (no
mail). //www.freelists.org/webpage/mso

To be able to use the files section for sharing files with the group,
send a request to mso-moderators@xxxxxxxxxxxxx and you will be sent an
invitation with instructions.  Once you are a member of the files group,
you can go here to upload/download files:
http://www.smartgroups.com/vault/msofiles
*************************************************************
*************************************************************
You are receiving this mail because you subscribed to mso@xxxxxxxxxxxxx or 
MicrosoftOffice@xxxxxxxxxxxxxxxx

To send mail to the group, simply address it to mso@xxxxxxxxxxxxx

To Unsubscribe from this group, send an email to 
mso-request@xxxxxxxxxxxxx with the word "unsubscribe" (without the quotes) in 
the subject line.

Or, visit the group's homepage and use the dropdown menu.  This will also allow 
you to change your email settings to digest or vacation (no mail).
//www.freelists.org/webpage/mso

To be able to use the files section for sharing files with the group, send a 
request to mso-moderators@xxxxxxxxxxxxx and you will be sent an invitation with 
instructions.  Once you are a member of the files group, you can go here to 
upload/download files:
http://www.smartgroups.com/vault/msofiles
*************************************************************

Other related posts: