We use a macro found on this site to save a pdf and a dxf of a drawing, the connection to our server is often very very slow and we are forced to "kill" solidworks to stop the process of the macro that tries in vain to connect, is it possible to add a code that would stop it after a while and allow us to switch back on solidworks.
I tend to agree with remrem, you'll have to deal with the errors in your code.
Now, if you just want a snippet of code that allows you to get out of the macro after a while you need to be able to do something like this (not fully tested):
Sub main()
Dim ts As Date
Dim tshour1 As Date
Dim tshour2 As Date
ts = Now
tshour1 = DateAdd("s", 20, ts)
tshour2 = Now
Do While True
If Len(Dir("ton fichier pdf avec chemin")) <> 0 Then
Exit Do
End If
tshour2 = Now
If DateDiff("s", tshour2, tshour1) <= 0 Then
Exit Sub
End If
Loop
End Sub
This snippet of code allows you to get out of the loop if your pdf file exists or to exit the macro after 20 seconds if your file doesn't exist, to be adapted to your code of course.
Hello Big disappointment! after trying with inaccessible server, I don't get out of the macro... Maybe I didn't insert the code of d.roger in the right place? However, I deleted the part that checks for the presence of the file. Anyone have any clarifications to give me?
Sub main()
Dim ts As Date
Dim tshour1 As Date
Dim tshour2 As Date
ts = Now
tshour1 = DateAdd("s", 20, ts)
go to the beginning of the code.
Then the lines:
tshour2 = Now
Do While True
If Len(Dir("ton fichier pdf avec chemin")) <> 0 Then
Exit Do
End If
tshour2 = Now
If DateDiff("s", tshour2, tshour1) <= 0 Then
Exit Sub
End If
Loop
go right after the line where you save your file.
Now, it's quite possible that this won't work, it means that your code is getting on hold or in error before it gets to these lines. What happens and what feedback do you get in debug mode when the problem occurs?
This brings us back to remrem's initial remark, with which I completely agree, you'll have to deal with the errors in your code. The "SaveAs2 (IModelDoc2)" method being obsolete it would be better to switch to the "SaveAs (IModelDocExtension)" method (see here), which allows you to handle a certain number of errors through the "swFileSaveError_e Enumeration" and " swFileSaveWarning_e Enumeration " returns (see here and here). Be careful, the way to save in pdf is different and must go through " ExportPdfData" (see example here).
To deal with errors in vba, you can also see here.
For your information, the macros found on this site are generally snippets of code in which the error management is never done, it is up to the requester to complete and make his macro reliable otherwise it can quickly become specific development on specifications, then it becomes a job...