Solved

Damaged pdf when converting Excel workbook using PDFCreator

Posted on 2008-06-19
7
686 Views
Last Modified: 2012-05-05
I am trying to create *.pdf files from an Excel workbook using the following C# statement:

wb.PrintOut(1, wb.Sheets.Count, 1, false, "PDFCreator", true, false, FileName);

where wb is a Workbook using Microsoft.Office.Interop.Excel.

This results in a "damaged" pdf file - see attached file for error message.
What would be the solution?
Question.JPG
0
Comment
Question by:MTecho
  • 4
  • 3
7 Comments
 
LVL 45

Expert Comment

by:patrickab
ID: 21829413
MTecho,

If you use PDFCreator as a stand-alone application, does it create a valid working pdf file? Once you've established that then you can move on to other possibilities.

Patrick
0
 

Author Comment

by:MTecho
ID: 21837090
Thanks, Patrick.

Yes, it does.
0
 
LVL 45

Expert Comment

by:patrickab
ID: 21837112
MTecho,

In this statement:

wb.PrintOut(1, wb.Sheets.Count, 1, false, "PDFCreator", true, false, FileName);

how are you declaring and defining each element? Please give your actual code for

===
wb
Filename
===

Thanks

Patrick
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:MTecho
ID: 21837157
           string ReportID = "A1b";
            Microsoft.Office.Interop.Excel.Application xlApp = null;
...
                        xlApp = new Microsoft.Office.Interop.Excel.Application();
                        Workbook wb = xlApp.Workbooks.Open(Reports.Properties.Settings.Default.XLT_LOCATIONA1b, 3, false, 5, "", "", true, XlPlatform.xlWindows, null, null, false, null, false, false, XlCorruptLoad.xlNormalLoad);
                        Worksheet objsheet1 = (Worksheet)xlApp.Worksheets["All Arrivals"];
                        PivotTable Pv = (PivotTable)objsheet1.PivotTables("PivotTable1");
                        if (Pv.RefreshTable())
                        {
...
                                    wb.PrintOut(1, wb.Sheets.Count, 1, false, "PDFCreator", true, false, Reports.Properties.Settings.Default.WEB_FOLDER + (char)92 + ReportID + ".pdf");
...
                        }
==============================================
Reports.Properties.Settings.Default.XLT_LOCATIONA1b is a string setting, set by OpenFileDialog to the path for an Excel template file (*.xlt). The template file is loaded and the pivot table is refreshed
Reports.Properties.Settings.Default.WEB_FOLDER  is a string setting, set by OpenFolderDialog

Thanks

Mark
0
 
LVL 45

Accepted Solution

by:
patrickab earned 50 total points
ID: 21837454
Mark,

I do not pretend know anything about MS Interop, however it does look to be a very complicated way to do something that is really very simple. I'm not in the habit of producing pdf files from Excel so I had a look on the net and found Sonic PDF Creator 2.0. I installed it and then recorded an Excel macro to create a pdf file. All of that took a matter of minutes. The result was this short macro:

Sub print2pdf()
    Application.ActivePrinter = "Sonic PDF on Ne02:"
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:= _
        "Sonic PDF on Ne02:", Collate:=True
End Sub

I obtained Sonic PDF Creator 2.0 from:

http://www.investintech.com/downloads/sonic/download.htm

I realise you are using C#, but perhaps it might be much easier to do what you want to do from within Excel and using VBA instead.

Patrick
0
 

Author Closing Comment

by:MTecho
ID: 31469055
Thanks for your help Patrick.
The solution I would really like would enable my application to process multiple Excel files and not require any macro in the spreadsheets.
I have found some VB code that does this, but generates an error when processing the second file - have not solved it yet.
0
 
LVL 45

Expert Comment

by:patrickab
ID: 21923541
MTecho - Thanks for the points. - Patrick
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question