Checking if PDF and saving as PDF if it isn't

m_in_spain

Board Regular
Joined
Sep 28, 2018
Messages
64
Office Version
  1. 365
Platform
  1. Windows
Hi
I have written the following code which takes a file and stores it in my temp directory ready for a later stage. Seems to work well, however, the file can be anything, doc, picture, ppt, etc etc.
this is my code:
VBA Code:
'COPIES THE REQUESTED ATTACHMENT FILE AND RENAMES IT PROJECT SPECIFIC TO C:\TEMP\makePO FOLDER
Sub CopyAttachment()
Dim FSO
Dim sFile As String
Dim sSFolder As String
Dim sDFolder As String
Dim newfilename As String
    'Ends sub if no attachment specified
    If Range("FilePath") = "" Then End
    'This is the file to be copied
    sFile = Range("attchFILE")
    'This is the source folder path
    sSFolder = Range("attchPATH")
    'This is the Save destination directory
    'Checks to see if save directory is there
        If Dir("c:\temp\makePO", vbDirectory) = "" Then
            'directory doesn't exist
            MkDir path:="C:\temp\makePO"
        Else
            'directory does exist
        End If
    'This is the destination folder path
    sDFolder = "C:\temp\makePO\"
    'Rename the copied file to Project Specific
    newfilename = Range("AttchNewName")
        'Create Object
        Set FSO = CreateObject("Scripting.FileSystemObject")
        'If file already exists then it is silently overwritten
        Application.DisplayAlerts = False
            FSO.CopyFile (sSFolder & sFile), sDFolder & newfilename, True
        Application.DisplayAlerts = True
End Sub

Is there a way I can check if the file saved is a PDF file, if it is not (i.e. any other file type) i need to print it to the same folder as a PDF with the same file name, and keep the native file there too?

Thanks, as ever, if there is any help out there!
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Since you're already using the FileSystemObject, you can make use of its GetExtensionName function. Using something like
VBA Code:
fso.GetExtensionName("C:\Temp\test.csv")
will return a string csv.

So for your case, use a string you already have to represent the full file path + name + extension, assign that to a string then check if it equals pdf.
 
Upvote 0
Since you're already using the FileSystemObject, you can make use of its GetExtensionName function. Using something like
VBA Code:
fso.GetExtensionName("C:\Temp\test.csv")
will return a string csv.

So for your case, use a string you already have to represent the full file path + name + extension, assign that to a string then check if it equals pdf.
Thanks! I shall work my way through to understand that and try.. and report back
 
Upvote 0

Forum statistics

Threads
1,214,947
Messages
6,122,411
Members
449,081
Latest member
JAMES KECULAH

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