473,785 Members | 2,829 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Excel Process Close

Hi all,

I've spent countless hours trying to get this silly EXCEL process to
close once I have used it. If anyone can help it would really REALLy
be appreciated! Below is my code-

Dim t As Integer
Dim testCount As Boolean
testCount = False
For t = 0 To UserListView.It ems.Count - 1
If UserListView.It ems(t).Checked = True Then
testCount = True
End If
Next

If testCount = True Then

Dim j As Integer
Dim k As Integer
Dim strSelectedStar tDate As String
Dim strSelectedEndD ate As String
strSelectedStar tDate = CStr(Format(Dat eTimePickerStar t.Value,
"dd-MM-yyyy"))
strSelectedEndD ate = CStr(Format(Dat eTimePickerEnd. Value,
"dd-MM-yyyy"))
Dim fiPath As IO.FileInfo
Dim strPath As String

fiPath = New IO.FileInfo
System.Reflecti on.Assembly.Get ExecutingAssemb ly.Location)
strPath = fiPath.Director y.FullName

Dim PathStr As String
PathStr = "" & strPath & "\ClientTemplat e.xls"

For j = 0 To UserListView.It ems.Count - 1

If UserListView.It ems(j).Checked = True Then
ConnectToDataba se()
DoSummary(UserL istView.Items(j ).Text,
DateTimePickerS tart.Value, _

DateTimePickerE nd.Value)
Dim EXL As Excel.Applicati on = New
Excel.Applicati on
Dim oWorkbooks As Excel.Workbooks = EXL.Workbooks
Dim oWorkbook As Excel.Workbook =
oWorkbooks.Open (PathStr)

Dim SummaryWorkShee t As Excel.Worksheet

Dim DetailWorksheet As Excel.Worksheet

EXL.Visible = False
SummaryWorkShee t = EXL.Workbooks(1 ).Worksheets(1)
SummaryWorkShee t.Cells(1, 2) =
UserListView.It ems(j).Text
SummaryWorkShee t.Cells(2, 2) =
strSelectedStar tDate
SummaryWorkShee t.Cells(3, 2) = strSelectedEndD ate

If SummaryTable.Ro ws.Count > 0 Then
Dim i As Integer

For i = 0 To SummaryTable.Ro ws.Count - 1
Dim EXLAppName As String

SummaryWorkShee t.Activate()
SummaryWorkShee t.Cells(6 + i, 1) =
SummaryTable.Ro ws(i).Item(0)
SummaryWorkShee t.Cells(6 + i, 2) =
SummaryTable.Ro ws(i).Item(1)
SummaryWorkShee t.Cells(6 + i, 3) =
SummaryTable.Ro ws(i).Item(2)

DetailWorksheet =
EXL.Workbooks(1 ).Worksheets.Ad d(after:=EXL.Wo rkbooks(1).Work sheets(1 +
i))
DetailWorksheet =
EXL.Workbooks(1 ).Worksheets(i + 2)
DetailWorksheet .Name =
SummaryTable.Ro ws(i).Item(0)

DetailWorksheet .Cells(6, 1) =
SummaryTable.Ro ws(i).Item(0)
DetailWorksheet .Cells(6, 2) =
SummaryTable.Ro ws(i).Item(1)
DetailWorksheet .Cells(6, 3) =
SummaryTable.Ro ws(i).Item(2)

DetailWorksheet .Cells(1, 2) =
UserListView.It ems(j).Text
DetailWorksheet .Cells(2, 2) =
strSelectedStar tDate
DetailWorksheet .Cells(3, 2) =
strSelectedEndD ate
DetailWorksheet .Columns("A").C olumnWidth =
16.71
DetailWorksheet .Columns("B").C olumnWidth =
25
DetailWorksheet .Columns("C").C olumnWidth =
50
DetailWorksheet .Columns("D").C olumnWidth =
18

