Step through template AutoNew macro?

E

Ed

I have a Word 2000 template with an AutoNew macro. This is called by a
macro in Excel. I need to make some changes to the Word macro, but I can't
seem to figure out how to step through it to see what's happening. I can
step through the Excel macro, but when it hits
Set WD = CreateObject("Word.Application")
WD.Documents.Add
the Word document and actions are invisible, and I can't see the steps.

Any suggestions?

Ed
 
J

Jean-Guy Marcil

Hi Ed,

Just for testing purposes, add the following under the code you posted:

WD.Visible = True
Stop

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
E

Ed

"Jean-Guy Marcil" <
Just for testing purposes, add the following under the code you posted:

WD.Visible = True
Stop

That didn't work. It runs right over the Visible = True line and stops on
Stop, but Word is not visible, either on the screen or in the Taskbar. It's
as though Word is running *deep* in the background. Any other suggestions?

Ed
 
A

Alex Ivanov

It's weird. Try to copy your XL macro to a Word Doc, at least that part that
calls
Set WD = CreateObject("Word.Application")
WD.Documents.Add
WD.Visible = True
and run it. Does it make any difference?

Alex.
 
E

Ed

Alex - I have:
Sub Test_See_Word()

Dim MyTarget As String
Dim WD As Object
Dim doc As String

Set WD = CreateObject("Word.Application")
WD.Documents.Add ("EFF_List(bbox)")
WD.Visible = True
Stop

End Sub

The Word doc became visible only to show me an error box because I was using
a "sample" Excel table to paste in, which didn't have the data required for
some text strings.

If you hve anything else, I'd be interested to heazr it. But I'm headed
home from work right now, and won;t be back for a while. Good night.
Ed
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Hi Ed,

Throw in a WD.Activate command

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
 
A

Alex Ivanov

Try to hardcode an actual template path in
WD.Documents.Add ("EFF_List(bbox)")' Invalid template
or use Add without parameters; with the code you posted Word throws an
exception before it has a chance to get visible.
You may also try to set Options/General/Break on all errors, but don't
forget to set it back to Unhandled errors when you are done debugging.

Alex.

"Doug Robbins - Word MVP - DELETE UPPERCASE CHARACTERS FROM EMAIL ADDRESS"
 
E

Ed

Doug - The WD.Activate didn't work, in neither the Excel nor the Word code.

Alex - That *is* a hard-coded path to an actual template. The template has
an AutoNew macro that takes the Excel data from the clipboard, pastes it in
as a Word table and formats it, does some things and saves the document.
The whole process runs okay, except I needed to make some changes and it
would have been nice to be able to step through the entire routine from the
Excel side (the start of the process is in Excel, where the spreadsheet gets
filtered and sorted and put on the clipboard before calling the Word
template). During the whole process, though, Word is totally invisible - I
never see a document on the screen or the taskbar. I had to put a MsgBox
"I'm done!" at the end, or I'd never know! 8>)

Anyway, this isn't a critical thing, and y'all have better things to do, I'm
sure. Thanks for all the help.

Ed
 
T

Tom Winter

Sorry to butt in, but why don't you start Word manually before everything,
and then use GetObject()?
 
E

Ed

Hi, Tom. Do you mean change
Set WD = CreateObject("Word.Application")
to
Set WD = GetObject("Word.Application") ?

What is the difference between starting Word manually and starting it by
code? Would it be just as effective if I set code in the Excel macro
earlier to start a new instance of Word, but let it set there before using
it? Although that would be difficult here - this is a template with
AutoNew, so as soon as I opened it the macro would fire. I'd have to put
that in a module and call it in the Excel procedure. Haven't had much luck
with that, which is why I'm doing it this way.

Ed
 
A

Alex Ivanov

Another thought: Maybe you set WD.Visible=False somewhere in your AutoNew
macro?
Try to run a simple vbs file with content like this:

set x=createobject("word.application")
x.visible=true
x.documents.add
set x=nothing

Does Word become visible with this?

Alex.
 
E

Ed

Alex - I put that code in an Excel module and it worked fine, even calling
my template, leaving Word visible.
I do not have Visible = False or even ScreenUpdating = False in my Word
macro.

So I needed to set Visible *before* Adding the doc? Thank you!

Ed
 
T

Tom Winter

I was suggesting that just for debugging purposes. If you start Word
manually, then you know its displayed all ready. That was my point.
 
E

Ed

Oh, right. Yes, I've had Word open before when this has run. But, with
Word's "Multi-Document Interfarce", and the new document opens up from a
different template than Normal, that document window never comes to the
front. Thanks for the input, though.

Ed
 
P

Peter Hewett

Hi Ed

This threads far toooooooo long!

Ok, if you use GetObject (Words already running) you can try a few things:

1. In your Excel code once you have a Word document object reference try
"WordDocObject.Activate".
2. In Word open the template the document Excel opens/creates uses and set a
breakpoint in the Word VBA IDE in AutoNew/AutoOpen etc.
3. Introduce a deliberate bug into the AutoNew/AutoOpen code so that the VBA
IDE faults, and asks you if you want to debug!

If I need to debug Interop apps, as Tom suggests, I manually start the other
application and use GetObject to connect to the application. In the other app
I open the necessary document/template/spreadsheet (whatever), start that
applications VBA IDE and set breakpoints where I want to debug.

Once it's all sorted I removed the GetObject (I try not to use CreateObject
either as that's late bound and inefficient) and use something like:

Set appWord = New Word.Application
Set appExcel = New Excel.Application

HTH + Cheers - Peter
 
P

Peter Hewett

Hi Ed

Just to clarify. When you use GetObject in Excel use it as follows:

Dim appWord as Object
Set appWord = GetObject(,"Word.Application")
If err.Number <> 0 Then MsgBox "Could not connect to Word"

Cheers - Peter
 
E

Ed

Hi, Peter!

Peter Hewett said:
Hi Ed

This threads far toooooooo long!

I agree! Even though I keep adding! 8>) (But this will be the last one -
I promise!)

I know the "deliberate bug" works - accidental bugs have raised the
debugger. I'll check out the other ideaas. Many thanks for chiming in.

Ed
 

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