PC Review


Reply
Thread Tools Rate Thread

Create command button with macro in a Excel using C#

 
 
Senthil
Guest
Posts: n/a
 
      28th Dec 2006
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

 
Reply With Quote
 
 
 
 
=?Utf-8?B?TmVoYSBHdXB0YQ==?=
Guest
Posts: n/a
 
      28th Dec 2006
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
>
>

 
Reply With Quote
 
Senthil
Guest
Posts: n/a
 
      11th Jan 2007
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.Interop.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.AddFromString(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
> >
> >


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to create a command button, then assign macro to it in excel =?Utf-8?B?a3NoYWhlZW4=?= Microsoft Excel Misc 3 24th Jun 2008 01:03 PM
Create a command button in Excel using C# Senthil Microsoft Excel Programming 0 27th Dec 2006 01:52 PM
how can a create a print command button in excel =?Utf-8?B?dmVsY3Jvd2UxMQ==?= Microsoft Excel Programming 8 12th May 2006 04:39 PM
How do i create a command button or macro to exit Microsoft Excel =?Utf-8?B?cmFq?= Microsoft Excel Worksheet Functions 1 30th Apr 2005 02:17 PM
Can I run a macro from a command button in Excel? =?Utf-8?B?amFjY2hvcHM=?= Microsoft Excel Programming 3 16th Feb 2005 06:18 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:37 AM.