3 Replies Latest reply on Mar 8, 2018 7:41 AM by James Barnes

    Macro hangs if file is open

    James Barnes

      the line that hangs the macro:

      boolstatus = swModelDocExt.SaveAs(filename, 0, 0, swExportData, lErrors, lWarnings)


      this macro saves the current drawing as a PDF file. It works just fine unless the PDF is open in Adobe reader.

      not usually a problem before as I was the only person that had access to the PDF file system, but... always one.

      now we a few users that can access the PDF files though a custom app.

      so sometimes a PDF will be open that I'm trying to save, on another machine hanging the macro.

      I've tried On Error GoTo..  but it doesn't see it as an error.


      for testing I've opened the PDF on my local machine, the macro will hang. at least until I close the PDF file then the macro will complete.


      is there a timeout setting in the API I can set or some other way to detect the PDF is open before trying to save?




        • Re: Macro hangs if file is open
          Ivana Kolin

          check that pdf not exists (dir) and when exists, try to delete it (kill) or check whether file is in use


          • Re: Macro hangs if file is open
            Nilesh Patel

            Hi James,


            You have specified the value of third parameter (which is 'Options') as 0 which doesn't exist as per API help. Replace 0 with 1.


            Try this:

            boolstatus = swModelDocExt.SaveAs(filename, 0, 1, swExportData, lErrors, lWarnings)


            It Sw fails to save PDF document, the return value of 'boolstatus' will be 'False' and you can use this value to show a message to user to notify that PDF document has not been saved. Below are some codes from the macro I use:


            'Saves the active drawing as PDF. Warning message if fails to save PDF.
                bRatval = swModel.Extension.SaveAs(sFilePath, swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent, swExpPDFData, lErrors, lWarnings)
                If bRatval = False Then
                    swApp.SendMsgToUser2 "Error: Failed to save PDF- " & sFilePath, swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
                    Exit Sub
                End If


            Hope this helps.