Macro Works in Form, but not in Module

T

Thomas M

Word 2000

The following code works if I put it into the click event of a form
button, but not when it's in a module.


Sub RemoveRecord()

' Removes the current record, then finds and copies the MIN or NIC from
' the next record.

' Finds the top of the record.
Selection.HomeKey Unit:=wdLine
Selection.Find.ClearFormatting
With Selection.Find
.Text = "-----------------------------------------------"
.Forward = False
.Wrap = wdFindStop
End With
Selection.Find.Execute
If Selection.Find.Found = False Then
Selection.HomeKey Unit:=wdStory
End If

' Extends the selection to bottom of the record.
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.HomeKey Unit:=wdLine
Selection.Find.ClearFormatting
Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.Text = "-----------------------------------------------"
.Forward = True
.Wrap = wdFindStop
End With
Selection.Find.Execute

' Deletes the Record and calls NextRecord to find the next record.
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
NextRecord

End Sub


Basically, it finds the top of the current record, extends the selection
down to the bottom of the record, then deletes the record. The problem
is that when I put this code in a module, the selection does not get
extended for some reason. I noticed that the section that extends the
selection has two Selection.Find.ClearFormatting commands where only one
should be needed. I'm not sure how I ended up with two of those
commands, but taking one or the other out does not seem to have any
impact on the problem.

I did notice today that when I had the module open, then switched to
another module, then switched back, I got an error saying that it is
impossible to view the project. Could it be that the module, or the
entire project, has become corrupt and that I need to create a new module
or project?

--Tom
 

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