8 Replies Latest reply on Jun 5, 2018 3:17 PM by Deepak Gupta

    Macro to save as a PDF with a different name every time

    Luke Peterson

      I currently have a macro that makes some simple changes to a drawing and saves it as a pdf. The problem is that it saves the pdf with the same file name each time, and so it is just overwriting the previous version each time. I don't want to have to do a save as and type in a new name hundreds of times, so can anyone tell me a simple way to save each new drawing with a new name? Preferably something like "CompanyName ###.pdf " with the number increasing each time.

        • Re: Macro to save as a PDF with a different name every time
          Peter Kennedy

          If you are always saving to the same folder:

           

          pdfFile = "C:\Company###.pdf"

          Set fso = CreateObject("Scripting.FileSystemObject")

          If fso.FileExists(pdfFile) Then

               For k = 0 To 1000

                    If fso.FileExists(NewFile) Then

                         NewFile = "C:\CompanyName " & k + 1 & ".pdf"

                    Else

                         k = 1001

                    End If

               Next K

          End If

           

           

           

          The problem you have is you need to find out the last saved file from your folder and and then you know what the next file can be saved as, what I wrote isn't complete but with some googling you should be able figure out what you need.

          • Re: Macro to save as a PDF with a different name every time
            Alex Burnett

            I'm not sure if this helps or if it's too in depth for what you are doing but this is something that I'm doing with my files that I save as well except I archive/move the older revisions to a different folder before they are exported and saved. My example is in c# so some digging may need to be done on your part if you want to do this in VBA through solidworks. If you post the code you have then we may better be able to help you.

             

                            DirectoryInfo d = new DirectoryInfo(folderLocation);
            
            
                            if (Directory.Exists(folderLocation))
                            {
                                FileInfo[] fileInfo = d.GetFiles();
            
            
                                foreach (FileInfo file in fileInfo)
                                {
                                    MyFile thisFile = new MyFile(file);
                                    if (thisFile.IsExportedFromSolidworks && thisFile.PartNumber.Equals(currentFile.PartNumber))
                                    {
                                        files.Add(thisFile);
                                    }
                                }
                            }
            

             

            I made a class called MyFile that I added attributes to in order to ensure the file naming convention matched what I was looking for. If it matched, then I pulled the PN, revisions and other data from the file name.

             

            Instead of checking if the PN is equal, you could have a field that gets the number from the file name and you could just sort a big list and find the max number. Then add one and save it.

             

            In any event, seeing your code would help us help you.

            • Re: Macro to save as a PDF with a different name every time
              Deepak Gupta

              Alternatively you can store the last number used (storing-values-when-a-macro-ends) and then increment that number on next use.

              • Re: Macro to save as a PDF with a different name every time
                Christian Chu

                Are you using any PDM system?

                what we did before is using the secondary rev. from wPDM to add to the end of the file name