Thanks for the post cicatrix. If I use the Findwindow method I have to give the window name. It there are more request from server then all the Window name will be same ?
Please mark you thread resolved using the Thread Tools as shown
Thanks for the post cicatrix. If I use the Findwindow method I have to give the window name. It there are more request from server then all the Window name will be same ?
On the second thought you wouldn't probably know the exact window title. Only that it starts with 'Microsoft Excel'
And trying to close/kill(Kill ) the Excel in Process window using
I would suggest you not to use this method but close it the proper way.
Here is an example.
Code:
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlApp = New Excel.ApplicationClass
'~~> Opening existing workbook
xlWorkBook = xlApp.Workbooks.Open("c:\MyFile.xlsx")
'~~> Working with the first Sheet
xlWorkSheet = xlWorkBook.Worksheets("sheet1")
'~~> Writing to a cell A1
xlWorkSheet.Cells(1, 1) = "danasegarane"
'~~> Closing the workbook
'~~> Need to include one more line if you want to save it via code
xlWorkBook.Close()
xlApp.Quit()
'~~> Clean Up
releaseObject (xlApp)
releaseObject (xlWorkBook)
releaseObject (xlWorkSheet)
End Sub
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject (obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
End Class
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
I would suggest you not to use this method but close it the proper way.
Here is an example.
Code:
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlApp = New Excel.ApplicationClass
'~~> Opening existing workbook
xlWorkBook = xlApp.Workbooks.Open("c:\MyFile.xlsx")
'~~> Working with the first Sheet
xlWorkSheet = xlWorkBook.Worksheets("sheet1")
'~~> Writing to a cell A1
xlWorkSheet.Cells(1, 1) = "danasegarane"
'~~> Closing the workbook
'~~> Need to include one more line if you want to save it via code
xlWorkBook.Close()
xlApp.Quit()
'~~> Clean Up
releaseObject (xlApp)
releaseObject (xlWorkBook)
releaseObject (xlWorkSheet)
End Sub
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject (obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
End Class
I tried this before. And the thing is that I have 30 sheets in a WorkBook and trying to copy to another workbook cause the excel not closing from task list. Let me try once again
Please mark you thread resolved using the Thread Tools as shown
When you automate a Microsoft Office application from Microsoft Visual Basic .NET or Microsoft Visual C# .NET, the Office application does not quit when you call the Quit method.
Please mark you thread resolved using the Thread Tools as shown
I tried this before. And the thing is that I have 30 sheets in a WorkBook and trying to copy to another workbook cause the excel not closing from task list. Let me try once again
Did you try it? I tried copying sheets using my code (with slight amendments of course) and it works just fine....
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
The basics of excel or office automation is to create object variables for each level down the object model. Then destroy each in reverse order. So parent to child creation. Then child to parent destruction.
Dont Implicitly create instances as those will retain an open reference to the object and prevent the object variable from being destroyed.
Dont create an object variable reference more then one level down. This is an example of two levels down and "could" be an issue.
When properly created, destruction will be complete with no leftover open references. You should never really need to use .ReleaseComObject or .FinalReleaseComObject
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.