Save entire workbook as PDF Excel 2010 (C #)

In any case, you save the entire book in pdf format in excel. I found this, http://msdn.microsoft.com/en-us/library/bb407651(v=office.12).aspx , but it doesnโ€™t tell you exactly whether it saves the entire book as a pdf or just an active sheet . If there is no way to save the whole book in pdf format, will printing the entire workbook be the best option or even possible in C #? The following is what I still just need to save as a PDF so that I can send an email. Thanks for the help.

using Excel = Microsoft.Office.Interop.Excel; //Excel Reference //Gets Excel and gets Activeworkbook and worksheet Excel.Application oXL; Excel.Workbook oWB; Excel.Worksheet oSheet; //Create New Instance in Excel oXL = new Excel.Application(); oXL.Visible = true; //Open Excel Workbook oWB = oXL.Workbooks.Open(""); oWB = (Excel.Workbook)oXL.ActiveWorkbook; oSheet = (Excel.Worksheet)oWB.ActiveSheet; //Modify Excel Spreadsheet Based on Form oSheet.Cells[6, 4] = maskedTextBox1.Text; //Change Value in Cell, Cell Location [y-axis, x-axis] //Save Workbook As oWB.SaveAs(""); //Save Workbook As PDF //Close Workbook oWB.Close(""); //Quit Excel oXL.Quit(); 
+4
source share
1 answer

In 2010, you can save the entire book in PDF, making each sheet "active". It sounds strange, but if you notice the print options when you make a PDF, there is no way for a book. To get around this, open the excel file and fill in some data on 2-3 worksheets. Now hold the ctrl key and click on each other's book, then it will become a "Group".

You will notice that the name [GROUP] displayed at the top of the excel file, and now when you print the excel file, it prints the entire book.

Try it for yourself. In code, you just need to make each worksheet an active worksheet. I don't work much with the excel object model, but for this you might need to make a macro and look at the code.

I recorded a macro, and here is VBA:

Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select

It looks like you just need to store each sheet in an array, and then just

Sheets(MyArray).Select

Then all sheets will be activated and [grouped], and then you can print the printout in pdf format. Having recorded the macro, he also provided options for printing in pdf format:

 `ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _ "C:\Users\MyAccount\Desktop\test.pdf", Quality:=xlQualityStandard, _ IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _ True` 

In this case, the active sheet is your sheet group that you saved in the array.

+6
source

All Articles