PDA

View Full Version : [SOLVED:] Save File with number suffix



framcc06
05-21-2018, 12:55 PM
Hi Folks,

I got the below code from another forum which creates a folder and saves the file as a pdf in it. What I would like now is that if the folder already exists to save the file with number suffix at the end e.g. test1.pdf or test(1).pdf

Thanks,

Fra


Sub newFolder()

Dim strNewFolderName As String
strNewFolderName = "Attendance Register " & ActiveDocument.MailMerge.DataSource.DataFields("Text_Display").Value 'Needs to be text value
If Len(Dir("I:\Computer Programming\Languages\VBA\Mail Merge\Attendance Registers" & strNewFolderName, vbDirectory)) = 0 Then
MkDir ("I:\Computer Programming\Languages\VBA\Mail Merge\Attendance Registers\" & strNewFolderName)
End If
Dim PathName As String
PathName = ("Attendance Register " & (ActiveDocument.MailMerge.DataSource.DataFields("Text_Display").Value))
ActiveDocument.SaveAs FileName:="I:\Computer Programming\Languages\VBA\Mail Merge\Attendance Registers\" & strNewFolderName & "\" & Split(ActiveDocument.Name, ".")(0) & " " & ActiveDocument.MailMerge.DataSource.DataFields("Text_Display").Value & ".pdf", _
FileFormat:=wdFormatPDF

End Sub

gmayor
05-21-2018, 08:19 PM
See the FilenameUnique function at https://www.gmayor.com/useful_vba_functions.htm
However this looks like you are trying to name the individual documents of a mail merge - see http://www.gmayor.com/individual_merge_letters.htm.

framcc06
05-21-2018, 10:59 PM
Thanks Graham these will come in very handy.

Fra