title: Check if file exist, if so add number
body: <p>I have written a code that saves emails as pdf in Folder1 and downloads the attachments in Folder2. Now the problem I have is that if an attachment has the same name it overwrites it. </p>
  
  <p>I tried to add this piece of code so it automatically adds a number in front of the attachments name but it didn't work.</p>
  
  <pre><code>Dim x As Integer
  x = 0
  If strFile &lt;&gt; strFile Then
      objAttachments.Item(i).SaveAsFile strFile
      objAttachments.Item(i) = Replace(objAttachments.Item(i), " ", "_")
  
  Else
      strFile = strFile
      objAttachments.Item(i).SaveAsFile x &amp; strFile  
      objAttachments.Item(i) = Replace(objAttachments.Item(i), " ", "_")
  
      x = x + 1
  
  End If
  </code></pre>
  
  <p>Here is the whole code:</p>
  
  <pre><code>' Get the path to your My Documents folder
  strFolderpath = "C:\Users\Kevin\Downloads\bestanden\"
  On Error Resume Next
  
  ' Instantiate an Outlook Application object.
  Set objOL = CreateObject("Outlook.Application")
  
  ' Get the collection of selected objects.
  Set objSelection = objOL.ActiveExplorer.Selection
  
  ' Set the Attachment folder.
  strFolderpath = "C:\Users\Kevin\Downloads\bestanden\"
  
  ' Check each selected item for attachments. If attachments exist,
  ' save them to the strFolderPath folder and strip them from the item.
  For Each objMsg In objSelection
  
      ' This code only strips attachments from mail items.
      ' If objMsg.class=olMail Then
      ' Get the Attachments collection of the item.
      Set objAttachments = objMsg.Attachments
      lngCount = objAttachments.Count
      strDeletedFiles = ""
  
      If lngCount &gt; 0 Then
          ' We need to use a count down loop for removing items
          ' from a collection. Otherwise, the loop counter gets
          ' confused and only every other item is removed.
          For i = lngCount To 1 Step -1
              ' Save attachment before deleting from item.
              ' Get the file name.
              strFile = objAttachments.Item(i).Filename
              ' Combine with the path to the Temp folder.
              strFile = strFolderpath &amp; strFile
              strFile = Replace(strFile, " ", "_")
  
              ' Save the attachment as a file.
              If strFile &lt;&gt; strFile Then
                 objAttachments.Item(i).SaveAsFile strFile
                 objAttachments.Item(i) = Replace(objAttachments.Item(i), " ", "_")
              Else
                 strFile = strFile
                 objAttachments.Item(i).SaveAsFile strFile &amp; x
                 objAttachments.Item(i) = Replace(objAttachments.Item(i), " ", "_")
  
                  x = x + 1
              End If
          Next
      End If
  Next
  </code></pre>