DetailWorksheet .Columns("A:A") .select()
EXL.Selection.H orizontalAlignm ent =
Excel.XlHAlign. xlHAlignLeft
DetailWorksheet .Columns("B:B") .select()
EXL.Selection.H orizontalAlignm ent =
Excel.XlHAlign. xlHAlignLeft
DetailWorksheet .Columns("C:C") .select()
EXL.Selection.H orizontalAlignm ent =
Excel.XlHAlign. xlHAlignLeft
DetailWorksheet .Columns("D:D") .select()
EXL.Selection.H orizontalAlignm ent =
Excel.XlHAlign. xlHAlignLeft
DetailWorksheet .Cells(1, 1).select()

DetailWorksheet .Range("A1:A3") .Font.Bold =
True
DetailWorksheet .Range("A5:C5") .Font.Bold =
True
DetailWorksheet .Range("A8:D8") .Font.Bold =
True
DetailWorksheet .Cells(1, 1) = "UserName:"
DetailWorksheet .Cells(2, 1) = "Start
Date:"
DetailWorksheet .Cells(3, 1) = "End Date:"

DetailWorksheet .Cells(5, 1) = "Applicatio n
Name"
DetailWorksheet .Cells(5, 2) = "Total
Hours"
DetailWorksheet .Cells(5, 3) = "Condition"

DetailWorksheet .Cells(8, 1) = "Log
Details"
DetailWorksheet .Cells(8, 2) = "Start Time"
DetailWorksheet .Cells(8, 3) = "End Time"
DetailWorksheet .Cells(8, 4) = "Chargeable
Hours"
EXLAppName = SummaryGrid.Ite m(i, 0)
DoDetails(UserL istView.Items(j ).Text,
EXLAppName, DateTimePickerS tart.Value, _

DateTimePickerE nd.Value)
For k = 0 To DetailLogTable. Rows.Count - 1
DetailWorksheet .Cells(k + 9, 1) =
DetailLogTable. Rows(k).Item(0)
DetailWorksheet .Cells(k + 9, 2) =
DetailLogTable. Rows(k).Item(1)
DetailWorksheet .Cells(k + 9, 3) =
DetailLogTable. Rows(k).Item(2)
DetailWorksheet .Cells(k + 9, 4) =
DetailLogTable. Rows(k).Item(3)
Next

Next

SummaryWorkShee t.Activate()
End If

oWorkbook.SaveA s("" & strPath & "\" &
UserListView.It ems(j).Text & " from " & strSelectedStar tDate & " to "
& strSelectedEndD ate & ".xls")
oWorkbooks.Clos e()
SummaryWorkShee t = Nothing
DetailWorksheet = Nothing
oWorkbooks = Nothing
oWorkbook = Nothing

EXL.Quit()
EXL = Nothing

DisconDatabase( )
GC.Collect()

End If

Next

I know its alot of code to look at but I'm not quite sure what to
leave out incase that is what is creating the problem!
I'm really running out of ideas..

Thanks
PowaGuy
Nov 21 '05 #1
4 4390
* ge******@hotmai l.com (Powerguy) scripsit:
I've spent countless hours trying to get this silly EXCEL process to
close once I have used it. If anyone can help it would really REALLy
be appreciated! Below is my code-


PRB: Office Application Does Not Quit After Automation from Visual Studio .NET Client
<URL:http://support.microso ft.com/?scid=kb;EN-US;317109>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #2
Hi,

Replace EXL = Nothing with Marshal.Release ComObject(EXL)
http://msdn.microsoft.com/library/de...bjecttopic.asp

Ken
--------------------

"Powerguy" <ge******@hotma il.com> wrote in message
news:79******** *************** ***@posting.goo gle.com...
Hi all,

I've spent countless hours trying to get this silly EXCEL process to
close once I have used it. If anyone can help it would really REALLy
be appreciated! Below is my code-

