473,326 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Create command button with macro in a Excel using C#

Hi All
I need to create an Excel report and create a command button and
have to run a macro on the
click event that will print all the pages in the Excel workbook.
I am able to create the report from the database but I am not sure how
do I put a command button and associate a macro with that using C#. Can
anyone help me??

Thanks in advance.
Senthil

Dec 28 '06 #1
2 17671
Hi

I dont know much myself, but I found this example while looking for
something. I was having some problems running the code, see if it helps you
in any way.

http://discuss.develop.com/archives/...T=0&P=6671&D=0

I am actually looking for a way to add a button to an excel sheet using c#
and then handle the click event using c# code itself, without using a macro.
Any help would be greatly appreciated!!

Neha

"Senthil" wrote:
Hi All
I need to create an Excel report and create a command button and
have to run a macro on the
click event that will print all the pages in the Excel workbook.
I am able to create the report from the database but I am not sure how
do I put a command button and associate a macro with that using C#. Can
anyone help me??

Thanks in advance.
Senthil

Dec 28 '06 #2
Hi all
I managed to create an Excel report using a console
application in C#. I wrote a macro that prints all pages on clicking a
command button.
The code that does this part is

Console.WriteLine("Adding button to the workbook for
Printing..");
Shape btnPrint =
sheet1.Shapes.AddOLEObject("Forms.CommandButton.1" , _missing, _missing,
_missing, _missing,
_missing, _missing, 50, 200, 60, 22);
btnPrint.Name = "btnPrint";
OLEObject sheetBtn =
(OLEObject)sheet1.OLEObjects(btnPrint.Name);
sheetBtn.Object.GetType().InvokeMember("Caption",
System.Reflection.BindingFlags.SetProperty, null, sheetBtn.Object, new
object[] { "Print All" });
Console.WriteLine("Added Button workbook for Printing..");
//Code to add macro to the Worksheet
//This macro prints all the sheets in the report in
Landscape format with a zoom of 85
Console.WriteLine("Adding macro to the workbook for
Printing..");

VBComponent oModule =
book.VBProject.VBComponents.Add(Microsoft.Vbe.Inte rop.vbext_ComponentType.vbext_ct_StdModule);
string sCode = "sub VBAPrint()\r\n" +
"\tDim sht As Worksheet\r\n" +
"\tFor Each sht In ActiveWorkbook.Sheets\r\n" +
"\t\tsht.PageSetup.Zoom = 85\r\n" +
"\t\tsht.PageSetup.Orientation =
xlLandscape\r\n" +
//"\t\tsht.PrintOut\r\n" +
"\t\tNext\r\n" +
"\t\tWorksheets.PrintOut\r\n" +
"end sub";
oModule.CodeModule.AddFromString(sCode);
sCode = "Sub btnPrint_Click()\r\n" +
" VBAPrint\r\n" +
"End Sub";

book.VBProject.VBComponents.Item(1).CodeModule.Add FromString(sCode);
Console.WriteLine("Added macro to the workbook for
Printing..");

Downloaded and installed PIAs for Office 2003.. was able to create a
report with the macro in it.
But when I tried running the console application from the Server macine
through xp_commandshell the excel does not get created. When I
commented out the macro creation part it gets created without any
problems. I tried installing the PIAs in the server but it does not get
installed. It is not giving any errors. It justs quits without any
error message..
The Server has Office 2003 Professional with Windows XP on it.

Please let me know how to proceed.

Regards
Senthil
Neha Gupta wrote:
Hi

I dont know much myself, but I found this example while looking for
something. I was having some problems running the code, see if it helps you
in any way.

http://discuss.develop.com/archives/...T=0&P=6671&D=0

I am actually looking for a way to add a button to an excel sheet using c#
and then handle the click event using c# code itself, without using a macro.
Any help would be greatly appreciated!!

Neha

"Senthil" wrote:
Hi All
I need to create an Excel report and create a command button and
have to run a macro on the
click event that will print all the pages in the Excel workbook.
I am able to create the report from the database but I am not sure how
do I put a command button and associate a macro with that using C#. Can
anyone help me??

Thanks in advance.
Senthil
Jan 11 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: brazilnut52 | last post by:
I am going to outline the steps I go through to produce the problem. Hopefully this will help you understand the problem better I have created a simple COM DLL in .NET by using the COM class...
3
by: VbUser25 | last post by:
hi.. i have a form i accept some user inputs in the form. there is a link on the form from where i open the excel file in the same browser (not in a new page...simply using a href. i am also...
2
by: surjs | last post by:
Hi I'm wanting some help on how to create a message that automatically pops up on opening an access form, that says for example ' Remember to ......'. I've been able to this by assigning a...
2
by: Darren | last post by:
Hi, I have a command button which has a macro running in it. The macro on the click event, runs to ensure that certain values in a form are valid, (e.g. the textboxes are not null) and...
9
by: John Brock | last post by:
I am trying to create a workbook where one of the worksheets contains an AutoFiltered table. It looks like it should be simple -- what I am doing is this: rng = ws.Range("MyTableRange")...
10
by: Steve | last post by:
I am trying to create a DLL in Visual Studio 2005-Visual Basic that contains custom functions. I believe I need to use COM interop to allow VBA code in Excel 2002 to access it. I've studied...
3
by: SAH | last post by:
This code:- ActiveSheet.Shapes("CommandButton1").Delete Gets rid of the Button OK but it leaves the Code in the Command Buttton1_Click Subroutine and I should like to clear that too. Can I do...
0
by: sclinkenbeard | last post by:
I want to use a command button in Excel and have the VBA macro to open a PDF document. Does anyone know the VBA code to do so? It is easy opening word doc and another excel program but not sure how...
4
by: mld01s | last post by:
Hi all!! I need help, I have been stuck for a few days on this one. I am trying to open an excel table from a command button in Access. The excel table has an auto_open macro, that is supposed to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.