Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2005

Tip: Looking for answers? Try searching our database.

Why does my macro reset?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ed - 04 Oct 2005 14:55 GMT
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
Tony Jollans - 04 Oct 2005 18:38 GMT
What's the line of code immediately after the open?

--
Enjoy,
Tony

> 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
[quoted text clipped - 11 lines]
>
> Ed
Ed - 04 Oct 2005 19:01 GMT
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

> What's the line of code immediately after the open?
>
[quoted text clipped - 19 lines]
> >
> > Ed
Tony Jollans - 04 Oct 2005 19:20 GMT
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.

--
Enjoy,
Tony

> Hi, Tony.  The macro starts
>
[quoted text clipped - 41 lines]
> > >
> > > Ed
rhamre@citation.com - 05 Oct 2005 01:50 GMT
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

> 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
[quoted text clipped - 11 lines]
>
> Ed
Ed - 05 Oct 2005 16:10 GMT
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

> 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
[quoted text clipped - 11 lines]
>
> Ed
rhamre@citation.com - 05 Oct 2005 16:20 GMT
I don't think it left "step" mode, it only shows the step when it steps onto
a working piece of code.

> 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
[quoted text clipped - 24 lines]
> >
> > Ed
Ed - 05 Oct 2005 21:14 GMT
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

> I don't think it left "step" mode, it only shows the step when it steps onto
> a working piece of code.
[quoted text clipped - 27 lines]
> > >
> > > Ed
Tony Jollans - 05 Oct 2005 22:52 GMT
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.

--
Enjoy,
Tony

> 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
[quoted text clipped - 24 lines]
> >
> > Ed
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.