Dim t As Integer
Dim testCount As Boolean
testCount = False
For t = 0 To UserListView.It ems.Count - 1
If UserListView.It ems(t).Checked = True Then
testCount = True
End If
Next

If testCount = True Then

Dim j As Integer
Dim k As Integer
Dim strSelectedStar tDate As String
Dim strSelectedEndD ate As String
strSelectedStar tDate = CStr(Format(Dat eTimePickerStar t.Value,
"dd-MM-yyyy"))
strSelectedEndD ate = CStr(Format(Dat eTimePickerEnd. Value,
"dd-MM-yyyy"))
Dim fiPath As IO.FileInfo
Dim strPath As String

fiPath = New IO.FileInfo
System.Reflecti on.Assembly.Get ExecutingAssemb ly.Location)
strPath = fiPath.Director y.FullName

Dim PathStr As String
PathStr = "" & strPath & "\ClientTemplat e.xls"

For j = 0 To UserListView.It ems.Count - 1

If UserListView.It ems(j).Checked = True Then
ConnectToDataba se()
DoSummary(UserL istView.Items(j ).Text,
DateTimePickerS tart.Value, _

DateTimePickerE nd.Value)
Dim EXL As Excel.Applicati on = New
Excel.Applicati on
Dim oWorkbooks As Excel.Workbooks = EXL.Workbooks
Dim oWorkbook As Excel.Workbook =
oWorkbooks.Open (PathStr)

Dim SummaryWorkShee t As Excel.Worksheet

Dim DetailWorksheet As Excel.Worksheet

EXL.Visible = False
SummaryWorkShee t = EXL.Workbooks(1 ).Worksheets(1)
SummaryWorkShee t.Cells(1, 2) =
UserListView.It ems(j).Text
SummaryWorkShee t.Cells(2, 2) =
strSelectedStar tDate
SummaryWorkShee t.Cells(3, 2) = strSelectedEndD ate

If SummaryTable.Ro ws.Count > 0 Then
Dim i As Integer

For i = 0 To SummaryTable.Ro ws.Count - 1
Dim EXLAppName As String

SummaryWorkShee t.Activate()
SummaryWorkShee t.Cells(6 + i, 1) =
SummaryTable.Ro ws(i).Item(0)
SummaryWorkShee t.Cells(6 + i, 2) =
SummaryTable.Ro ws(i).Item(1)
SummaryWorkShee t.Cells(6 + i, 3) =
SummaryTable.Ro ws(i).Item(2)

DetailWorksheet =
EXL.Workbooks(1 ).Worksheets.Ad d(after:=EXL.Wo rkbooks(1).Work sheets(1 +
i))
DetailWorksheet =
EXL.Workbooks(1 ).Worksheets(i + 2)
DetailWorksheet .Name =
SummaryTable.Ro ws(i).Item(0)

DetailWorksheet .Cells(6, 1) =
SummaryTable.Ro ws(i).Item(0)
DetailWorksheet .Cells(6, 2) =
SummaryTable.Ro ws(i).Item(1)
DetailWorksheet .Cells(6, 3) =
SummaryTable.Ro ws(i).Item(2)

DetailWorksheet .Cells(1, 2) =
UserListView.It ems(j).Text
DetailWorksheet .Cells(2, 2) =
strSelectedStar tDate
DetailWorksheet .Cells(3, 2) =
strSelectedEndD ate
DetailWorksheet .Columns("A").C olumnWidth =
16.71
DetailWorksheet .Columns("B").C olumnWidth =
25
DetailWorksheet .Columns("C").C olumnWidth =
50
DetailWorksheet .Columns("D").C olumnWidth =
18

