Solved

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

Posted on 2014-03-11
7
4,115 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

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

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,…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

863 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

21 Experts available now in Live!

Get 1:1 Help Now