Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loops In Word

Status
Not open for further replies.

dbaseboy

Programmer
Apr 24, 2002
48
0
0
GB
Ive just started mucking about with VBA for Word and decided to do a module that would go through a Word document and reverse all the words in that doc ie Tek-Tips becomes spiT-keT (I was bored!) I dont have the code to hand as it was at work however the basis is:

Get number of words in document, loop from 1 to that number, using Selection. get the word (and store in string WordIn) and then loop from the number of letters in the word to 1, each time taking the relevant letter (using MID) and appending it to a String called WordOut before replacing the selection with WordOut.

Everything works fine until the number of characters gets to about 500 and then it stops. Ive debugged, nothing seems out of the ordinary. I dont get an error message or anything else. Ive tried using different variable types for my counters (Single, Double, Integer) but no difference.

Any thoughts?
 
Hi dbaseboy,

And I thought I had time on my hands [smile]

When you say it stops what do you mean? Does the reversal not happen - or partially happen - or does the process stop - or what?

There is no reason you can't work with strings much longer than that in either Word or VBA. It would be better to see the code but certainly more info please (if you can remember it).

Enjoy,
Tony
 
Try....

Sub ReplaceTekTips()
Dim w As Word.Application
Dim d As Word.Document, s As Word.Selection
Set w = New Word.Application
Set d = w.Documents.Open("c:\SomeDoc.doc")
w.Visible = True 'just to watch

w.Selection.Find.ClearFormatting
w.Selection.Find.Replacement.ClearFormatting
With w.Selection.Find
.Text = "Tek-Tips"
.Replacement.Text = "spiT-keT "
.Forward = True
.Wrap = wdFindContinue
End With
w.Selection.Find.Execute Replace:=wdReplaceAll
d.Save
d.Close
Set d = Nothing
w.Quit
Set w = Nothing
End Sub
--jsteph
 
Apologies but I still forgot the email the code home! However it seems that when the code is running it sees punctuation marks as being words in their own right. I added an IF statement to say that if the word was one char in length then ignore it and not increment the counter however it now gets to the end of the doc and runs into an endless loop. Ive now sussed that the easiest thing would be to run the loop until the end of file but dont know how to check for the end.

Any thoughts?
 
dbaseboy,
I can guarantee that my suggestion will work. But if you're going with the loop method as an exercise, then there is an EndOfDoc property that you might need to check to prevent the endless loop. In my past I spent a fair amount of time writing gobs of Word vba to parse files in every-wich way, and I remember running into the endless loop, I'm fairly sure it's the endofdoc thing...
--jsteph
 
Below is the command for end of file. Good luck. When you get finished with your code, post it here...

While Not EOF(FileVariable)'while not end of file
'do something
Wend 'end while
 
Hi dbaseboy,

You are right about punctuation being treated as separate 'words' - also paragragh marks and tabs - and while you're at it look at the effects of multiple spaces.

If you ignore single character words (what about "I" and "a"?) you must still increment your loop counter otherwise you WILL enter an infinite loop because the last word in the document will be a single character paragraph mark.

Enjoy,
Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top