Solved

Damaged pdf when converting Excel workbook using PDFCreator

Posted on 2008-06-19
7
692 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

624 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