Solved

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

Posted on 2014-03-11
7
3,951 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 26

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 26

Expert Comment

by:MacroShadow
ID: 39920441
Please show the code you are using.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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.

706 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

19 Experts available now in Live!

Get 1:1 Help Now