Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2005

Tip: Looking for answers? Try searching our database.

need help writing a macro to insert a slash in between words

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jbraden - 03 Oct 2005 15:48 GMT
I need to write a macro (or somehow otherwise automate this task.) I need to  
insert a slash mark after every twentieth word of continuous text.

In other words, in a paragraph of text, a "/" would be inserted in between
words 20 and 21, between words 40 and 41, between words 60 and 61, etc and so
forth.

I appreciate any assistance!
Helmut Weber - 03 Oct 2005 18:41 GMT
Hi,

as easy as that, to get you going,
yet impossible in a way:

Sub Test12345()
Dim c As Long
Dim rDoc As Range
Set rDoc = ActiveDocument.Range
For c = 20 To rDoc.Words.Count Step 20
  rDoc.Words(c).InsertAfter "/"
Next
End Sub

Impossible, because in natural language,
the meaning of "word", like everything, is fuzzy,
always more or less, and disputable.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

jbraden - 04 Oct 2005 16:46 GMT
Helmut, Thank you so much. This gets me very close to what I need.
Unfortunately it seems to be counting punctuation as words. For instance I
ran the macro on the following piece of literature and got:

DURING the whole of a dull, dark, and soundless day in the autumn of the
year, when / the clouds hung oppressively low in the heavens, I had been
passing alone, on horseback, through / a singularly dreary tract of country;
and at length found myself, as the shades of the evening / drew on, within
view of the melancholy House of Usher. I know not how it was; / but, with the
first glimpse of the building, a sense of insufferable gloom pervaded my
spirit. / I say insufferable; for the feeling was unrelieved by any of that
half-pleasurable, because poetic/ , sentiment, with which the mind usually
receives even the sternest natural images of the desolate or terrible/ . I
looked upon the scene before me upon the mere house, and the simple landscape
features of / the domain upon the bleak walls upon the vacant eye-like
windows upon a few rank sedges and / upon a few white trunks of decayed trees
with an utter depression of soul which I can compare to / no earthly
sensation more properly than to the after-dream of the reveler upon opium the
bitter lapse / into every-day life the hideous dropping off of the veil.

Any further assistance would be GREATLY appreciated! Thank you! Was in your
beautiful Bavaria in June. It was just lovely.

Thanks again Helmut.

-

> Hi,
>
[quoted text clipped - 13 lines]
> the meaning of "word", like everything, is fuzzy,
> always more or less, and disputable.
Helmut Weber - 04 Oct 2005 17:38 GMT
Hi,

>Helmut, Thank you so much. This gets me very close to what I need.

I am really sorry to say so, getting closer would involve
a lot of tedious coding, an endless list of exceptions,
an very substantial reduction of performance,
and still only get you a bit closer.

Try this one, for gaining an impression
of what a word is to "Word".

Dim oWrd As Object
For Each oWrd In ActiveDocument.Words
  oWrd.Select
Next

Try define, what isn't a word:
#.;,-_/&%$"!=?\'<>{}[]1234567890™®±.........
Beware of O'Connor, Formula-1-Pilot, ¾-inch-pipe,
killer whale (one word), singer songwriter composer (one word),
according to what I've learned at university.

At least you'll get an impression, what "fuzzy" means.
No computer can decide between cold and worm,
early or late, fast or slow, good and bad...

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Greg - 04 Oct 2005 18:14 GMT
jbraden,

I think Helmut is correct that this could be very complex.  However,
"if" you are careful with your spacing, you might get closer to your
desired result with:

Sub ScratchMacro()
Dim oChar As Range
Dim i As Long
For Each oChar In ActiveDocument.Range.Characters
 If Asc(oChar) = 32 Then
   i = i + 1
   If i = 20 Then
     oChar.InsertAfter "/"
     i = 0
   End If
 End If
Next oChar
End Sub
Tony Jollans - 04 Oct 2005 19:15 GMT
What about doing this with Find and Replace?

Looking for strings delimited by space and/or carriage return would
approximate to natural language words.

Unfortunately, Word's pattern matcher throws up its hands in horror at what
it sees as over-complex search strings, so a little loop of repeated finds
is called for ...

   Selection.HomeKey Unit:=wdStory
   Selection.Find.ClearFormatting
   Selection.Find.Replacement.ClearFormatting
   With Selection.Find
       .Text = "[! ^13]{1,}[ ^13]{1,}[! ^13]{1,}[ ^13]{1,}"
       .Replacement.Text = "^&/ "
       .Forward = True
       .Wrap = wdFindStop
       .Format = False
       .MatchCase = False
       .MatchWholeWord = False
       .MatchAllWordForms = False
       .MatchSoundsLike = False
       .MatchWildcards = True
   End With
   Do
       For i = 1 To 9
           If Not Selection.Find.Execute Then Exit Do
       Next i
       If Not Selection.Find.Execute(Replace:=wdReplaceOne) Then Exit Do
   Loop

--
Enjoy,
Tony

> jbraden,
>
[quoted text clipped - 15 lines]
> Next oChar
> End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.