472,096 Members | 1,198 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

Using vb.net to dynamically create excel activeX controls

Using VB.net, i want to dynamically create a workbook and add a command botton to one of the sheets. how would i do this. I see example using VBA but i am not able to translate them to Vb.net. Also, after having created the button, how would I be able to defines an event for it (such as the click event). Thanks
Mar 9 '07 #1
2 5433
807 Expert 512MB
This should get you started. HTH --Sam
Note -- Before you start, add an Excel Reference to your solution: Solution Explorer - right-click on References - Add - COM Tab - MS Excel Obj Lib. Also, before you start, open Excel, Tools-Macros-Security-Trust Tab-Trust Access to Visual Basic Project.
Expand|Select|Wrap|Line Numbers
  1.         Dim xlApp As Excel.Application
  2.         Dim xlBook As Excel.Workbook
  3.         Dim xlSheet As Excel.Worksheet
  4.         Dim xlButton As Excel.OLEObject
  5.         Dim iStartLine As Long
  6.         xlApp = New Excel.Application
  7.         xlApp.Visible = True
  8.         xlBook = xlApp.Workbooks.Add
  9.         xlSheet = xlBook.ActiveSheet
  10.         xlButton = xlSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
  11.             Link:=False, DisplayAsIcon:=False, Left:=30, Top:=20, Width:=72, Height:=24)
  12.         xlButton.Name = "BtnTest"
  13.         xlButton.Object.Caption = "Press"
  14.         With xlBook.VBProject.VBComponents.Item(xlSheet.CodeName).CodeModule
  15.             iStartLine = .CreateEventProc("Click", "BtnTest") + 1
  16.             .InsertLines(iStartLine, "Msgbox ""Hi""")
  17.         End With
  18.         xlApp.VBE.MainWindow.Visible = False
Mar 9 '07 #2
Thanks for your help SammyB your code help me a lot
Jun 15 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Steve Richter | last post: by
3 posts views Thread by Rich Ulichny | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.