vb.net cannot read excel sheet if sheet is open first

808 views Asked by At

I have workbook that is open before my form is loaded.

using vba in excel, this code works without a flaw:

 wbListPath = "C:\WAREHOUSE CONTROL ----- DO NOT DELETE\"
 wbListName = "WAREHOUSE CONTROL FORM ----- DO NOT DELETE.xlsm"


 Set wbList = Application.Workbooks("WAREHOUSE CONTROL FORM ----- DO NOT DELETE.xlsm")

If Not BookOpen(wbList.Sheets("Directories").Cells(15, 3)) Then                    ' inventory master
    wb2 = Workbooks.Open(wbList.Sheets("Directories").Cells(15, 2) & wbList.Sheets("Directories").Cells(15, 3))    'Cambridge Master
 Else
     wbLkup = wbList.Sheets("Directories").Cells(15, 3).Text
     Set wb2 = Application.Workbooks(wbLkup)
 End If




Function BookOpen(strBookName As String) As Boolean
    Dim oBk As Workbook
    On Error Resume Next
    Set oBk = Workbooks(strBookName)
    On Error GoTo 0
    If oBk Is Nothing Then
        BookOpen = False
    Else
        BookOpen = True
    End If
End Function

switching to vb.net, i cannot get any code to work to detect a workbook that has been open previous to running form1, the closest i could find is this:

Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
     Dim WorkBookNames() As String = {TextBox1.Text, TextBox1.Text & "  [Compatibility Mode]", "Microsoft Excel - 001 Phone List  [Compatibility Mode]"}
    exPhone = ChkNOpenWB(exPhone, WorkBookNames, TextBox12.Text)
    excelApp.Visible = True

End Sub

Function ChkNOpenWB(bookName As Workbook, WorkBookNames() As String, path As String) As Workbook
    Dim openFlag As Boolean = True
    For Each p As System.Diagnostics.Process In System.Diagnostics.Process.GetProcesses()
        If p.ProcessName = "EXCEL" Then
            For Each excelFile As String In WorkBookNames
                If p.MainWindowTitle.Contains(excelFile) Then
                    bookName = GetObject(TextBox12.Text & "\" & TextBox1.Text)
                    bookName.RefreshAll()
                    openFlag = False
                End If
            Next
        End If
    Next
    If (openFlag) Then
        bookName = excelApp.Workbooks.Open(TextBox12.Text & "\" & TextBox1.Text, ReadOnly:=False)
        bookName.Activate()
    End If
    Return (bookName)
End Function
2

There are 2 answers

0
APrough On

You can also use Marshalling. This looks to see if the workbook is already open. If it is not, it opens it. If it is already open, then it assigns the workbook to the variable.

    Dim exApp AS Excel.Application = New Excel.Application
    Dim wb as excel.Workbook
    Dim exSheet As Excel.Worksheet = Nothing

    exApp.Visible = True

    wb = System.Runtime.InteropServices.Marshal.BindToMoniker(FileNamePath)
    exApp = wb.Parent

    'lets see it
    exApp.Visible = True
    exApp.Windows(1).Visible = True

    Dim tSheet As Excel.Worksheet = exApp.ActiveWorkbook.Sheets.Item(1)
0
David Sdot On

You can just check if the file can be be opened for writing. If opened in Excel the check will fail.

Public Shared Function FileInUse(ByVal Filename As String) As Boolean

    Dim thisFileInUse As Boolean = False
    If System.IO.File.Exists(Filename) Then
        Try
            Using f As New IO.FileStream(Filename, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
                thisFileInUse = False
            End Using
        Catch
            thisFileInUse = True
        End Try
    End If
    Return thisFileInUse

End Function