3 Replies Latest reply on Jul 3, 2018 9:49 AM by Peter Kennedy

    PDM Standard - Save as PDF Failing

    Andrew Miles

      Hi All,


      I'm in the process of trying to set a macro up to hotkey saving PDF and DXF files out for production drawings. At the moment it's pretty much the same as the Solidworks API example found here: 2017 SOLIDWORKS API Help - Save File as PDF Example (VBA)


      I know the code works as I've been able to save out both file types from Solidworks into the relevant folders within the PDM and locally with little to no issue. The problem comes when trying to save a PDF which is write locked in the PDM (either not checked out or released etc) as Solidworks locks up causing the need to restart it entirely. If the file is new or checked out there's no issue and the file saves fine.


      The line which is failing is the following:

      boolstatus = swModelDocExt.SaveAs(pdflocation, 0, 0, swExportPDFData, lErrors, lWarnings)


      At a guess I'd say it's trying to write to the file, failing then getting stuck in a loop, however I don't face the same issue when saving out DXF files which are also checked out. These appear to fail gracefully by flagging boolstatus false which then pops up a warning message box.


      Is there any kind of check I can add to scan the PDM location status to prevent the file from writing something which is always going to fail? Alternatively is there any other save function I'm unaware of which would avoid this issue entirely?


      Apologies if this is a re-post, I've been searching for the last few hours bouncing between Solidworks and VBA help pages, but it seems that it's the PDM causing the problem rather than the process I'm taking.

        • Re: PDM Standard - Save as PDF Failing
          Michele Vergine

          hi Andrew, i've been creating a small plugin with c# and i've implemented the same PDF Save Procedure. I hope this will help you:


          public bool CreatePDF(out string errorMessage, out string warningMessage, SldWorks app, ModelDoc2 Drawing)


                      bool result = false;



                      errorMessage = "";

                      warningMessage = "";

                      int errors = 0;

                      int warnings = 0;







                          ExportPdfData swExportPDFData = default(ExportPdfData);

                          ModelDocExtension swModExt = default(ModelDocExtension);


                          string sourceFullPath = Drawing.GetPathName();

                          string newPdfFullFileName = Path.ChangeExtension(sourceFullPath, "pdf");


                          swModExt = (ModelDocExtension)Drawing.Extension;

                          swExportPDFData = (ExportPdfData)app.GetExportFileData((int)swExportDataFileType_e.swExportPdfData);

                          swExportPDFData.ViewPdfAfterSaving = false;

                          result = swModExt.SaveAs(newPdfFullFileName, (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)(swSaveAsOptions_e.swSaveAsOptions_Silent | swSaveAsOptions_e.swSaveAsOptions_Copy), swExportPDFData, ref errors, ref warnings);



                      catch (Exception ex)


                          errorMessage = ex.ToString();




                      return result;


          • Re: PDM Standard - Save as PDF Failing
            Andrew Miles

            Thank you for the reply Michele, however I'm afraid I can't confirm that solution works, because something funky is going on with the visual studio package here.


            In case anyone stumbles upon this I believe I've managed a rough solution within VBA. Because PDM Standard doesn't appear to support vault access/checking files out etc, I've come across a way of simply checking the write status of the PDF file destination before even attempting to save (thank you stack exchange!). I can then at least alert anyone using the macro the destination is locked and intervention is needed.


            The code for this is the following:

            Public Function isReadOnly(ByVal fName As String) As Boolean



                'vbNormal = 0, vbReadOnly = 1, vbHidden = 2, vbDirectory = 16



                If Len(fName) > 0 Then isReadOnly = GetAttr(fName) And vbReadOnly



            End Function

            This takes in the file location (i.e. C:\Folder\drawing.pdf) and returns true if the file is writable, then if the file isn't checked out/writable then this will return false.

            • Re: PDM Standard - Save as PDF Failing
              Peter Kennedy

              If you think there is an issue with the API coding in solidworks you can email support and depending on the issue maybe able to help you out at apisupport@solidworks.com.


              I know I use a macro for saving PDF and DXF files, and I use that same line of code, however I don't have issues but I also don't have PDM standard so I can't help there.