Imports Excel = Microsoft.Office.Interop.Excel Imports System.Diagnostics Imports System.IO.Path Imports System.Runtime.InteropServices Public Class xlRunning Shared xlApp As Excel.Application Shared xlBook As Excel.Workbook Shared xlSheet As Excel.Worksheet Const xlFile As String = "C:\XVBT\Case.xls" Shared Function Basic_Approach() As Boolean Try 'Check to see if an Excel session exists or not. 'If not running it will through a COM exception. xlApp = CType(Marshal.GetActiveObject("Excel.Application"), Excel.Application) Catch ex As Exception 'Instantiate a new Excel session. xlApp = New Excel.Application End Try Try xlBook = xlApp.Workbooks.Open(xlFile) With xlApp .Visible = True .UserControl = True End With Catch ex As Exception Finally xlBook = Nothing xlApp = Nothing End Try End Function Shared Function Process_Approach() As Boolean Try 'Check to see if (at least) one Excel session is running. If Process.GetProcessesByName("Excel").Length > 0 Then xlApp = CType(GetObject(, "Excel.Application"), Excel.Application) Else ''Instantiate a new Excel session. xlApp = New Excel.Application End If xlBook = xlApp.Workbooks.Open(xlFile) With xlApp .UserControl = True .Visible = True End With Catch ex As Exception Finally xlBook = Nothing xlApp = Nothing End Try End Function Shared Function Process_Approach_BindToMoniker() As Boolean Try '1.If no Excel session is running then a new one is instantiated 'with the wanted workbook opened. '2. If an Excel session exists and the wanted workbook is not open 'then it will be opened. '3. If the workbook is already open then it will be moved to the 'front. xlBook = CType(Marshal.BindToMoniker(xlFile), Excel.Workbook) xlApp = CType(xlBook.Parent, Excel.Application) With xlApp .ScreenUpdating = False .Visible = True With .Windows(GetFileName(xlFile)) .Visible = True .Activate() End With '.UserControl = True End With Catch ex As Exception Finally xlApp.ScreenUpdating = True xlBook = Nothing xlApp = Nothing End Try End Function Shared Function Start_Excel_Process_Do_Stuff_End_Excel_Process() As Boolean Dim procIDSumBefore As Integer = 0 Dim procIDSumAfter As Integer = 0 'Sum all present Excel processes' IDs. For Each xlProcess As Process In Process.GetProcessesByName("Excel") procIDSumBefore = procIDSumBefore + xlProcess.Id Next Try 'Instantiate our Excel process. xlApp = New Excel.Application xlBook = xlApp.Workbooks.Open(xlFile) With xlApp .Visible = True .UserControl = True End With 'Sum all processes' IDs including our Excel process's ID. For Each xlProcess As Process In Process.GetProcessesByName("Excel") procIDSumAfter = procIDSumAfter + xlProcess.Id Next 'Find our Excel process' ID. Dim procID As Integer = procIDSumAfter - procIDSumBefore 'Do the stuff with the Excel file. 'Grab our Excel process. Dim myXLProcess As Process = Process.GetProcessById(procID) With myXLProcess 'Allow users to save the work. .CloseMainWindow() 'Make sure that our Excel process actuals is closed. 'In order to check it a timer is used. .WaitForExit(60000) End With 'If not our Excel process has ended we need to close it now. If Not myXLProcess.HasExited Then myXLProcess.Kill() End If Catch ex As Exception Finally xlBook = Nothing xlApp = Nothing End Try End Function Shared Function Get_All_Excel_Sessions() As Boolean Try If Process.GetProcessesByName("Excel").GetLength(0) > 0 Then xlApp = CType(GetObject(, "Excel.Application"), Excel.Application) xlBook = xlApp.Workbooks.Add xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet) Dim xlProcesses() As Process = Process.GetProcessesByName("Excel") For Each xlProcess As Process In xlProcesses Dim Counter As Integer = Counter + 1 xlSheet.Cells(1, Counter) = xlProcess.MainWindowTitle.ToString Next End If Catch ex As Exception Finally xlSheet = Nothing xlBook = Nothing xlApp = Nothing End Try End Function End Class