Opening up a pdf file in excel (using PDF-XChange Editor)

RawlinsCross

Active Member
Joined
Sep 9, 2016
Messages
437
Very small code... opens the executable program (i.e. C:\Program Files\Tracker Software\PDF Editor\PDFXEdit.exe) but not the file.


Sub RunPDFWithExe()

MyPath = "C:\Program Files\Tracker Software\PDF Editor\PDFXEdit.exe"
MyFile = "C:\Users\jhalfyard\Desktop\Daily Production Report - January 4, 2017.pdf"
Shell MyPath & " " & MyFile, vbNormalFocus
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Shell does not understand strings with space characters as part of program or file name. Encapsulate those strings with quotes.

e.g.
Code:
Sub RunPDFWithExe()
  Dim MyPath$, MyFile$, q$
  q = """"
  MyPath = "C:\Program Files\Tracker Software\PDF Editor\PDFXEdit.exe"
  MyFile = "C:\Users\jhalfyard\Desktop\Daily Production Report - January 4, 2017.pdf"
  Shell q & MyPath & q & " " & q & MyFile & q, vbNormalFocus
End Sub
 
Upvote 0
Thanks Kenneth... worked like a charm.

I'd like to introduce another complexity. From another code I found here, I want the code to access a directory and choose the most recent modified file. Your code worked, this other code works (using it to call up latest outlook msg files) but when I try to combine the two it doesn't work. PDFXEDIT.exe loads but again no file appears.

Here's the new code:


Sub RunPDFWithExe()
Dim MyPath$, q$, strFilename$
Dim myFolder
Dim dteFile As Date
Dim objFile As File

q = """"
MyPath = "C:\Program Files\Tracker Software\PDF Editor\PDFXEdit.exe"

'set path for files - change for your folder
Const myDir As String = "C:\Users\jhalfyard\Desktop"
'set up filesys objects
Set FileSys = New FileSystemObject
Set myFolder = FileSys.GetFolder(myDir)


'loop through each file and get date last modified. If largest date then store Filename
dteFile = DateSerial(1900, 1, 1)
For Each objFile In myFolder.Files
If objFile.DateLastModified > dteFile Then
dteFile = objFile.DateLastModified
strFilename = objFile.Name
End If
Next objFile


'Call the file

Shell q & MyPath & q & " " & q & strFilename & q, vbNormalFocus


Set FileSys = Nothing
Set myFolder = Nothing

End Sub
 
Upvote 0
As a new user, I recommend:
1. Start a new thread for the new problem.
2. Paste code between code tags.
e.g.
(code)MsgBox "Hi"(/code)
but replace ()'s with []'s.

I am not sure what the exe does with a passed file. Your first example was a pdf while the 2nd is maybe an msg file? If it is doing something in the background, you may wind up needing a shell execute wait rather than just shell. Shell() alone may end before some task was completed and following VBA code executes.

In any case, you probably need to concatenate the folder as prefix to strFilename or just get the full path via FSO.
 
Last edited:
Upvote 0
Okay, thanks for the tips. Will try to play around with the code to get it to work. And yes, the second code is a part of one I got from this forum on how to search a folder and call the latest Outlook msg file. It works very nicely.

Code:
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
 
Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWMINIMIZED As Long = 2
 
Sub OpenEmailProduction_Click()
    
     
    Dim FileSys As FileSystemObject
    Dim objFile As File
    Dim myFolder
    Dim strFilename As String
    Dim dteFile As Date
    
        
    'set path for files - change for your folder
    Const myDir As String = "C:\users\jhalfyard"
    
    
    'set up filesys objects
    Set FileSys = New FileSystemObject
    Set myFolder = FileSys.GetFolder(myDir)
        
    
    'loop through each file and get date last modified. If largest date then store Filename
    dteFile = DateSerial(1900, 1, 1)
    For Each objFile In myFolder.Files
        If objFile.DateLastModified > dteFile Then
            dteFile = objFile.DateLastModified
            strFilename = objFile.Name
        End If
    Next objFile
    
    'Call the file
    ShellExecute 0, "Open", strFilename, "", "C:\users\jhalfyard", SW_SHOWNORMAL
    
    Set FileSys = Nothing
    Set myFolder = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top