(31-05-17, 08:03 PM)alma2 كتب : http://www.siddharthrout.com/2012/03/20/...-in-vb-net
السلام عليكم اخي الحبيب
نفذت الكود التالي ولكن ظهرت رسالة خطأ كالتالي
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Sub RunMe()
MsgBox("Called from VB.net Client", vbInformation, "Demo to run Excel macros from VB.net")
End Sub
Sub ShowMsg(ByVal msg As String, ByVal title As String)
MsgBox(msg, vbInformation, title)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'~~> Define your Excel Objects
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
'~~> Start Excel and open the workbook.
xlWorkBook = xlApp.Workbooks.Open("C:\book1.xlsm")
'~~> Run the macros.
xlApp.Run("RunMe")
'~~> Clean-up: Close the workbook and quit Excel.
xlWorkBook.Close(False)
'~~> Quit the Excel Application
xlApp.Quit()
'~~> Clean Up
releaseObject(xlApp)
releaseObject(xlWorkBook)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'~~> Define your Excel Objects
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
'~~> Start Excel and open the workbook.
xlWorkBook = xlApp.Workbooks.Open("C:\book1.xlsm")
'~~> Run the macros.
xlApp.Run("ShowMsg", "Hello from VB .NET Client", "Demo 2nd Button")
'~~> Clean-up: Close the workbook and quit Excel.
xlWorkBook.Close(False)
'~~> Quit the Excel Application
xlApp.Quit()
'~~> Clean Up
releaseObject(xlApp)
releaseObject(xlWorkBook)
End Sub
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
End Class