jeck09nt > 24-04-19, 11:51 PM
Sub PrintWordDoc(sFile As String, sSavePath As String)
Dim oApp As Object
Dim oDoc As Object
Dim sFileName As String
Dim bAppAlreadyOpen As Boolean
bAppAlreadyOpen = True
'Get an instance of word to work with
'See if Word is already running
On Error Resume Next
Set oApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
'Launch a new instance of Word
Err.Clear
On Error GoTo Error_Handler
Set oApp = CreateObject("Word.Application")
bAppAlreadyOpen = False
End If
'Determine the Word doc filename without the path or extension
sFileName = Right(sFile, Len(sFile) - InStrRev(sFile, "\"))
sFileName = Left(sFileName, InStr(sFileName, ".") - 1)
'Ensure the path has the final \
If Right(sSavePath, 1) <> "\" Then sSavePath = sSavePath & "\"
'Open the document
Set oDoc = oApp.Documents.Open(sFile)
'Print the document as a PDF
oDoc.ExportAsFixedFormat sSavePath & sFileName & ".pdf", 17
Error_Handler_Exit:
On Error Resume Next
'Close the Document
oDoc.Close False
Set oDoc = Nothing
If bAppAlreadyOpen = False Then oApp.Quit
Set oApp = Nothing
Exit Sub
Error_Handler:
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: PrintWordDoc" & vbCrLf & _
"Error Description: " & Err.Description _
, vbOKOnly + vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Sub
ongke0711 > 25-04-19, 12:58 PM
ledangvan > 25-04-19, 02:09 PM
jeck09nt > 25-04-19, 03:52 PM