vbasolidworks

Check read only for PDF file from SolidWorks


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:

Here is the problem:

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


Solution

  • 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