Consulting

Results 1 to 3 of 3

Thread: Save File with number suffix

  1. #1
    VBAX Regular
    Joined
    Sep 2017
    Posts
    25
    Location

    Save File with number suffix

    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

  2. #2
    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.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Sep 2017
    Posts
    25
    Location
    Thanks Graham these will come in very handy.

    Fra

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •