473,390 Members | 1,112 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,390 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 17684
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.