I'm here in need of help for a macro in SolidWorks that works correctly, but is lacking a crucial check while working.
The macro simply saves the document (drawing) as a PDF. Simple right?
Well, it saves the file in the same location the file is being worked from, locally or in a network and with the same name but the ".pdf" extension.
So far so good.
The problem is access rights. usually you have rights to write the file, but depending on certain conditions you don't.
This is what happens:
If there is no PDF of that file in the target location, it creates the PDF and it finishes fine.
If there is a PDF file already, ( say you are updating changes to the drawing), then it overwrites the file but only IF you have access to it.
Here is the problem:
If the file exists in the location and it is READ ONLY, then the macro never finishes and hangs up.
This also hangs up SolidWorks, causing it to crash-hang.
Your only option is force quit SolidWorks and restart, loosing your changes.
If the program is hanged up and you try to manually force DELETE the read only file in Explorer, it also crashes Explorer.
The macro is only missing a step to check if the file is read only.
If it is, then is should just tell the user the file is read only and finish.
I have tried many ways to do this , but since I'm not trained at all in VB, I simply cannot figure it out...
I have cut and paste several other VB subs but I cannot make it work properly... having errors.
Here is the macro steps that work fine, (I recorded back in 2012) but it lacks the file checking for read only:
Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
On Error Resume Next
Dim FilePath As String
Dim PathSize As Long
Dim PathNoExtension As String
Dim NewFilePath As String
FilePath = Part.GetPathName
PathSize = Strings.Len(FilePath)
PathNoExtension = Strings.Left(FilePath, PathSize - 6)
NewFilePath = PathNoExtension & "pdf"
Part.SaveAs2 NewFilePath, 0, True, False
End Sub
Can someone add the missing steps to check if the file to be recorded is read only and then quit the macro with a " File is read only" message?
Then the user can go and take control of the file prior to re-writing over it using the macro.
Much appreciated, Thanks!
Adrian
Jerome:
Based on this post and your, answer I was able to solve the issue. The problem is that I'm working with SolidWorks PDM another layer of complexity.
PDM is marking the "res" = 1 if the file is read only (or checked-in into the vault)
And "res" = 0 is not read only (if the PDM document is check-out and available for write)
Based on your reply and the post, I was able to play around and make it work with the following:
Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
On Error Resume Next
Dim FilePath As String
Dim PathSize As Long
Dim PathNoExtension As String
Dim NewFilePath As String
Dim res As Long
FilePath = Part.GetPathName
PathSize = Strings.Len(FilePath)
PathNoExtension = Strings.Left(FilePath, PathSize - 6)
NewFilePath = PathNoExtension & "pdf"
res = GetAttr(NewFilePath)
If res = 0 Then
Part.SaveAs2 NewFilePath, 0, True, False
Exit Sub
Else
MsgBox "PDF File is not CHECK OUT"
End If
End Sub
Now I need to figure out the commands to simply Check it out and Check it in automatically from PDM and not force the user to do it manually.
Thank you so very much!.
Adrian