PDA

View Full Version : Word "Do loop" knowledge chasm!!!!



M-Unit
01-30-2008, 11:30 AM
Hello!
I've just spent 6 hours trying to make a Macro work, and what I can't get it to do is loop.
I'm really hoping somebody can help me ? VBA is not my forte and the life is dribbling out of me...

What I have is a big MS Word text document for editing.
Each paragraph is tabbed.
I need to remove the tab and apply formatting to the paragraph following the tab (first line indent & double line height).

I started by using the Macro Recorder, and this works:

Sub ReplaceTab()
'
' ReplaceTab Macro
' Macro recorded 30/1/08
'
Selection.WholeStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "^t"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.TypeBackspace
With Selection.ParagraphFormat
.LeftIndent = CentimetersToPoints(0)
.RightIndent = CentimetersToPoints(0)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 6
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
.Alignment = wdAlignParagraphJustify
.WidowControl = True
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = False
.NoLineNumber = False
.Hyphenation = True
.FirstLineIndent = CentimetersToPoints(1.27)
.OutlineLevel = wdOutlineLevelBodyText
End With
End Sub
But in order to insert a loop step (apart from actually needing to know how!) I have to edit the recorded script, and this is where I've fallen apart.

I could insert about five failed attempts to incorporate DO variants, but they're all embarrassing failures...

If you can help you will be giving somebody (ie Me) the gift of life (returned) and hopefully in the long term a reduction in RSI !!!

Many thanks in advance.

MU

Oorang
01-30-2008, 11:52 AM
rofl Ok I have to respond just because you used the phrase "Knowledge Chasm". Lets start with syntax. Take your code, put a "Do" on top, and a "Loop" on the bottom. Viola! Loop.
Do
'My Code Here
Loop Obviously this will cause an infinite loop but it's a loop. So now you need you an exit condition. There are 3 places to put one. You can either add a clause to the Do or Loop lines (Example: Do Until X = 5, or Loop Until X = 5) or you can get all fancy and smack one in the middle somewhere (Ex: If X = 5 Then Exit Do). Any of them will work it's a just a matter of where you want to put them. (Note you can also use the word "While" instead of "Until".)

So to make you code loop is fairly easy, the question is when do we want it to stop? I'm guessing it should stop when you have no tabs found. So here is how to do it. Selection.Find.Execute is actually a Function, not a Sub. Being a function it returns a value after executing. In this case it returns true if something was found, and false if something was not found. So we can change the line to this:
Do
'Some of my code here
If Not Selection.Find.Execute Then Exit Do
'Some more of my code here
Loop And that should do the trick.


Another approach would be to forgo the loop and do this instead:
Public Sub ReplaceTab()
Word.ActiveDocument.Content.Find.Execute FindText:=vbTab, _
ReplaceWith:=vbNullString, Replace:=wdReplaceAll
End Sub

M-Unit
01-30-2008, 12:28 PM
Many, many thanks for your speedy help!
Putting the three steps in seems to work on my little test file, now for a big one!
I'd love to think I can get the 6 hours back, but I'll just have to settle for something that works instead!!!

MU