473,721 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 17718
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.WriteLi ne("Adding button to the workbook for
Printing..");
Shape btnPrint =
sheet1.Shapes.A ddOLEObject("Fo rms.CommandButt on.1", _missing, _missing,
_missing, _missing,
_missing, _missing, 50, 200, 60, 22);
btnPrint.Name = "btnPrint";
OLEObject sheetBtn =
(OLEObject)shee t1.OLEObjects(b tnPrint.Name);
sheetBtn.Object .GetType().Invo keMember("Capti on",
System.Reflecti on.BindingFlags .SetProperty, null, sheetBtn.Object , new
object[] { "Print All" });
Console.WriteLi ne("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.WriteLi ne("Adding macro to the workbook for
Printing..");

VBComponent oModule =
book.VBProject. VBComponents.Ad d(Microsoft.Vbe .Interop.vbext_ ComponentType.v bext_ct_StdModu le);
string sCode = "sub VBAPrint()\r\n" +
"\tDim sht As Worksheet\r\n" +
"\tFor Each sht In ActiveWorkbook. Sheets\r\n" +
"\t\tsht.PageSe tup.Zoom = 85\r\n" +
"\t\tsht.PageSe tup.Orientation =
xlLandscape\r\n " +
//"\t\tsht.PrintO ut\r\n" +
"\t\tNext\r \n" +
"\t\tWorksheets .PrintOut\r\n" +
"end sub";
oModule.CodeMod ule.AddFromStri ng(sCode);
sCode = "Sub btnPrint_Click( )\r\n" +
" VBAPrint\r\n" +
"End Sub";

book.VBProject. VBComponents.It em(1).CodeModul e.AddFromString (sCode);
Console.WriteLi ne("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
13407
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 template and by setting output to a type library (DLL). All the object does is return a string value. At this point I have not checked the option to register for COM interop in Visual Studio So I go into Excel (where I want to use the object). Go to VB...
3
3708
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 accepting input from the user in the excel. now i want to save the data in excel as well as on asp form. i can save directly thru vba code in excel to the db. but suppose the
2
16798
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 macro then a command button but need the message to be displayed as soon as the form is opened by the user. I'm using access 2003 - not an programming expert therefore would appreciate some simple instructions or a good website
2
5620
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 after the macro runs, an update query would run... the problem is that how can i ensure that the update query would only
9
13830
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") rng.AutoFilter() Unfortunately I get an exception on the second statement, with the unhelpful message:
10
8202
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 everything I can find on COM Interop and .NET. I've also tried many of the 'Walkthroughs' on the MSDN site relating to COM add-ins, .NET and Office XP but am unable to get even these working in Excel 2002 or Word 2002. I've installed the Office XP...
3
3953
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 that in the vb macro?
0
1982
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 to do so with a pdf document. Do I need to tell the macro to open Adobe first? Any assistance is helpful. Thank you.
4
14587
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 run everytime I open excel. When I navigate to the excel file, and open it, it autoruns the macro with no problems. When I go from Access hit the command, the excel table opens with no problem, but the macro does not auto run on start. Here is...
0
8840
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9367
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9215
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8007
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5981
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2130
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.