VBA leaves an Excel process

VBA leaves an Excel process

Anonymous
Not applicable
386 Views
2 Replies
Message 1 of 3

VBA leaves an Excel process

Anonymous
Not applicable

Can anyone tell me why I run this code and it
leaves an Excel process running till I close ACAD?  And how can I stop this
from happening?

 

<snip>

Dim excelApp As Object
Dim wbkObj As
Object
Dim shtObj As Object

 

    On Error Resume
Next
    Set excelApp = GetObject(,
"Excel.Application")
    If Err <> 0
Then
       
Err.Clear
        Set excelApp =
CreateObject("Excel.Application")
       
If Err <> 0
Then
           
MsgBox "Could not start Excel",
vbExclamation
           
End
        End If
   
End If
    excelApp.Visible = True
    Set
wbkObj = Workbooks.Open(FileName:=strDWGIndex)
    If
Workbooks.COUNT = "0" Then
       
Workbooks.Add
    End If
    Set wbkObj =
Nothing
    Set excelApp = Nothing

 

End Sub

<snip>


--
 
Thanks,
David M.
Gardner
Change the DOT to reply
0 Likes
387 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable
Access the workbook object of the Excel application and make sure to close &
quit.....

Dim excelApp As Excel.Application
Dim wbkObj As Workbook
On Error Resume Next
Set excelApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Err.Clear
Set excelApp = CreateObject("Excel.Application")
If Err <> 0 Then
MsgBox "Could not start Excel", vbExclamation
End
End If
End If
excelApp.Visible = True
Set wbkObj = excelApp.Workbooks.Open("c:\test.xls")
If excelApp.Workbooks.Count = "0" Then
excelApp.Workbooks.Add
End If
wbkObj.Close
excelApp.Quit
Set wbkObj = Nothing
Set excelApp = Nothing

"David M. Gardner" wrote in message
news:5088296@discussion.autodesk.com...
Can anyone tell me why I run this code and it leaves an Excel process
running till I close ACAD? And how can I stop this from happening?

0 Likes
Message 3 of 3

Anonymous
Not applicable

You must quit excel when you are done.

 

[code]

Dim ExApp as Excel.Application

Dim NewExcelSession As Boolean

 

On Error Resume Next  ' if we fail on errors
then skip to error handler next
  Set ExApp = GetObject(,
"excel.application") ' Get Ref to Excel
  If Err <> 0 Then ' Can't
create object because Excel was not already open
   
Err.Clear
      Set ExApp =
CreateObject("excel.application")  ' Start
Excel
      If Err <> 0 Then ' Excel is not
installed fault
        Err.Raise
Err.Number  ' Raise error to calling
function
     
Else
        NewExcelSession =
True
      End If
 
Else
    NewExcelSession = False
  End
If
objExcelApp.Visible = True ' show excel so that errors can be shut
down

...    Do some work with
ExApp

...

Rem Insert code to close all open workbooks
first

 

If NewExcelSession Then   ' If a new
session then we can close it
  If ExApp.Workbooks.Count = 0
Then   ' check to see if there are no open Workbooks
first
    ExApp.Application.DisplayAlerts = False  
' Turn off alerts
    ExApp.Application.Quit  ' End excel
session
  End If

Endif

Set ExApp = Nothing

[/code]


--
Phil Custer II, P.E.
Custer Services,
Inc.

href="mailto:custer@landfillgas.com">custer@landfillgas.com


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">


Can anyone tell me why I run this code and it
leaves an Excel process running till I close ACAD?  And how can I stop
this from happening?

 

<snip>

Dim excelApp As Object
Dim wbkObj As
Object
Dim shtObj As Object

 

    On Error Resume
Next
    Set excelApp = GetObject(,
"Excel.Application")
    If Err <> 0
Then
       
Err.Clear
        Set excelApp =
CreateObject("Excel.Application")
       
If Err <> 0
Then
           
MsgBox "Could not start Excel",
vbExclamation
           
End
        End If
   
End If
    excelApp.Visible = True
    Set
wbkObj = Workbooks.Open(FileName:=strDWGIndex)
    If
Workbooks.COUNT = "0" Then
       
Workbooks.Add
    End If
    Set wbkObj =
Nothing
    Set excelApp = Nothing

 

End Sub

<snip>


--
 
Thanks,
David M.
Gardner
Change the DOT to reply
0 Likes