outlook vba works in break mode only

I have a macro that I run in Outlook. All it needs to do is open a Word document. It opens Word but won’t open the document. But when I step through one line at a time in break mode, it works fine. Here is the relevant part of the code:

Set Word = Application.CreateObject(“Word.Application”)
Word.Visible = True
Word.Documents.Open (“P:\TPS\Dave\checklist.doc”)

Should be simple enough. But when the macro is running it opens Word but not the document. Any ideas?

I have done a lot of VBA in Outlook, Excel, Word, and Project. I have never seen any macro that does not run properly unless you F8 it, so I’m not sure what is causing this.

Just from this snippet what jumps out at me is that you are using Word as an object name. I would recommend you use something else, like WordApp or something. You could be having a reserved word collision or something more subtle.

I don’t know if that is causing your problem but it’s so easy to change I would try it.

Nope, that didn’t help. I’ve actually been running this macro for quite a while in Windows XP with no problems, but this morning I was moved to a Virtual Desktop running Windows 7. Maybe that has something to do with it?

I use XP and Vista, so I’m not sure why there might be an issue in W7. Another thing that occurred to me is that your document object is anonymous. You might try something like this

Dim d as Document
Set Word = Application.CreateObject(“Word.Application”)
Word.Visible = True
Set d = Word.Documents.Open (“P:\TPS\Dave\checklist.doc”)

It shouldn’t be necessary, but I don’t really know what the problem is.

That gives a compile error. It doesn’t recognize “Document” as a dimension type; remember this macro is in Outlook, not Word.

Thanks for your help. This is truly bizarre.

Maybe you should try running it with UAC off, just to eliminate that as a roadblock.

You mean you don’t have the Word object reference library imported? That could also be a little glitch. But still doesn’t explain why your code works only in the debugger.

Wow, I was the developer at Microsoft who wrote the VBA integration into Outlook 10 years ago. Honestly, you are the first person I’ve ever encountered who actually used the feature. :slight_smile:

Your code appears to be correct, and the fact that it works when you step one line at a time in break mode is suspicious. Just a guess – do you have some add-ins or startup macros in Word (not Outlook, but word)? Word is initialized in two phases – phase one starts the app, and phase two loads up all your add-ins. If you have a slow-loading plugin, then could cause phase two to take longer than usual. Loading the document during that time will fail. But your macro works when you step line-by-line, because then you’re going slower, so Word has enough time to finish loading its plugins and startup macros before you get to the last line which loads the document.

Try disabling your word addins and startup macros. Does that fix it then?

Seriously? That makes you practically a celebrity :slight_smile: So maybe *you *can tell me. Why doesn’t Outlook have a macro recorder? There are actually a lot of people who use this feature and that would have made life a lot easier. Outlook’s object model is not quite as intuitive as the Excel model, for example.

I just had one add-in, a global template. I disabled it and it still didn’t work. I wrote a 3 second delay timer into the macro and now it works.

Thanks for your help.

This. +1000

Follow up question: is it possible to run my macro with an appointment selected, and have it pass the date and time of the appointment to a userform in the Word document?