VB Code to save pdf file with different name like (1) (2)

ateebali

Board Regular
Joined
Dec 13, 2018
Messages
108
Dear Sir,
I am using following code link with button, when it click, it save the file with cell value.
I want if the file already exist in folder, it should not replace the file, instead it saved it with extension like 123(1)


Sub PDFActiveSheet()


ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=ThisWorkbook.Path & "\TCS Style Name " & Range("D16").Value _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True

End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
IF you have Excel 32 bit, put this into a Module and call MakeUnique(your filename here) to build the unique filename.
Code:
'This kb article:
'http://www.vbaexpress.com/kb/getarticle.php?kb_id=1041

'Another sequential filename
'tstav,http://vbaexpress.com/kb/getarticle.php?kb_id=1008

Const Max_Path As String = 260
'http://msdn.microsoft.com/en-us/library/bb776479.aspx
Public Declare Function PathYetAnotherMakeUniqueName _
  Lib "shell32.dll" _
  ( _
  ByVal pszUniqueName As String, _
  ByVal pszPath As String, _
  ByVal pszShort As String, _
  ByVal pszFileSpec As String _
  ) As Boolean

'http://msdn.microsoft.com/en-us/library/bb776479.aspx
Public Declare Function PathMakeUniqueName _
  Lib "shell32.dll" _
  ( _
  ByVal pszUniqueName As String, _
  ByVal cchMax As Long, _
  ByVal pszTemplate As String, _
  ByVal pszLongPlate As String, _
  ByVal pszDir As String _
  ) As Boolean
  
Function fMakeAnotherUnique(vShortTemplate, vLongTemplate, vFolder) As String
  'vFolder can end in trailing backslash or not
  Dim rc As Boolean, vUniqueName As String, s As String
  vUniqueName = Space$(Max_Path)
  rc = PathYetAnotherMakeUniqueName(vUniqueName, StrConv(vFolder, vbUnicode), _
    StrConv(vShortTemplate, vbUnicode), StrConv(vLongTemplate, vbUnicode))
  If rc Then
    vUniqueName = StrConv(vUniqueName, vbFromUnicode)
    fMakeAnotherUnique = vUniqueName
  End If
End Function

Function MakeAnotherUnique(filespec As String) As String
  MakeAnotherUnique = fMakeAnotherUnique("", GetFileName(filespec), GetFolderName(filespec))
End Function

Function fMakeUnique(vShortTemplate, vLongTemplate, vFolder) As String
  'vFolder can end in trailing backslash or not
  Dim rc As Boolean, vUniqueName As String, s As String
  vUniqueName = Space$(Max_Path)
  rc = PathMakeUniqueName(vUniqueName, Max_Path, StrConv(vShortTemplate, vbUnicode), _
    StrConv(vLongTemplate, vbUnicode), StrConv(vFolder, vbUnicode))
  If rc Then
    vUniqueName = StrConv(vUniqueName, vbFromUnicode)
    fMakeUnique = vUniqueName
  End If
End Function

Function MakeUnique(filespec As String) As String
  MakeUnique = fMakeUnique("", GetFileName(filespec), GetFolderName(filespec))
End Function

Function GetFileName(filespec As String) As String
  Dim p1 As Integer, p2 As Integer
  p1 = InStrRev(filespec, "\")
  p2 = Len(filespec) - p1
  GetFileName = Mid$(filespec, p1 + 1, p2)
End Function

Function GetFolderName(filespec As String) As String
  Dim p1 As Integer
  p1 = InStrRev(filespec, "\")
  GetFolderName = Left$(filespec, p1)
End Function
 
Upvote 0
You need a helper function. Please try following to see if it helps.

Code:
Sub PDFActiveSheet()
Dim PDFFileName As String


    PDFFileName = GeneratedFileName(ThisWorkbook.Path & "\TCS Style Name " & Range("D16").Value, 0)


    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, fileName:=PDFFileName, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, OpenAfterPublish:=True


End Sub


Function GeneratedFileName(fileName As String, i As Integer)
Dim testFileName As String
    testFileName = fileName
    If i <> 0 Then
        testFileName = fileName & " (" & i & ")"
    End If
    GeneratedFileName = testFileName
    If Dir(testFileName & ".pdf", vbNormal) > "" Then
        GeneratedFileName = GeneratedFileName(fileName, i + 1)
    End If
    
End Function
 
Upvote 0
Dear Sir
Can you help me again please?
This code giving me error again;
Code:
Sub SavePDFActiveSheet()Dim PDFFileName As String




    PDFFileName = GeneratedFileName(ThisWorkbook.Path & "\PDF " & Range("T3").Value, 0)




    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, fileName:=PDFFileName, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, OpenAfterPublish:=True




End Sub




Function GeneratedFileName(fileName As String, i As Integer)
Dim testFileName As String
    testFileName = fileName
    If i <> 0 Then
        testFileName = fileName & " (" & i & ")"
    End If
    GeneratedFileName = testFileName
    If Dir(testFileName & ".pdf", vbNormal) > "" Then
        GeneratedFileName = GeneratedFileName(fileName, i + 1)
    End If
    
End Function

Run Time Error '52'
Bad File name or number
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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