Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

fire a excel button click event from vb.net

I need some help!!!

I am trying to call a commandbutton1_click() sub defined in excel from vb. I need to process any of the incoming excel files and have the vb application behave like someone were pushing the button...

I thought I could call it like I would call an excel macro.. but its saying Macro not found! :confused: This is what I have so far

Sub ProcessFiles(ByVal ProcessFile As String)
Dim XLApp As Object
Dim XLBook As Object
Dim VBComp As Object
Dim SheetName As String
SheetName = "'Sheet1$'"
XLApp = CreateObject("Excel.Application")
XLBook = XLApp.workbooks.open(ProcessFile)
XLApp.run(ProcessFile & "!" & SheetName & "!CommandButton1.Click()")
VBComp = XLBook.VBProject.VBComponents("Module1")
XLBook.VBProject.VBComponents.Remove(VBComp)
VBComp = Nothing
XLBook.Save()
XLApp.Quit()
XLBook = Nothing
XLApp = Nothing
End Sub

Any help is greatly appreciated.

Thank you
[1124 byte] By [skw2007] at [2007-11-11 10:09:17]
# 1 Re: fire a excel button click event from vb.net
This syntax works for me:

Sub XLTEST(ByVal ProcessFile As String)
Dim XLApp As New Excel.Application
Dim XLBook As Excel.Workbook = XLApp.Workbooks.Open(ProcessFile)
Try
Dim SheetName As String
SheetName = "Sheet1"
XLApp.Visible = True ' for debugging
XLApp.Run(SheetName & ".CommandButton1_Click")
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
XLBook.Close()
XLApp.Quit()
XLBook = Nothing
XLApp = Nothing
End Sub
joewmaki at 2007-11-11 20:48:30 >