By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,510 Members | 1,815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,510 IT Pros & Developers. It's quick & easy.

Shared addin for excel 2003, Can we add button on excel sheet

P: 18
hi guys,

this is first posting on The Scripts network.
Guys,
i have built a shared addin for Excel 2003 using visual studio 2005 and C#. Till adding command bar and menu items, it was okay.

But now i want to add typical button on excel sheet programmatically, but nothing seems to working.
Is it possible that i can add typical button controls on excel sheet from program.
Apr 19 '07 #1
Share this Question
Share on Google+
2 Replies


SammyB
Expert 100+
P: 807
Should be possible. Here's how I did it in Excel VBA:
Expand|Select|Wrap|Line Numbers
  1. Sub aBook()
  2. 'This Macro creates a workbook, a button, and a code module
  3. Dim wb As Workbook
  4. Dim ws As Worksheet
  5. Dim s As Variant
  6. Dim btn As OLEObject
  7. Dim lLines As Long
  8. ' Hide VBE Window = No screen flashes
  9.     Application.VBE.MainWindow.Visible = False
  10. ' Create a new Workbook with one worksheet, Cmds
  11.     Set wb = Workbooks.Add
  12.     Set ws = Worksheets.Add
  13.     ws.Name = "Cmds"
  14.     Application.DisplayAlerts = False
  15.     For Each s In wb.Worksheets ' Delete the rest of the worksheets
  16.         If s.Name <> "Cmds" Then s.Delete
  17.     Next s
  18.     Application.DisplayAlerts = True
  19.  
  20. ' Create a button
  21.     Set btn = ws.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
  22.                 Left:=50, Top:=25, Width:=75, Height:=20)
  23.     btn.Name = "cmdWork"
  24.     btn.Object.Caption = "Press"
  25.  
  26. ' Find the associated code module and create the click event handler
  27.     For Each s In wb.VBProject.VBComponents
  28.         If s.Name = ws.CodeName Then
  29.             With s.CodeModule
  30.                 lLines = .CountOfLines
  31.                 .InsertLines lLines + 1, "Sub cmdWork_Click()"
  32.                 .InsertLines lLines + 2, "    MsgBox ""Back to Work!"""
  33.                 .InsertLines lLines + 3, "End Sub"
  34.             End With
  35.             Exit For
  36.         End If
  37.     Next s
  38. End Sub
Obviously, you have some translation to do.

But, I have had a lot of trouble trying to do the stuff you have already done. Can you outline the steps to creating the Addin with C# 2005? I want to add a dialog box. I could do it with VB6, but am stuck with .Net.
Apr 19 '07 #2

P: 18
thanx frnd
its working, just need little translation.

As far as creating addin in C# is concerned,
there are two ways
1. VSTO addin
2. Shared addin

VSTO is future but still little still not support all office applications like access.

main steps for creating shared addin

1 .In the New Project dialog box, expand Other Projects under Project Types, select Extensibility Projects, and then select the Shared Add-in template.
2 . Type xyz as the name of the add-in and then click OK.
3. When the Extensibility Wizard appears, follow these steps:
4. select Create an Add-in using Visual C#, and then click Next.
5. select the any host applications, and then click Next:

Automatically it wil generate a lot of code for u.
The class of interest is Connect class

add following reference in connect class

using Core = Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;
using System.Windows.Forms;

I have just given here is just abstract description


these are two links to gud one to start with
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q302901
http://msdn2.microsoft.com/en-us/library/aa663367.aspx

just reply if there is any problem
Apr 20 '07 #3

Post your reply

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