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
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