Solved

Damaged pdf when converting Excel workbook using PDFCreator

Posted on 2008-06-19
7
682 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DBF to ... Converter 5 43
Exit the loop 4 35
C#.NET and microsoft certification. 3 34
remove upper case characters in excel 4 15
A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now