PDA

View Full Version : Open and close a PDF file with native program



gmaxey
01-26-2015, 03:06 PM
Hi,

I am working on a process and there is a small piece I can't work out. Basically, need to 1) save a Word document in .PDF format, 2) Close the Word document, 3) open the newly created .PDF file in its native application (e.g., Abobe), then 4) Close the PDF file and save changes if prompted.

I can work out the first three steps, but stumped on th fourth. Thanks.



Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long



Public Function OpenWithDefaultProgram(ByVal WhichFilePath As String, _
Optional sParams As String = "", _
Optional sStartIn As String = vbNullString, _
Optional lngOpenMode As Long = 1) As Long
OpenWithDefaultProgram = ShellExecute(0, vbNullString, WhichFilePath, sParams, sStartIn, lngOpenMode)
lbl_Exit:
Exit Function
End Function

Sub Test()
Dim strDocName As String
Dim oDoc As Word.Document
Set oDoc = ActiveDocument
'Develop new file name.
strDocName = Left(oDoc.FullName, Len(oDoc.FullName) - 18)
'Save file as a PDF.
oDoc.ExportAsFixedFormat OutputFileName:=strDocName, ExportFormat:= _
wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
'Close the Word document
oDoc.Close wdDoNotSaveChanges
'I can open the PDF in the default program.
OpenWithDefaultProgram strDocName & ".pdf"
'Now I need to save and close the PDF file. Can anyone tell me how?

End Sub

gmayor
01-26-2015, 11:33 PM
Changing the line to

oDoc.ExportAsFixedFormat OutputFileName:=strDocName, 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

will open the file in its default application. You don't need the extra function for that.

What however is the point of then closing and saving it from that application. It is already saved?

I suspect you are only going to have this level of interaction if you know what the default application is and whether it is programmable. Most are not.

gmaxey
01-27-2015, 08:04 AM
Graham,

Yes, I was aware of that. For his own reasons, the end user is specific that the created.pdf be closed, then close the word document, then re-open an immediately close the .PDF. Something about Adobe (his .pdf application) internally updating links or something. I've not been able speak with him other than e-mail and I'm just not that clear on what his issue is.

Maybe if I could just get the files converted and saved, and get a nudge on how to create a batch process to open then close and save each .pdf file in folder Adobe, that would help. Thanks.