macro deletes entire doc

J

Julia

Hi, can't figure out why the following code deletes my document, one
character at a time!

It needs to simply search for the next page break, move one character to the
right, select until it hits a space and delete the selection, which happens
to be several blank paragraphs at the top of each page. It works fine
without the looping, but I just can't get it to work. Can you help???
Thanks much

With Selection
.WholeStory
.ExtendMode = False
.Collapse
End With

With Selection.Find
.ClearFormatting
.Text = "^m"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False

While .Execute

Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.Extend

With Selection.Find
.Text = " "
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop

End With
Selection.Find.Execute
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Wend

End With


End Sub
 
D

Dave Lett

Hi Julia,

When you perform your second find action, Word changes what it is looking
for with While .Execute (to your blank space). Try the following; I think it
does what you're looking for:

With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Forward = True
.Wrap = wdFindStop
Do While Selection.Find.Execute(FindText:="^m")
With Selection
.MoveRight Unit:=wdCharacter, Count:=1
.Extend
With .Find
.Text = " "
.Execute
End With
.delete
End With
Loop
End With
End With


HTH,
Dave
 
J

Julia

PERFECT!!!!!!!!!!!!! THANK YOU I LOVE YOU except one thing...it's deleting
the first character on each page too (which happens to be a space). I am
trying to fix that now...

thanks so much
 
D

Dave Lett

Hi Julia,

<snip>THANK YOU I LOVE YOU </snip>
Okay, but don't tell my fiancee. She's the jealous type :)

This probably even easier:

With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Text = "^m"
.Forward = True
.Wrap = wdFindStop
Do While .Execute
With Selection
.MoveRight
.MoveEndUntil Cset:=" "
.delete
End With
Loop
End With
End With

HTH,
Dave
 
J

Julia

THANKS DAVE!!!!

Dave Lett said:
Hi Julia,

<snip>THANK YOU I LOVE YOU </snip>
Okay, but don't tell my fiancee. She's the jealous type :)

This probably even easier:

With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Text = "^m"
.Forward = True
.Wrap = wdFindStop
Do While .Execute
With Selection
.MoveRight
.MoveEndUntil Cset:=" "
.delete
End With
Loop
End With
End With

HTH,
Dave
 

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