Save a PDF File to a Folder ONLY that contains a Certain String

USFengBULLS

Board Regular
Joined
May 7, 2018
Messages
66
Office Version
  1. 365
Platform
  1. Windows
Hello all,
I have been working on this code in many variation for quite sometime now so there may be some jeberish in here for my own notes and such, just please look over it.
Please note as well there is one function sub that replaces illegal characters.
The main concern is this, there will always be either two of these scenarios I need this Sub to perform under:
1. If the folder in the Workbookpath does not exist then create it and put the pdf file inside that new folder.
2. If the folder does exist, find it based of the specific string in its name, which is my variable strExistingFolderName and plce the pdf file in it. and then rename the folder to the new string concentation I have from strFolderName

If I understand correctly I cannot search if the folder exists and then pick the specific folder becuase the folder path has to inculde the full name where I just want it to find the folder that begins with the string
strExistingFolderName = P_Number & " CO " & sp(0) & "*" with the asterisks saying any character combination after. basically from the Worksheet page this string is saying the four digit project number, a space with the letters CO and another space and then sp(0) in the change order number. If it is already created, this folder will always be named with those 3 segments at the beginning of the folder name no matter if there are revisions or acceptance, etc.
My code is below, do not necessarily focus on my declaration of variables or assigning them but rather the code I am using in the first IF THEN statement. That is where it gives me the error everytime I perform my step into debug function inside VBE.
That is where I need some guidance because the second IF THEN works great everytime (which of course it does because the folder never exists so it makes a new directory). I need guidance on how to Find this particular folder based off strExistingFolderName to save my PDF to and then rename that same folder based off the new strFolderName Values.

VBA Code:
Public Sub PDF_Save()
 'Creates & Names a New Folder. Creates & Names the new CO worksheet as a PDF in the new Folder
 'Also Replaces Illegal Characters when Naming the Folder and File
 
 Static PreSheetName As String
 Dim FindFolder As Variant
 Dim P_Number As Range
 Dim CO_Overview As Range
 Dim CO_Date As Range
 Dim CO_Location As Range
 Dim FindTerm As String
 Dim ReplaceTerm As String
 Dim strName As String
 Dim strPath As String
 Dim strFolderName As String
 Dim strgNewFolderName As String
 Dim strFolderPath As String
 Dim strUpdatedFolder As String
 Dim strExistingFolderName As String
 Dim wbA As Workbook
 Dim wsA As Worksheet
 Dim FSOLibrary As Object
 Dim FSOFolder As Object
 Dim FSOExistingFolderName As Object
 Dim ExistingCO As String


 PreSheetName = ActiveSheet.Name
 Set P_Number = ActiveSheet.Range("B4")
 Set CO_Date = ActiveSheet.Range("F3")
 Set CO_Overview = ActiveSheet.Range("B9")
 Set CO_Location = ActiveSheet.Range("D8")
 ExistingCO = PreSheetName
 Set wbA = ThisWorkbook
 Set wsA = ActiveSheet
 Set FSOLibrary = CreateObject("Scripting.FileSystemObject")
 

 sp = Split(ExistingCO, "-")
 
 
 strPath = wbA.Path
 strName = P_Number & " CO " & wsA.Name & " " & CO_Location & CO_Overview & " " & Format(Date, "mm-dd-yyyy") & ".pdf"
 strFolderName = P_Number & " CO " & wsA.Name & " " & CO_Location & CO_Overview
 strFolderPath = wbA.Path & "\" & ReplaceIllegalCharacters(strFolderName, "_")
 strExistingFolderName = P_Number & " CO " & sp(0) & "*"
 'Set FSOFolder = FSOLibrary.GetFolder(strPath & strExistingFolderName)
 

 If FSOLibrary.FolderExists(strExistingFolderName) Then

         'strUpdatedFolder = wbA.Path & "\" & ReplaceIllegalCharacters(strFolderName, "_")
        wsA.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=strFolderPath & "\" & ReplaceIllegalCharacters(strName, "_"), _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=True

             'confirmation message with file info
             MsgBox "PDF file has been created: " _
                & strName _
                & " File Location " _
                & strFolderPath
        

 End If
 
 
 If FSOLibrary.FolderExists(strExistingFolderName) = False Then

         MkDir (strPath & "\" & ReplaceIllegalCharacters(strFolderName, "_"))
         wsA.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=strFolderPath & "\" & ReplaceIllegalCharacters(strName, "_"), _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=True

             'confirmation message with file info
             MsgBox "PDF file has been created: " _
                & strName _
                & " File Location: " _
                & strFolderPath
                
 End If

End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
I have found a post on this forum that gets close to what I am trying to do..both here and here2
I tried it my code and it is not saving the pdf to the folder as well as this is not going to fully work because the strFolderName in my sub is going to change almost every time, except for the portion labeled strExistingFolderName. This Remains the same each time a new Change Order is created... 2888 CO 1, 2888 CO 2

The Full Folder Name for the CO 1 example would be something like this 2888 CO 1-0 Rm 112 Added Base Cabinets
Then if there is a revision to that Change Order the New strFolderName would be 2888 CO 1-1 Rm 112 Added Base and Uppers
Notice the 2888 CO 1 is the same for both and will always be once that Change order is initially created but I can't figure a way to find that folder Based of that specific text string, then Save the revised PDF into that same folder (instead of creating a new folder each time for each revision) and to rename that folder to what the new strFolderName is which reflects the latest information regarding the change order status of that change order.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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