0

特定のワークブックが開いているかどうかを確認する方法を見つけようとする助けを探しています。Windows フォームにボタンがあり、押すと Excel が開き、特定のワークシートに移動します。ただし、ユーザーがその特定のブックを既に開いている可能性があるという問題に遭遇したので、Excel の新しいインスタンスを作成してブックを再度開くのではなく、そのブックが開いているかどうかを確認し、開いている場合は次に進みます。選択したワークシートに。

これが私がこれまでに持っているコードです:

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
    xlBook = xlApp.Workbooks.Open("F:\Test Environment\Compensation Workbook\Compensation Workbook\bin\Debug\2011.1004.Compensation Template.xlsx")

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

    Me.Close()
End Sub
4

4 に答える 4

1

次のように、ワークブックが既に開いているかどうかを確認する関数を作成してみてください。

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

次に、次のようにコードで使用できます。

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
于 2013-09-01T03:32:50.250 に答える
0

jst この作品はちゃんとチェックしてね

Microsoft.Office.Interop をインポートします

パブリック クラス Form1 プライベート moExcelApplication As Excel.Application プライベート moExcelWorkBook As Excel.Workbook プライベート moWorkBooks As Excel.Workbooks プライベート moActivesheet As Excel.Worksheet プライベート moWorkSheets As Excel.Sheets プライベート 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

クラス終了

于 2013-11-30T11:10:59.317 に答える
0

Microsoft.Office.Interop パブリック クラス コレクションをインポートします

Private moExcelApplication As Excel.Application
Private moExcelWorkBook As Excel.Workbook
Private moExcelWorkBook1 As Excel.Workbook
Private moWorkBooks As Excel.Workbooks
Private moActivesheet As Excel.Worksheet
Private moCurrentRange As Excel.Range



Public Function CloseExcelFile()
    Dim bprocess As Boolean
    Try
        bprocess = False
        If (Not moExcelWorkBook Is Nothing) Then
            If (Not moExcelApplication Is Nothing) Then
                moExcelApplication.DisplayAlerts = False
                bprocess = True
            End If
        End If

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

        End If

    Catch ex As Exception

    Finally
        recordclear(moCurrentRange)
        moExcelWorkBook.Close(False)
        recordclear(moExcelWorkBook)
        'moExcelApplication.Quit()
        recordclear(moExcelApplication)

        'System.Threading.Thread.Sleep(500)
        'MessageBox.Show("Excel Closed")
        CloseExcelFile = bprocess

    End Try
End Function
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


Public Function openExcel()
    Dim bprocess As Boolean
    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

    Return bprocess

End Function


Public 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

クラス終了

于 2013-11-30T11:07:25.783 に答える