Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

debugging autoexec sub in word vba project

Status
Not open for further replies.

davikokar

Technical User
May 13, 2004
523
IT
hallo,

I have a word template that contains a module with the autoexec sub.

I don' know how I can debug this sub. It is called automatically when I open word. But I need to have word open in order to place a breakpoint ?

Does someone knows ? Thanks
 
What happens if you open word with the shift key depressed ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
if I press "shift" and I open word, it seems that the autoexec it is not called (i placed a msgbox in it, and it doesn't show if shift is pressed)
 
Isn't what you wanted ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
well... I am not sure. Is this the traditional way to debug the autoexec ?

thanks
 
No, it is the traditional way to bypass ANY automatic execution of procedures. It loads Word with no macros.

However, now that you are in Word, you can debug anything you like. Although, it seems to me that, saving any autoexec procedure before debugging is a wee bit backwards, and/or dangerous.

Gerry
 
Nobody seems to be answering the question here.

Firstly, you can run the macro (single stepped, or with a breakpoint set if you want) any time you like - just because it automatically executes at a particular time does not stop it running at a different time as well, if that is what you want.

If you want to actually debug at load time you could try putting a Msgbox in the code and pressing Ctrl+Break when it shows. However, if you are testing a global template (which it rather sounds like) you won't be able to do that because the source code will not be available.

If you do have a problem that only occurs at startup then debugging can be a slow process, somehow logging actions until you identify whatever is going wrong. You may not need to continually stop and start Word - it may be sufficient to unload and load the template. It all depends on exactly what the problem is.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Nobody seems to be answering the question here."

Hmmm. Yes...sort of, but not really. Let's look at the OP.

"I don' know how I can debug this sub. It is called automatically when I open word. But I need to have word open in order to place a breakpoint ?"

First of all, yes, it would be called automatically, as it IS autoexec. But once Word is opened, and yes it would have executed, you still debug it like any other procedure, in the VBE.

It is a procedure just like any other procedure, and all debugging tools/options are available.

"If you want to actually debug at load time " Yikes Tony, I am surprised you mention that! Debugging at load-time is, IMO, an oxymoron.

karerda, if you want to debug it, either use the shift-execute Word (bypassing it), and then debug it in the VBE; OR, let it run, and then...debug it in the VBE.

In either case, you debug any procedure in the VBE, using debugging tools/options. Breakpoints, Watches, Stepping-Through....whatever.



Gerry
 
Additionally, if you are able to edit the code, save the file, and open it again without "messing something up". You can add this line:
Code:
[i]sub whateversub()
' comments
variables As Type[/i]

[b][red]Debug.Assert 0[/red][/b]

[i]rest of code...[/i]
This will immediately bring the Microsoft Visual Basic Editor up, suspend the processing of the macro, and you may then step through the code (F8). Just don't forget to comment it out or remove it before finalizing!
 
> Debugging at load-time is, IMO, an oxymoron.

Well, yes and no. If you have code that works in isolation, but fails due to some interaction at load time, you have to work out what is going wrong at load time, whatever you call it.

Debugging is a skill that programmers learn, but many VBA users are not programmers and they can come unstuck.

Gruuuu .. unfortunately, code in global templates is not available for stepping through (at startup or any other time), which I suspect is part of karerda's problem - although (s)he's not giving much away.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
unfortunately, code in global templates is not available for stepping through (at startup or any other time),"

Except of course for Normal. But yes, we do not have enough specific detail. It is AutoExec in "a template", but where is this template? How is it invoked? Is this being done via a File > New, cloning the .DOT file?

Why is it even in an AutoExec in the first place? Frankly, I rarely, if ever, use an AutoExec. I used to years ago, but I find there is little actual need for an AutoExec. Other automatic procedures work better (more explicitly), such as Document_New.

Gerry
 
> Except of course for Normal.

Indeed. I shouldn't generalise.

AutoExec fires when an Addin/Global Template is loaded and there isn't an Event that can be used instead. I don't see a problem with using one, and there can be timing issues that only show up when loaded at startup in combination with other templates, or in a partially loaded Word environment. I think the original question may be valid but we seem to have lost karerda.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
The original question is valid, but it has also been answered. At least I think so. You debug a procedure - any procedure - within the VBE.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top