View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
NickHK NickHK is offline
external usenet poster
 
Posts: 4,391
Default Excel VBA cause Word to Crash

I didn't wade through all your code, but if this is in Excel VBA, you do not
need:

Set xlApp = GetObject(, "Excel.Application")

as you already have the Application object which refers to the current
instance.
Actually, the above code may return a completely different instance of Excel
to the one the code is running in, if multiple instances exist.

It would appear you are using late binding to Word, as you have decalred
those variables "As Object".
As such, you cannot use named constants/enums like "wdWindowStateMinimize",
but you need their numeric value, which can get from the Word VBE Immediate
window:
?wdWindowStateMinimize

What do expect this to evaluate to ?
If (missingObject1 Or missingObject2) Then

You could be more consistent with your references to objects instead of
mixing it up e.g.:
Sheets("IT - Resources & Costs").Range("A5:G51").Copy
destXL.Sheets("IT - Resources & Costs").Activate
Range("A5:G51").Select
ActiveSheet.Paste


NickHK

wrote in message
ups.com...
Hi,
I am somewhat new to vba and I am not sure if I am posting this in
the
right place but I can't seem to figure this one out or find a problem
that is similar in the postings.

My vba code runs in excel, which opens a word file, and then
subsequently has to access excel documents embedded in that word
file,
to copy some information back to the original excel document running
the code. After it is done, the code closes the document and then
word
(only if word was not previously running). The problem is that Word
seems to intermittently crash when I am trying to close the document
or the application.


I have thought of several reasons this may be happening. My primary
concern is this though: I do not know how change the focus/close from
the last embedded excel document once it has been activated, so I go
from that to just close the document. I wonder if what causes Word to
crash is that it is trying to hold on to the OLE connections of the
embedded excel files and takes time to release them. I had tried to
deal with this by putting a Sleep break in the program, but it still
seems to crash 1 out of about 10 times.


I have attached the code below:
========CODE============
Dim SAFileName As String
Dim strFilter As String


Dim wdApp As Object
Dim wdDoc As Object
Dim wdRunning As Boolean

--------- CUT ----