| ■No100893 (ジェイド さん) に返信
> 以下のようなコードで、Excelを起動して終了するまで待っている処理を組んでおります。
こんな感じでどうでしょう?
Imports Microsoft.Office.Interop
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function GetWindowThreadProcessId(ByVal hwnd As IntPtr, _
ByRef lpdwProcessId As Integer) As Integer
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim app As New Excel.Application
Dim books As Excel.Workbooks = app.Workbooks
Dim book As Excel.Workbook = books.Open("E:\TEST\test.xlsx")
app.Visible = True
Dim hwnd As IntPtr = New IntPtr(app.Hwnd)
Dim pid As Integer
Dim threadId As Integer = GetWindowThreadProcessId(hwnd, pid)
Marshal.ReleaseComObject(book)
Marshal.ReleaseComObject(books)
Marshal.ReleaseComObject(app)
Using p = Process.GetProcessById(pid)
p.WaitForExit()
End Using
MessageBox.Show("EXCEL 終了!")
End Sub
End Class
|