3 Replies Latest reply on Oct 3, 2016 1:51 PM by jason kooken

    Check if file is "in use" during Macro save

    jason kooken

      A am trying to Add warnings to my saving Macro

      My new requirements are as follows:

      1. I need a message box to alert user that one or both of the files to be saved are in use(locked)

      2. I need to give the option to either retry or cancel from that message

       

      Thanks in advance for any help.

      below is the working code I have to this point:

       

       

       

      Dim longstatus As Long, longwarnings As Long

      Dim swModel As SldWorks.ModelDoc2

      Dim swApp As SldWorks.SldWorks

       

       

      Sub main()

          Set swApp = Application.SldWorks

          Set Part = swApp.ActiveDoc

          Set swModel = swApp.ActiveDoc

          Set SelMgr = Part.SelectionManager

          Dim proj As String

          Dim dwg As String

          Dim filename As String

          Dim saveLoc As String

          Dim rev As String

          Dim trueRev As String

          Set swCusPropMgr = swModel.Extension.CustomPropertyManager("")

       

          filename = swModel.GetPathName

          If filename = "" Then

              MsgBox "Please save the file first and try again", vbCritical

              End

          End If

        

      'Get Revision

          retval = swCusPropMgr.Get3("Revision", False, rev, trueRev)

          If rev = "-" Then

              rev = ""

          End If

       

          dwg = Strings.Left(filename, Len(filename) - 7)

          proj = Strings.Left(dwg, 4)

        

      'Filepath

              dwg = Strings.Right(dwg, 8)

              proj = Strings.Left(dwg, 4)

              Part.Extension.SaveAs "\\xxxxxxx\DWGS\" + proj + "\" + dwg + rev + ".PDF", 0, 0, Nothing, longstatus, longwarnings

              Part.Extension.SaveAs "\\xxxxxxx\DWGS\" + proj + "\" + dwg + rev + ".EDRW", 0, 0, Nothing, longstatus, longwarnings

      End Sub

       

      Message was edited by: jason kooken

        • Re: Check if file is "in use" during Macro save
          Deepak Gupta

          You can use IsFileOpen to check the file is open or not.

           

          If the value is TRUE then file is open and then use the IF condition to retry or cancel.

          • Re: Check if file is "in use" during Macro save
            Deepak Gupta

            Try this one. I've updated it only for PDF file at the moment. You can check and add codes for the eDrawings.

              • Re: Check if file is "in use" during Macro save
                jason kooken

                Deepak,

                I finally have upgraded from SW 2014 and now your macro seems to work, I have tried (with no success to integrate my existing code with what you sent me.

                Existing code:

                Uses file properties/name to dictate the .pdf and .edrw save location, not where the part/.slddrw is saved

                see below:

                 

                Dim longstatus As Long, longwarnings As Long
                Dim swModel As SldWorks.ModelDoc2
                Dim swApp As SldWorks.SldWorks

                Sub main()
                    Set swApp = Application.SldWorks
                    Set Part = swApp.ActiveDoc
                    Set swModel = swApp.ActiveDoc
                    Set SelMgr = Part.SelectionManager
                    Dim proj As String
                    Dim dwg As String
                    Dim filename As String
                    Dim saveLoc As String
                    Dim rev As String
                    Dim trueRev As String
                    Set swCusPropMgr = swModel.Extension.CustomPropertyManager("")

                'filename
                    filename = swModel.GetPathName
                    If filename = "" Then
                        MsgBox "Please save the file first and try again", vbCritical
                        End
                    End If
                   
                    'Get Revision
                    retval = swCusPropMgr.Get3("Revision", False, rev, trueRev)
                    If rev = "-" Then
                        rev = ""
                    End If

                    dwg = Strings.Left(filename, Len(filename) - 7)
                    proj = Strings.Left(dwg, 4)
                   
                    'Filepath
                        dwg = Strings.Right(dwg, 8)
                        proj = Strings.Left(dwg, 4)
                        Part.Extension.SaveAs "\\Nwtfs003\DWGS\" + proj + "\" + dwg + rev + ".PDF", 0, 0, Nothing, longstatus, longwarnings
                       Part.Extension.SaveAs "\\Nwtfs003\DWGS\" + proj + "\" + dwg + rev + ".EDRW", 0, 0, Nothing, longstatus, longwarnings
                       
                        End Sub

                 

                I tried changing your code to tell it to look in this custom location (see underlined bold) for the isfile open command but with no success I just end up breaking the macro each time. I think that there is some overlap in the two that is breaking them I really appreciate all your help on this!

                below is what you sent me:

                 

                Dim swApp        As SldWorks.SldWorks
                Dim swModel      As SldWorks.ModelDoc2
                Dim swDraw       As SldWorks.DrawingDoc
                Dim Filepath     As String
                Dim filename     As String
                Dim boolstatus As Boolean
                Dim longstatus As Long, longwarnings As Long

                Sub main()

                Set swApp = Application.SldWorks
                Set swModel = swApp.ActiveDoc

                ' Is document active?
                If swModel Is Nothing Then
                    swApp.SendMsgToUser2 "A document must be active.", swMbWarning, swMbOk
                    Exit Sub
                End If

                'Save Document
                swModel.Save

                ' Is it a Drawing document?
                If swModel.GetType <> swDocDRAWING Then
                    Exit Sub
                End If

                Set swDraw = swModel

                Filepath = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\"))
                filename = Left(swDraw.GetTitle, Len(swDraw.GetTitle) - 9)

                ' Test to see if the file is open.
                Restart:

                If IsFileOpen(Filepath + filename + ".PDF") Then
                   Dim iRetVal As Integer
                   iRetVal = MsgBox(Filepath + filename + ".PDF is in use by you or another user." & vbNewLine & "Make sure the file is no longer open" & vbNewLine & "or click cancel to exit without updating PDF file.", vbRetryCancel)
                  
                   If iRetVal = 4 Then 'User clicked Retry
                       GoTo Restart:
                   Else
                    'User clicked Cancel at messagebox
                       Exit Sub
                   End If
                Else
                    swDraw.SaveAs (Filepath + filename + ".PDF")
                End If

                ' Warnings to user on Error
                '  MsgBox "Errors: " & Errs & vbCrLf & "Warnings: " & Warnings

                  If Warnings <> 0 Then
                     MsgBox "There were warnings.  PDF creation may have failed. Verify " & Chr(13) & "results and check possible causes.", vbExclamation
                     Else
                  End If

                End Sub

                ' This function checks to see if a file is open or not. If the file is
                ' already open, it returns True. If the file is not open, it returns
                ' False. Otherwise, a run-time error occurs because there is
                ' some other problem accessing the file.

                Function IsFileOpen(filename As String)
                    Dim filenum As Integer, errnum As Integer

                    If Len(Dir(filename)) > 0 Then 'Does file exist?
                   
                        On Error Resume Next   ' Turn error checking off.
                        filenum = FreeFile()   ' Get a free file number.
                        ' Attempt to open the file and lock it.
                        Open filename For Input Lock Read As #filenum
                        Close filenum          ' Close the file.
                        errnum = Err           ' Save the error number that occurred.
                        On Error GoTo 0        ' Turn error checking back on.
                   
                        ' Check to see which error occurred.
                        Select Case errnum
                   
                            ' No error occurred.
                            ' File is NOT already open by another user.
                            Case 0
                             IsFileOpen = False
                   
                            ' Error number for "Permission Denied."
                            ' File is already opened by another user.
                            Case 70
                                IsFileOpen = True
                   
                            ' Another error occurred.
                            Case Else
                                Error errnum
                        End Select
                       
                    Else
                   
                        IsFileOpen = False
                    End If

                End Function