Macro pdf recording with error handling

Hello I am currently using the following code to save an MEP in PDF

longstatus = Part.SaveAs3(FilePath + FileName + ".PDF", 0, 0)

 

No problems on this side EXCEPT when the said PDF is already open and therefore not overwritten at this time the solidworks and blocked and waits indefinitely for the pdf to be overwritten again I don't know if my problem is very understandable but if anyone has a solution I'm interested :)

You have to go through an error management inside your macro

or a loop with a validity period.

you find it easy to find on programming forums

2 Likes

Personally, I used a long VBA program to do this... so I'm not going to bring everything back to you;)

But for the part that interested you, my line was slightly different but I wondered if I wanted to write on an already existing file and indicated that it was impossible to write if already opened.

 

So, I think it should work with that:)

 

boolstatus = swpdfext. SaveAs(namePDF, 0, 0, swPDF, IErrors, IWarnings)

 

with

Dim swpdfext As SldWorks.ModelDocExtension

Dim swPDF As SldWorks.ExportPdfData

Dim swpdfext As SldWorks.ModelDocExtension

Dim IErrors As Long

Dim IWarnings As Long

Dim boolstatus As Boolean

 

Set swpdfext = swdoc. Extension

Set swPDF = swapp. GetExportFileData(1)

Set swpdfext = swdoc. Extension

 

namePDF = FilePath & newname & ".pdf"

FilePath being the record folder

Newname being the name of your file

And if the opening of the pdf is read-only. Crushing should not be a problem.

Thank you todescocoin37coin & Bart☺

for your answers but nothing helps, the macro freezes as long as the PDF file is not closed, I tried an error handling but it doesn't react either :/

Hello

I think the link below should be able to help you:

http://www.cpearson.com/excel/IsFileOpen.aspx

Hello

Possible to do this for example:

        protected virtual bool IsFileLocked(FileInfo file)
        {
            FileStream stream = null;
            try
            {
                stream = file. Open(FileMode.Open, FileAccess. Read, FileShare. None);
            }
            catch (IOException)
            {
                return true;
            }
            finally
            {
                if (stream != null)
                    Stream. Close();
            }
            return false;
        }

to be called as follows:

           ModelDoc2 SWPart = null;
            SWPart = ((ModelDoc2)(SolidWorksMacro.swapp. ActiveDoc));
            Create the pdf on desktop
            string namePDF = string. Empty;
            namePDF = System. Environment. GetFolderPath(System.Environment. SpecialFolder. Desktop) + "\\test.pdf";
            
            FileInfo namePDFinfo = new FileInfo(namePDF);
            if (IsFileLocked(namePDFinfo))
            {
                MessageBox. Show("Read-only");
                return;
            }
            SWPart. SaveAs(namePDF);

 

Sorry it's C# (so to be translated into vba) but the principle is:

1°) create a function that allows you to know if the file is being used.

2°) Test if the file is used before saving it as a PDF.

Kind regards

Thank you Cyril.f 

 

This gives the following code

If IsFileOpen(FilePath + FileName + ". DWG") = True Then
MsgBox "The "File & FilePath + FileName +". DWG is currently being used by a user and therefore cannot be overwritten."
Else
longstatus = Part.SaveAs3(FilePath + FileName + ". DWG", 0, 0)
End If

to be adapted according to DWG or PDF without forgetting to add a module with the content of Cyril's link.f