5 Replies Latest reply on Jan 15, 2015 8:00 PM by Thomas Hotchkin

    Macro to save PDF in dynamic location

    Thomas Hotchkin



      All the Solidworks (and related) files I use are setup similar to below:




      00-Category/00-Product/DXFs/00-Subassembly/Flat Pattern-Part.dxf


      I would like to write a macro to update all the PDF and DXF files automatically. So it needs to find all parts in a directory and if its sheet metal export the flat pattern to the same file path but replacing Parts with DXFs. Same for drawings to PDFs. If I could set this up to automatically update files periodically that would be even better.


      I have some C# experience but have never done anything with macros in Solidworks so would anyone be able to help me out a bit?


        • Re: Macro to save PDF in dynamic location
          Greg Rupp

          Hi Thomas, there are many posts on the forum about macros saving PDFs, DXFs, and DWG files... here are a couple:





          Generally macros are not automatically ran. Depending on if you wanted it to run daily, triggered when a file is saved or some other event... you could either create a program (.exe) or an add-in to do what you want.


          I would also suggest reviewing the examples posted here:


          A bunch of api examples:


          • Re: Macro to save PDF in dynamic location
            Thomas Hotchkin

            Thanks for your help guys have been looking through examples but unfortunately most are in VBA not C#. I wouldn't say I know C# but have some experience in it.


            I have made a macro which works but before letting it loose on all my drawings (not just a copy of a few) would someone be able to check that it is coded as it should be and nothing is going to go wrong?



            using SolidWorks.Interop.sldworks;

            using SolidWorks.Interop.swconst;

            using System.Runtime.InteropServices;

            using System;

            using System.IO;


            namespace PDF.csproj


                public partial class SolidWorksMacro




                    public void Main()


                        string pdfPath;

                        string[] drwPaths = System.IO.Directory.GetFiles("C:\\Users\\SAFEROADS201\\Desktop\\01-VMS Trailer\\Drawings", "*.slddrw", SearchOption.AllDirectories);

                        foreach (string s in drwPaths)


                            if (!s.Contains("~$"))


                                ModelDoc2 swDoc = null;

                                int longstatus = 0;

                                int longwarnings = 0;

                                pdfPath = s;

                                pdfPath = pdfPath.Replace("Drawings", "PDFs").Replace("SLDDRW", "PDF");

                                (new FileInfo(pdfPath)).Directory.Create();

                                swDoc = ((ModelDoc2)(swApp.OpenDoc6(s, 3, 0, "", ref longstatus, ref longwarnings)));

                                longstatus = swDoc.SaveAs3(pdfPath, 0, 0);

                                swDoc = null;






                    /// <summary>

                    ///  The SldWorks swApp variable is pre-assigned for you.

                    /// </summary>

                    public SldWorks swApp;




            Thanks for the tip on the task manager too that will be a very easy way to do it.

            • Re: Macro to save PDF in dynamic location
              Keith Rice



              You will probably want to learn VBA if you want to seriously use the API. Most of the learning resources as well as examples are in VBA. I talk about this some in this blog post: 7 Mistakes New SolidWorks API Programmers Make. It's only my opinion, of course, but if you're trying to save time in the long run then this is the way to go. I don't think you'll find VBA very hard to pick up if you already know some C#.


              I also explain how to use the Task Scheduler to run macros here.



              SolidWorks API Tutorials