DetailWorksheet .Columns("A:A") .select()
EXL.Selection.H orizontalAlignm ent =
Excel.XlHAlign. xlHAlignLeft
DetailWorksheet .Columns("B:B") .select()
EXL.Selection.H orizontalAlignm ent =
Excel.XlHAlign. xlHAlignLeft
DetailWorksheet .Columns("C:C") .select()
EXL.Selection.H orizontalAlignm ent =
Excel.XlHAlign. xlHAlignLeft
DetailWorksheet .Columns("D:D") .select()
EXL.Selection.H orizontalAlignm ent =
Excel.XlHAlign. xlHAlignLeft
DetailWorksheet .Cells(1, 1).select()

DetailWorksheet .Range("A1:A3") .Font.Bold =
True
DetailWorksheet .Range("A5:C5") .Font.Bold =
True
DetailWorksheet .Range("A8:D8") .Font.Bold =
True
DetailWorksheet .Cells(1, 1) = "UserName:"
DetailWorksheet .Cells(2, 1) = "Start
Date:"
DetailWorksheet .Cells(3, 1) = "End Date:"

DetailWorksheet .Cells(5, 1) = "Applicatio n
Name"
DetailWorksheet .Cells(5, 2) = "Total
Hours"
DetailWorksheet .Cells(5, 3) = "Condition"

DetailWorksheet .Cells(8, 1) = "Log
Details"
DetailWorksheet .Cells(8, 2) = "Start Time"
DetailWorksheet .Cells(8, 3) = "End Time"
DetailWorksheet .Cells(8, 4) = "Chargeable
Hours"
EXLAppName = SummaryGrid.Ite m(i, 0)
DoDetails(UserL istView.Items(j ).Text,
EXLAppName, DateTimePickerS tart.Value, _

DateTimePickerE nd.Value)
For k = 0 To DetailLogTable. Rows.Count - 1
DetailWorksheet .Cells(k + 9, 1) =
DetailLogTable. Rows(k).Item(0)
DetailWorksheet .Cells(k + 9, 2) =
DetailLogTable. Rows(k).Item(1)
DetailWorksheet .Cells(k + 9, 3) =
DetailLogTable. Rows(k).Item(2)
DetailWorksheet .Cells(k + 9, 4) =
DetailLogTable. Rows(k).Item(3)
Next

Next

SummaryWorkShee t.Activate()
End If

oWorkbook.SaveA s("" & strPath & "\" &
UserListView.It ems(j).Text & " from " & strSelectedStar tDate & " to "
& strSelectedEndD ate & ".xls")
oWorkbooks.Clos e()
SummaryWorkShee t = Nothing
DetailWorksheet = Nothing
oWorkbooks = Nothing
oWorkbook = Nothing

EXL.Quit()
EXL = Nothing

DisconDatabase( )
GC.Collect()

End If

Next

I know its alot of code to look at but I'm not quite sure what to
leave out incase that is what is creating the problem!
I'm really running out of ideas..

Thanks
PowaGuy
Nov 21 '05 #3
Hi guys,

Yah believe me I've tried that marshal thing. That EXCEL.exe process
just refuses to die. Is there a way that I can find out the PID of
the process of the EXCEL.exe process that has been opened by the
automation (left open!!)somehow through code.

I'm hoping if I can get that instance of the process I can use some
sort of API function to kill it on exit of the subroutine.

Any ideas how this can be done?
Nov 21 '05 #4
Hi All,

Thanks for all your help. I have managed to get the process id's of
all the EXCEL.exe processes at the instance that I want to terminate
my EXCEL.exe process.

The problem I have now is - is there any way to determine which
instance of EXCEL.exe is the one that Visual Basic has created. I
need some way to get the PID of the EXCEL.exe process as Visual Basic
creates it??

Something like

Dim EXL as new Excel.Applicati on
'' SOME CODE NOW TO GET THE PROCESS ID OF PROCESS CREATED??!

I need to get this information because I get an answer of all the
instances of excel running. I need some way to put into my code to
terminate just the instance that it created. I don't want to terminate
another users session of excel!! (The program is to run on a server) I
know i can get the owner of the process but what if I myself have afew
instances of excel running?? I'm sure there must be away.

