Why does my macro reset?

E

Ed

I'm using Word 2000. I'm trying to open a document and set an object to it.
The doc opens fine - and then the macro resets! I'm stepping through with
F8 - when it executes the Open statement, the doc opens, and the next toggle
of the F8 key takes me back to the Sub line. What did I get wrong?

Sub Foo_ReplaceMyWord()

Dim doc As Document
Dim str As String
Dim rng As Range

str = "C:\Documents and Settings\emillis\Desktop\Test.doc"
Set doc = Application.Documents.Open(str)


Ed
 
E

Ed

Hi, Tony. The macro starts

Sub Foo_ReplaceMyWord()

Dim doc As Document
Dim str As String
Dim rng As Range

str = "C:\Documents and Settings\emillis\Desktop\Test.doc"
Set doc = Application.Documents.Open(str)
Set rng = doc.Content
With rng.Find
.ClearFormatting
''' etc on the .Find

I'm watching my Locals window. As soon as it executes the Open, everything
resets, and I have no variables.

Any suggestions?
Ed
 
T

Tony Jollans

The reason I asked about the next line of code was that it sounds like
you're recursively entering the procedure. Your statement about the
variables tends to confirm that but, unfortunately, I can't see why that
would happen from what you've posted.

Sorry, I'll have to hand over to an expert. I'm sure there'll be one along
shortly.
 
R

rhamre

I don't know exactly why it resets, but I think it has something to do with-

"Set doc = Application.Documents.Open(str)"

I made this up real fast... all it does is opens your directx log and
highlights it.

Sub test()

Dim x As String

x = "C:\WINDOWS\DirectX.log"

Application.Documents.Open (x)

Selection.WholeStory

End Sub

This syntax works.

I hope this helped.

-Ryan Hamre
 
E

Ed

An update and sort of an answer: the macro was doing all it was supposed to
do. It opened the doc and set the objects just fine. It then went on
through the rest of the code - which was written incorrectly and did
nothing, so it looked like the macro was simply resetting itself. The rest
of the code is fixed and the macro works fins.

Except - why, upon opening the doc, did the macro jump out of Step mode into
Run mode???

Ed
 
R

rhamre

I don't think it left "step" mode, it only shows the step when it steps onto
a working piece of code.
 
E

Ed

The code that was written was workable and executable (using Find) - it just
lacked the proper steps with the .Execute to actually perform any useful
function. I'm used to seeing the yellow step highlight go down through each
line. Instead, as soon as this code opened the document, it dropped to the
next code line.

For instance, to check my code, I changed
Set doc = Application.Documents.Open (str)
to
Application.Documents.Open (str)
MsgBox ActiveDocument.Name

As soon as the doc opened, the message box popped up. Instead, it should
have taken be back to the macro code with the MsgBox line waiting to be
executed. A Stop command inserted took me back into the macro, but it left
the Step Through mode.

Ed
 
T

Tony Jollans

There are various actions (usually those which affect the VBE) which can
disable 'step mode'.

Also protected code won't be stepped through.

So it is possible to have a document_open (or autoOpen) macro (which would
fire following the open command) which is protected and whcih then does
something which disables step mode.

Might this be the case? I can't think of anything else off the top of my
head.
 

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