Convert Office files to PDF

Hello all,
We have 2 issues:
1) We have Office 2003 files we want to convert to PDF programmatically.  We'd like to use the Adobe PDF printer but have not been able to find anything for .Net programming.  I don't know if I've just been going down the wrong path in my research or what.  I'd like to get some direction for code and/or examples and information on how to do this in .Net.  
2) We also have Office 2007 files we'd like to convert to PDF programmatically.  We found both SaveAs and ExportAsFixedFormat.  I don't know which is the better one to use, if there is one.  Having said that, I have coded and tested both SaveAs and ExportAsFixedFormat for Word, Excel and PowerPoint and it all works great with the exception of the SaveAs for Excel which does not contain a FileFormat for PDF.  

Please let me know if any further information is needed.  Thank you in advance!
Who is Participating?
Kyle AbrahamsConnect With a Mentor Senior .Net DeveloperCommented:
1)  The adobe PDF printer should automatically take a print job and convert it to PDF no?  If not I know of the cutePDF writer (freeware) which does the same thing.  I think the distiller is for taking files in one directory and automatically converting them.

An easy test before coding would be to try to print to the pdf writer.  If it converts the file with no distiller then it will work the same whether a user or program does it.

With regards to saveas vs export:


It looks like the export only does PDF/XPS whereas the saveas can do a whole bunch of them.  Not sure what your requirements are regarding that.


Let me know if you have more issues/questions.
Kyle AbrahamsSenior .Net DeveloperCommented:
For 1)
Research on how to print a document to any printer.  Then just choose the Adobe PDF printer and that will take care of everything for you.

2)  Does ExportAsFixedFormat offer PDF for Excel?  If so use that one.
chilekittyAuthor Commented:
Thank you for the quick response.....
1) Won't I need the distiller?  Or does that matter?  Is that all there is to it?
2) It does not offer PDF; I have referenced Microsoft.Office.Interop for all 3 but Excel does not contain that file type.  So which procedure is best SaveAs or ExportAsFixedFormat?
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Set word = CreateObject("Word.Application")
Set docs = wdo.Documents
' Remember current active printer
Set sPrevPrinter = wdo.ActivePrinter
' Select the PDFCreator as your printer
word.ActivePrinter = "PDFCreator"
' Open the Word document
Set document = docs.Open(sMyDocumentFile)
' Print the document file to the PDFCreator
document.Close WdDoNotSaveChanges
word.ActivePrinter = sPrevPrinter
word.Quit WdDoNotSaveChanges
Kyle AbrahamsSenior .Net DeveloperCommented:
Yep, like I said, print right to the PDF printer.
chilekittyAuthor Commented:
Again...thanks for responding so quickl :)
1)Let me do a bit more research based on your suggestion; maybe I was looking too hard.
2)I will read up on these...
chilekittyAuthor Commented:
Thanks x77; I will work with what you sent
chilekittyAuthor Commented:
Ok here is where I'm at:
1) Still need help here :  You're right ged325, no distiller is needed; that's good.  But x77 where I might have a problem (please correct me if I'm wrong anyone) that the printer is specific to an Application, like word, excel, ppt - so if this is true then that would mean that I would need to make a new or active printer for each application depending on the file.  We'd like the ability to have the user select a file or multiple files or a directory and for each file convert to PDF (i.e. print to Adobe PDF). Is there a 'generic' way for doing this without the use of another 3rd party?  

2) Unless others have an opinion, Office 2007 to PDF is settled:
Thanks for the info ged325; The last link provide the information that I needed;
Use ExportAsFIxedFormat for excel to PDF...which will lead me to implement this for all Office files to PDF (docx, xlsx, pptx) to retain some standard in code.

Thanks all!
Kyle AbrahamsSenior .Net DeveloperCommented:

You could right your own app for it:
Essentially have an OpenFile dialog, then add the files to a listbox.
User clicks a process button and you can open and print each document directly to the printer.

2) (Just thought of this)  Instead of assigning the printer for the application, change the default printer.  As long as the printer exists, this will work for any application.  When you're done, simply change the default printer back and you are good to go.
chilekittyAuthor Commented:
ged325 #1- not viable at this time but would be an option if nothing else is found to work
ged325 #2- was something I was looking into earlier based on the comment by x77 (change the active printer); I can do that through an 'Application', but how can this be done outside an 'Application'?  x77 your code works, we'd just have to do this for each document/application....we'd really like something more 'generic' like ged325's  #2 option.
Does someone have some code or examples for accessing and assigning a printer and then printing out a document in .Net without going through an 'Application'?  Hope this makes sense; just started getting my feet wet in printing and .Net.
Thank you for all your help!
Kyle AbrahamsConnect With a Mentor Senior .Net DeveloperCommented:
by default anything written in .net is an application in and of itself . . . so not sure what you mean by "outside of an application"

Get default printer:

For setting the default printer:
(note you can just call     SetDefaultPrinterWinNT ("Adobe pdf") )

(then you would call the setdefault() again with the old printer name to reset)

You then just need to process files . . . but this is where I don't know how you want to go about it.

The app will need to know what files to print.  (Unless you just want to have a mini application to toggle, the printer).  If that's the case you can just have the user minimize your window (adobe PDF will be set as default) they can print whatever, then click the toggle again to reset it back to default)

chilekittyAuthor Commented:
Sorry ged325 and all; it's been a little crazy me. Let me get reaquainted with the issues again and look at your suggested sites and will get back :)
If you install PDFCREATOR, it give us samples in the install directory for MsAccess (Vba), Word, VbNet, CSharp...
In the MsAccess sample, it set and restore the Default Printer to print a report.

C:\Archivos de programa\PDFCreator\COM\MS Office\modPDFCreatorAccess.bas

With PDFCreator1
  .cStart "/NoProcessingAtStartup"
  .cOption("UseAutosave") = 1
  .cOption("UseAutosaveDirectory") = 1
  .cOption("AutosaveDirectory") = "C:\"
  .cOption("AutosaveFilename") = RepName
  .cOption("AutosaveFormat") = 0                            ' 0 = PDF
  DefaultPrinter = .cDefaultPrinter
  .cDefaultPrinter = "PDFCreator"
  DoCmd.OpenReport RepName, acViewNormal
  .cPrinterStop = False
 End With

 With PDFCreator1
  .cDefaultPrinter = DefaultPrinter
  Sleep 200
 End With

chilekittyAuthor Commented:
Thanks ged325 you provided us with the most suggestions and options to our issues.  Thanks all!!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.