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
クラス終了