File is allready saved but code should be stopping duplicates being saved

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,602
Office Version
  1. 2007
Platform
  1. Windows
Morning.
My code is shown below.

A folder on my pc has saved pdf documents & more are added each day.
I am trying to stop duplicates being saved so my code below should be doing that but doesnt.

From my userform i generate the pdf & save it to the folder in question.
The code checks this folder & if it the file is present i see the Msgbox CUSTOMERS FILE HAS ALLREADY BEEN SAVED & then the exit sub kicks in & i am taken to the pdf folder to take a look.
What i have noticed is that even though i am told the file exists its still being saved so example, I now have two files called TOM JONES 001 & should only have the 1.

Do you see my mistake & advise me please.


VBA Code:
Private Sub PurchasedKey_Click()
  Dim sPath As String
  Dim strFileName As String
  Dim sh As Worksheet
  Dim wb As Workbook
 
    With ActiveSheet
    If .Range("Q1") = "" Then
      MsgBox "NO CODE SHOWN TO GENERATE PDF", vbCritical, "NO CODE ON SHEET TO CREATE PDF"
      Exit Sub
    End If
    
    If .Range("N1") = "M" Then
       strFileName = "C:\Users\Ian\Desktop\REMOTES ETC\DISCO II CODE\DISCO II PDF\" & .Range("B3").Value & " (SLS).pdf"
    Else
       strFileName = "C:\Users\Ian\Desktop\REMOTES ETC\DISCO II CODE\DISCO II PDF\" & .Range("B3").Value & ".pdf"
    End If
          
    If Dir(strFileName) = "" Then
      .Range("A1:K23").ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFileName, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False
       MsgBox "PDF FILE HAS NOW BEEN SAVED", vbInformation + vbOKOnly, "SAVED PDF FILE MESSAGE"
      
    With ActiveSheet
    'ActiveWindow.SelectedSheets.PrintOut copies:=1
    Unload PrinterForm
    
    Set wb = Application.Workbooks.Open("C:\Users\Ian\Desktop\REMOTES ETC\DR\DR.xlsm")
             Worksheets("POSTAGE").Activate
    
            Application.Goto Sheets("POSTAGE").Range("A" & Rows.Count).End(xlUp), True
            ActiveWindow.SmallScroll UP:=14
    
    End With
    
    Else
        'IF FILE IS PRESENT DO NOT ALLOW FILE TO BE OVERWRITTEN & TO SHOW MSGBOX
        MsgBox "CUSTOMERS FILE HAS ALLREADY BEEN SAVED", vbCritical + vbOKOnly, "FILE ALLREADY SAVED MESSAGE"
            
    Dim strFolder As String
        strFolder = "C:\Users\Ian\Desktop\REMOTES ETC\DISCO II CODE\DISCO II PDF\"
        ActiveWorkbook.FollowHyperlink Address:=strFolder, NewWindow:=True
        Unload PrinterForm
    Exit Sub
    End If
    End With
    
    Call DISCOHYPERLINK

End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I now have two files called TOM JONES 001 & should only have the 1
You cannot have 2 files with the same name, that is not possible.
Check the names carefully, there should be some letter in the name that makes them different.
I already checked your macro and it works fine.
 
Upvote 0
Well im getting nowhere.
I have made an edit to the code so now if the file is present the userform closes & the pdf file opens the folder so the user can take a look.

BUT the resr of the code continues.
So i then added exit sub SHOWN IN RED BELOW but the issue now is if the file isnt present in the folder after it is saved the DR book / POSTAGE sheet is supposed to open but it then fails


VBA Code:
Private Sub PurchasedKey_Click()
  Dim sPath As String
  Dim strFileName As String
  Dim sh As Worksheet
  Dim wb As Workbook
 
    With ActiveSheet
    If .Range("Q1") = "" Then
      MsgBox "NO CODE SHOWN TO GENERATE PDF", vbCritical, "NO CODE ON SHEET TO CREATE PDF"
      Exit Sub
    End If
    
    If .Range("N1") = "M" Then
       strFileName = "C:\Users\Ian\Desktop\REMOTES ETC\DISCO II CODE\DISCO II PDF\" & .Range("B3").Value & " (SLS).pdf"
    Else
       strFileName = "C:\Users\Ian\Desktop\REMOTES ETC\DISCO II CODE\DISCO II PDF\" & .Range("B3").Value & ".pdf"
    End If
          
    If Dir(strFileName) = "" Then
      .Range("A1:K23").ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFileName, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False
       MsgBox "PDF FILE HAS NOW BEEN SAVED", vbInformation + vbOKOnly, "SAVED PDF FILE MESSAGE"
      
    Else
      'IF FILE IS PRESENT DO NOT ALLOW FILE TO BE OVERWRITTEN & TO SHOW MSGBOX
       MsgBox "CUSTOMERS FILE HAS ALLREADY BEEN SAVED", vbCritical + vbOKOnly, "FILE ALLREADY SAVED MESSAGE"
      
    Dim strFolder As String
        strFolder = "C:\Users\Ian\Desktop\REMOTES ETC\DISCO II CODE\DISCO II PDF\"
        ActiveWorkbook.FollowHyperlink Address:=strFolder, NewWindow:=True
       Unload PrinterForm
      
    [COLOR=rgb(184, 49, 47)]EXIT SUB[/COLOR]
      
    With ActiveSheet
    'ActiveWindow.SelectedSheets.PrintOut copies:=1
    Unload PrinterForm
    
    Set wb = Application.Workbooks.Open("C:\Users\Ian\Desktop\REMOTES ETC\DR\DR.xlsm")
             Worksheets("POSTAGE").Activate
    
    End With
            

    Exit Sub
    End If
    End With
    
    Call DISCOHYPERLINK

End Sub
 
Upvote 0
I am unable to get any fither than the above so will stop.

Ive monitored the following when trying to save a pdf that exists.
CUSTOMERS FILE HAS ALLREADY BEEN SAVED
I CLICK OK
USERFORM CLOSES
THE PDF FOLDER OPENS

The above works fine.

Now when i wish to save a new pdf i see this.
PDF FILE HAS BEEN SAVED
I CLICK OK
I now get a run time error 9

What should happen after the above is
USERFORM CLOSES
WORKSHEET DR OPENS & POSTAGE SHEET SELECTED
Call DISCOHYPERLINK which will check last 10 rows & hyperlink customer if in the pdf folder.

I will await help.
Thanks
 
Upvote 0
I can't help but think the cause of the issue you're facing here, and in your other thread
aren't a result of the same thing, and that would have to be what's in that B3 cell.

We know nothing about B3 other than to assume that it's right.
 
Upvote 0
B3 has no play in this in that i dont think its the issue.

My issue i believe is because i have sytax errors.

The pdf saves the file fine.
Its the process of opening the next workbook / sheet that is a mess
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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