Does this work?
Option Explicit
Private Const mstrcPATH_SEPARATOR As String = "\"
Public Sub Test()
Dim strErrMsg As String
Dim strPathPDFs As String
Dim strFileNamePDF As String
Dim strFullNameReader As String
' set values
With Worksheets("Sheet1")
strPathPDFs = .Range("E12").Value2
strFullNameReader = .Range("E10").Value2
strFileNamePDF = "PO#" & " " & .Range("F6").Value & " " & .Range("G6").Value & " " & .Range("H6").Value & ".pdf"
End With
' verify exist
strErrMsg = "PDF Reader program not found at " & strFullNameReader
If Not fnblnExistsFileFolder(strFullNameReader) Then
GoTo ExitProcedure
End If
strErrMsg = "PDF output path not found at " & strPathPDFs
If Not fnblnExistsFileFolder(strPathPDFs) Then
GoTo ExitProcedure
End If
' ensure path separatored and fix for spaces when used in Shell
strPathPDFs = fnstrGetSeparatoredPath(strPathPDFs)
strFullNameReader = """" & strFullNameReader & """"
' ensure pdf exists
strErrMsg = "PDF file was not found at " & strPathPDFs & strFileNamePDF
If Not fnblnExistsFileFolder(strPathPDFs & strFileNamePDF) Then
GoTo ExitProcedure
End If
strErrMsg = vbNullString
' open Pdf
Shell strFullNameReader & " " & """" & strPathPDFs & strFileNamePDF & """", vbNormalFocus
ExitProcedure:
If LenB(strErrMsg) Then
MsgBox strErrMsg, vbExclamation
End If
End Sub
Public Function fnblnExistsFileFolder(ByRef strFullName As String) As Boolean
'adapted from function written by Ken Puls (www.excelguru.ca)
If LenB(strFullName) Then
On Error Resume Next
fnblnExistsFileFolder = LenB(Dir(strFullName, 31))
On Error GoTo 0
End If
End Function
Public Function fnstrGetSeparatoredPath(ByVal strDirPath As String, Optional ByVal blnInvert As Boolean) As String
'/ ensures folder path ends in path separator (aka trailing backslash)
'/ doesn't detect garbage input, requires a Path arg
Dim blnChange As Boolean
strDirPath = Trim$(strDirPath)
If LenB(strDirPath) Then
blnChange = ((Right$(strDirPath, 1) = mstrcPATH_SEPARATOR) = blnInvert)
If Not blnChange Then
fnstrGetSeparatoredPath = strDirPath
ElseIf Not blnInvert Then
'add trailing separator
fnstrGetSeparatoredPath = strDirPath & mstrcPATH_SEPARATOR
Else
'remove last character
fnstrGetSeparatoredPath = Left$(strDirPath, Len(strDirPath) - 1)
End If
End If
End Function
Bookmarks