Any help would be greatly appreciated.

Come on Gurus!

PowaGuy
Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2095
by: Lorenzo Melato | last post by:
Hi everyone, I have a very strange problem. If I open a .NET application from EXCEL using SHELL function, when I close EXCEL, the EXCEL.EXE process remain active and I must close it by Task manager. If I open a normal application (I.E. Notepad.exe) in the same manner, I have no problem.
11
4057
by: Mr. Smith | last post by:
Hello all, My code can successfully open, write to, format and save several worksheets in a workbook then save it by a given name, close and quit excel. My problem is that if I try and do it again, Excel hangs. OR if I open Excel again (say from a desktop icon) before I close Access, Excel hangs. (this has happened for both 97 & 2000 for me) I of course thought that I mustn't be unloading a variable properly.
3
8373
by: David Berman | last post by:
Hi, I've written an application to do a while bunch of Excel automation. I open a file, scan through all the worksheets, extract data, and then I try to close Excel. However, I can see that I'm not doing it effectively. If I am debugging my app, and I kill the app before my app exits, but after the call that should close excel, I still have an excell process sitting around. After about 20 + of those stack up, you start to feel it. If I...
2
5438
by: Praveen K | last post by:
I have a problem in communicating between the C# and the Excel Interop objects. The problem is something as described below. I use Microsoft Office-XP PIA dll’s as these dll’s were been recommended by many for web applications. I create the instances of Excel, Workbook and the worksheet. And later on Release the references by “System.Runtime.InteropServices.Marshal.ReleaseComObject(Object)” and making the object as null finally....
16
4553
by: LP | last post by:
Hello, I am trying to use .NET with Excel. I installed Office 2003 and selected ..NET programming suport option, so it installed all those PIA, as MS sugests. But I can not find a way to destroy Excel process, it still hangs in the taks manager' Processes as running. I am trying very simple code (see below), but Excel wont go away, I tried just about anything I could find on MSDN or by Google. Even calling WIN32 API to destroy Excel. But...
5
6582
by: mabond | last post by:
Hi recently read a posting and reply about Excel processs still running after the Appliction.Quit was called. Thought I might be able to use the same (System.Runtime.InteropServices.Marshal.ReleaseComObject(exApplication)) to solve my problem but could not. Background : My app uses a WebBrowser control to display to the user the contents of an Excel spreadsheet.:
10
8159
by: Hendri Adriaens | last post by:
Hi, I'm trying to automate the creation of an excel file via COM. I copied my code below. I read many articles about how to release the COM objects that I create. The code below runs just fine and excel is closed. But there are some commented lines: //xlSeries.XValues = xlWs.get_Range("B2", "B4"); // makes com objects, but which...
1
2756
by: popsoftheyear | last post by:
I'm trying to automate some very simple things in excel, but have run into a roadblock (using c++). To detect excel closing, I just monitor the Excel Process using WaitForSingleObject. In order to close it myself, I just call ExcelApp.Quit(). Really basic stuff. Only problem is - I need to release all dispatches to detect the Excel process closing, but I need those dispatches (at least the application one) in order to close excel myself. Am...
1
4286
by: fakehitswizard | last post by:
this is the correct way to close excel with C#. I've seen alot of other bogus posts ALL over the web that don't work, how frustrating. string savepath; bool foundPID; int ourPID = 0; int tmpX = 0; int indexRow = 1; int indexCol = 1; int existingPIDs;
7
2165
by: =?Utf-8?B?VGVycnkgSG9sbGFuZA==?= | last post by:
I have a vb.net app that opens an excel worksheet, reads data and then closes the sheet. Im noticing that the Excel process is still running after I have closed and disposed of my excel objects. The following code (Test1) demonstrates the essence of what I am doing. When I check the processes while ruinning the method, I notice that the Excel process remains after exiting the sub (and until I exit the application) Sub Test1 Dim...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10327
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10151
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.