Solved

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

Posted on 2014-03-11
7
4,318 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

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…
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 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.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

830 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