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