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

J

jbraden

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!
 
H

Helmut Weber

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.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

jbraden

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.

-
 
H

Helmut Weber

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...

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Greg

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
 
T

Tony Jollans

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
 

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