USFengBULLS
Board Regular
- Joined
- May 7, 2018
- Messages
- 66
- Office Version
- 365
- Platform
- 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.
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