Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Check if excel is already open

Posted on 2016-09-15
4
Medium Priority
?
165 Views
Last Modified: 2016-09-15
Hello,
I n vb.net 2015 how to check if excel file is already open ?

Cheers
0
Comment
Question by:RIAS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 7

Expert Comment

by:D Patel
ID: 41799450
Try making a function that will check to see if the workbook is already open or not, like this:

Private Shared Function IsWorkbookAlreadyOpen(app As Excel.Application, workbookName As String) As Boolean
    Dim isAlreadyOpen As Boolean = True

    Try
        app.Workbooks.get_Item(workbookName)
    Catch theException As Exception
        isAlreadyOpen = False
    End Try

    Return isAlreadyOpen
End Function

Then you can use it in your code like this:

Private Sub btnMinSummaryWorksheet_Click(sender As Object, e As EventArgs) Handles btnMinSummaryWorksheet.Click
    'This procedure runs when the btnOpenSummaryWorksheet button is clicked. Calls the
    'Sub procedure opens the Summary Worksheet Dashboard

    Dim xlApp As New Excel.Application
    xlApp.Visible = True

    Dim xlBook As Excel.Workbook
    Dim workbookName = "F:\Test Environment\Compensation Workbook\Compensation Workbook\bin\Debug\2011.1004.Compensation Template.xlsx"
    If IsWorkbookAlreadyOpen(xlApp, workbookName) Then
        xlBook = xlApp.Workbooks.get_Item(workbookName)
    Else
        xlBook = xlApp.Workbooks.Open(workbookName)
    End If

    Dim xlSheet As Excel.Worksheet
    xlSheet = CType(xlBook.Sheets("SummaryWorksheet"), Worksheet)
    xlSheet.Activate()

    Me.Close()
End Sub
0
 
LVL 7

Accepted Solution

by:
D Patel earned 2000 total points
ID: 41799455
Also you can write like this:

jst Check it out this work properly

Imports Microsoft.Office.Interop

Public Class Form1 Private moExcelApplication As Excel.Application Private moExcelWorkBook As Excel.Workbook Private moWorkBooks As Excel.Workbooks Private moActivesheet As Excel.Worksheet Private moWorkSheets As Excel.Sheets Private moCurrentRange As Excel.Range

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShowDirect.Click
    System.Diagnostics.Process.Start("C:\Program Files\Microsoft Office\Office12\EXCEL.EXE")
End Sub

Private Sub Showobj_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Showobj.Click
    Dim sCount As String

    Try
        Cancel.Enabled = True
        If isopen() = 1 Then
            Call OpenExcelFile()
            moActivesheet = moExcelWorkBook.ActiveSheet
            moCurrentRange = moActivesheet.Range("a1", "e5")
            For i As Integer = 1 To 5
                For j As Integer = 1 To 5
                    sCount = i.ToString + j.ToString
                    moActivesheet.Cells(i, j).value = sCount
                Next
            Next
        Else

            MsgBox("already open")

        End If

    Catch ex As Exception

    End Try


End Sub
Private Declare Auto Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As IntPtr, _
          ByRef lpdwProcessId As Integer) As Integer


Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
    'Dim oclass As Collection
    'Try
    '    Showobj.Enabled = True
    '    oclass = New Collection
    '    oclass.moExcelApplication = moExcelApplication
    '    oclass.moExcelWorkBook = moExcelWorkBook
    '    oclass.moCurrentRange = moCurrentRange
    '    oclass.CloseExcelFile()


    'Catch ex As Exception

    'End Try

    Try
        If (Not moExcelWorkBook Is Nothing) Then
            If (Not moExcelApplication Is Nothing) Then
                moExcelApplication.DisplayAlerts = False
                moExcelWorkBook.Nothing() = True

            End If
        End If

        If (Not moExcelApplication Is Nothing) Then
            moExcelApplication.DisplayAlerts = True
            moExcelWorkBook.Nothing() = True

        End If

    Catch ex As Exception

    Finally

        If moExcelApplication.Visible = True Then
            moExcelWorkBook.Close(False)
        Else
            MsgBox("Excel already closed")
        End If
        recordclear(moCurrentRange)
        recordclear(moActivesheet)
        recordclear(moWorkSheets)
        recordclear(moExcelWorkBook)
        recordclear(moWorkBooks)
        moExcelApplication.Quit()
        recordclear(moExcelApplication)
        System.Threading.Thread.Sleep(500)
        MessageBox.Show("Excel Closed")
        Cancel.Enabled = False
    End Try



End Sub




Private Sub OpenExcelFile()
    Try
        moExcelApplication = New Excel.Application
        moWorkBooks = moExcelApplication.Workbooks

        If Not (moExcelApplication Is Nothing) Then
            If Not moWorkBooks Is Nothing Then
                moExcelWorkBook = moWorkBooks.Open("C:\Documents and Settings\anand\Desktop\amit2.xlsx")
                moExcelApplication.Visible = True
            End If
        End If
    Catch ex As Exception
    End Try
End Sub
Function isopen()

    On Error Resume Next
    moExcelWorkBook.Open("C:\Documents and Settings\anand\Desktop\amit2.xlsx")
    If Not moExcelWorkBook Is Nothing Then
        moExcelWorkBook.Nothing() = False
        On Error GoTo -1
        If moExcelWorkBook.ConnectionsDisabled = True Then
            Return 1
        Else
            Return 0
        End If
    Else 'It is open
        moExcelWorkBook = Nothing
        On Error GoTo 0
        Return 1
    End If

End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Showobj.Enabled = True
    Cancel.Enabled = False
End Sub

Private Sub recordclear(ByVal o As Object)
    Try
        Do Until _
        System.Runtime.InteropServices.Marshal.ReleaseComObject(o) <= 0
        Loop
    Catch

    Finally
        o = Nothing
    End Try


End Sub
End Class
1
 

Author Comment

by:RIAS
ID: 41799461
Thanks,

 xlApp.Workbooks.get_Item ---error not a member of workbooks?
0
 
LVL 7

Expert Comment

by:D Patel
ID: 41799486
usually it can be get solved by cleaning and rebuilding the application
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question