SAVEAS PDF and Word 2016 and WIndows 10
Hello.. In Word 2010 and 2013 we were able to save a word document as a PDF. Now along comes 2016 and it no longer works.
This is my function which works great for 2010 and 2013.
Function Convert_To_PDF() As Boolean
Dim NewFileName As String
Dim fileFormat As String
oWord = CreateObject("Word.Application")
If ExtType = "DOCX" Then
NewFileName = PathSaveTo.Replace(".docx", ".pdf")
Else
NewFileName = PathSaveTo.Replace(".doc", ".pdf")
End If
fileFormat = Word.WdSaveFormat.wdFormatPDF
oDoc = oWord.Documents.Open(PathIn)
Dim newdoc As Word.Document
newdoc = oDoc
newdoc.SaveAs2(NewFileName, Word.WdSaveFormat.wdFormatPDF)
oWord.ActiveDocument.Close(False)
oWord.Quit()
PathSaveTo = NewFileName
Return True
End Function
What's up with 2016? Is there a work-a-round that will function with 2010 and 2013 AND 2016 ??
Thank you
gollnick
Re: SAVEAS PDF and Word 2016 and WIndows 10
Quote:
Originally Posted by
gollnick
it no longer works.
Can you elaborate? What actually does happen? That is always relevant information. Have you consulted the relevant documentation for the Word 2016 object model?
Re: SAVEAS PDF and Word 2016 and WIndows 10
Quote:
Originally Posted by
jmcilhinney
Can you elaborate? What actually does happen? That is always relevant information. Have you consulted the relevant documentation for the Word 2016 object model?
Opps..sorry... it just goes out to lunch and never returns to the next line of code. From what I have gleaned from the 2016 documents the saveas to a pdf is no longer supported... ??
Gollnick
Re: SAVEAS PDF and Word 2016 and WIndows 10
I don't really use Office Interop myself so all I can tell you is that Word itself (I'm using Office 365 but I would expect Office 2016 to be the same) does still support saving a document to a PDF file.
I searched for "word 2016 interop save pdf" and one of the results I found was this:
https://www.codeproject.com/question...-pdf-in-csharp
One of the solutions there does this:
csharp Code:
wordDocument.ExportAsFixedFormat(@"D:\desktop\DocTo.pdf", WdExportFormat.wdExportFormatPDF);
You might try that ExportAsFixedFormat method instead of SaveAs2. It doesn't mention versions and it was posted some time ago (before 2016), but it may be worth a try.
Re: SAVEAS PDF and Word 2016 and WIndows 10
By the way, you should use the Path class to manipulate file and folder paths. Path.ChangeExtension is one method that is relevant to you.
Re: SAVEAS PDF and Word 2016 and WIndows 10
This page:
https://www.ryadel.com/en/programmat...et-c-doc-docx/
says this:
Quote:
Here’s a brief example showing what you can do:
csharp Code:
// NS alias to avoid writing the required namespace all the time
using word = Microsoft.Office.Interop.Word;
// [...]
Application app = new word.Application();
Document doc = app.Documents.Open(filePath);
doc.SaveAs2("path-to-pdf-file.pdf", word.WdSaveFormat.wdFormatPDF);
doc.Close();
app.Quit();
Alternatively, if you don’t like the SaveAs2 method, you can use the ExportAsFixedFormat() method instead and achieve a nearly identical result:
csharp Code:
doc.ExportAsFixedFormat(tmpFile, WdExportFormat.wdExportFormatPDF);
so it looks like you should be able to call ExportAsFixedFormat, but then it looks like you should be able to call SaveAs2 as well. That page is dated 2017 so should be relatively current.
Re: SAVEAS PDF and Word 2016 and WIndows 10
Recording a macro whilst saving as PDF in Word 2016 produced this:
Code:
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
"C:\Users\Paul\Desktop\Test1.pdf", ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=True, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent, _
IncludeDocProps:=True, KeepIRM:=True, CreateBookmarks:= _
wdExportCreateNoBookmarks, DocStructureTags:=True, BitmapMissingFonts:= _
True, UseISO19005_1:=False
ChangeFileOpenDirectory "C:\Users\Paul\Desktop\"
... so something like post #4 may be near the mark.