Solved

How do I save an Excel spreadsheet to PDF using VB.net

Posted on 2014-03-11
7
4,713 Views
Last Modified: 2014-03-16
This is the code I use to save a Word document to a PDF.  I need similar code for an Excel spreadsheet.  Any help will be appreciated!!

Thanks!

Dim savePath As String = "c:\ticket.pdf"
Dim saveAsFormat As Word.WdSaveFormat = Word.WdSaveFormat.wdFormatPDF
                oDoc.SaveAs(savePath, saveAsFormat, , , , , , , , , , , , , , )

If My.Computer.FileSystem.FileExists(savePath) = True Then
  System.Diagnostics.Process.Start(savePath)
Else
  MsgBox("")
End If

oWord.ActiveDocument.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
oWord.Quit()
0
Comment
Question by:Karen Wilson
[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
  • 5
  • 2
7 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39920280
Use ExportAsFixed:

oExcel.ActiveSheet.ExportAsFixedFormat( Excel.XlFixedFormatType.xlTypePDF, _
        "C:\Temp", _
        Excel.XlFixedFormatQuality.xlQualityStandard, True, True, 1, 10, False)

Open in new window

0
 

Author Comment

by:Karen Wilson
ID: 39920342
I get an error message, "Document not saved.  The document may be open, or an error may have been encountered when saving."  

I am trying different configurations to figure it out.
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39920441
Please show the code you are using.
0
How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

 

Author Comment

by:Karen Wilson
ID: 39920497
objApp.ActiveSheet.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, "C:\Temp", Excel.XlFixedFormatQuality.xlQualityStandard, True, True, 1, 10, False)
0
 

Author Comment

by:Karen Wilson
ID: 39920843
I got it to save to a PDF.  Now I can't get it to close automatically.  Here is the code:

Dim dow As String = Today.Date.ToString("s")

Dim useDate As String = dow.Remove(10)
useDate = useDate.Replace("-", "_")


Dim savePathPDF As String = "\\wstf-fs01\envr-files\5YearPlans\" & useDate & ".pdf"
Dim saveAsFormat As Excel.XlFixedFormatType = Excel.XlFixedFormatType.xlTypePDF

objBook.ExportAsFixedFormat(saveAsFormat, savePathPDF, Excel.XlFixedFormatQuality.xlQualityStandard, True, True, 1, , False, )


objBook.ActiveSheet.Close(Excel.XlSaveAction.xlDoNotSaveChanges) - this not working
objApp.Quit()
0
 

Accepted Solution

by:
Karen Wilson earned 0 total points
ID: 39921131
The fix:

Dim dow As String = Today.Date.ToString("s")

Dim useDate As String = dow.Remove(10)
useDate = useDate.Replace("-", "_")


Dim savePathPDF As String = "\\wstf-fs01\envr-files\5YearPlans\" & useDate & "_5YearPlan.pdf"

Dim saveAsFormat As Excel.XlFixedFormatType = Excel.XlFixedFormatType.xlTypePDF

objBook.ExportAsFixedFormat(saveAsFormat, savePathPDF, Excel.XlFixedFormatQuality.xlQualityStandard, True, True, 1, , True, ) - the last true opens the PDF for the user to review


Me.objApp.ActiveWorkbook.Close(SaveChanges:=False)
objApp.Quit()
0
 

Author Closing Comment

by:Karen Wilson
ID: 39932365
I have spent hours doing trial and error.  This is mine!
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

622 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