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