4 Replies Latest reply on Nov 3, 2018 9:39 AM by Solid Air

    How to check a file path is valid by macro?

    Jason Apple

      I have the following code. My plan is to check whether a drawing is in  a folder, if not then go to other folder to open.

      However, it does not work

      I believe the error is from line 38

      Any suggestion?

      Option Explicit
      
      
          Dim swApp               As SldWorks.SldWorks
          Dim swModel             As SldWorks.ModelDoc2
          Dim swSelMgr            As SelectionMgr
          Dim selComp             As Component2
          Dim sPath               As String
          Dim sPath1              As String
          Dim sPathX              As String
          Dim sPath2             As String
          Dim i                   As Integer
          Dim iCount              As Integer
          Dim longstatus          As Long
          Dim longwarnings        As Long
          Dim Part                As Object
              
      Sub Main()
          Set swApp = Application.SldWorks
          Set swModel = swApp.ActiveDoc
          Set swSelMgr = swModel.SelectionManager
      
      
          iCount = swSelMgr.GetSelectedObjectCount2(-1)
      
      
          For i = 0 To iCount - 1
              Set selComp = swSelMgr.GetSelectedObjectsComponent4(i + 1, -1)
              Debug.Print selComp.Name2
              
              sPath = Strings.Mid(selComp.GetPathName, InStrRev(selComp.GetPathName, "\") + 1)
              sPath = Left(sPath, InStrRev(sPath, ".") - 1)
              sPath1 = "C:\ET-VAULT\Automation\Projects\PRJ-0532\CAD Files\" & sPath & ".SLDDRW"
              sPathX = "C:\ET-VAULT\Automation\Projects\PRJ-0497\CAD Files\" & sPath & ".SLDDRW"
              sPath2 = "C:\Users\saikit.wu\Desktop\VENDOR PRINT\" & sPath & ".PDF"
          
              'Set Part = swApp.OpenDoc6(sPath, 3, 0, sPath1, longstatus, longwarnings)
              If IsNull(swApp.OpenDoc6(sPath1, 3, 0, "", longstatus, longwarnings)) Then
                  Set Part = swApp.OpenDoc6(sPathX, 3, 0, "", longstatus, longwarnings)
              Else
                  Set Part = swApp.OpenDoc6(sPath1, 3, 0, "", longstatus, longwarnings)
              End If
              
              
              Set Part = swApp.ActiveDoc
              longstatus = Part.SaveAs3(sPath2, 0, 0)
              'Part.PrintDirect
              'swApp.CloseDoc Part.GetTitle
              
              
          Next i
          
          End Sub
          
      
      
      
        • Re: How to check a file path is valid by macro?
          Josh Brady

          You can use the File System Object (scads of info online... search FSO) to see if a FileExists.

           

          Also, you can't compare objects to Null.  For objects, the equivalent is Nothing. So:

           

          If IsNull(swApp.OpenDoc6(sPath1, 3, 0, "", longstatus, longwarnings)) Then

          should be

          If swApp.OpenDoc6(sPath1, 3, 0, "", longstatus, longwarnings) is Nothing Then

           

          However this is wrong too. Because if it wasn't Nothing, that means it opened.  Therefore, when you try to open it again it will fail because it's already open because you just opened it!

           

          So instead:

          set part = swApp.OpenDoc6(sPath1, 3, 0, "", longstatus, longwarnings)

          If part is nothing Then

               Set Part = swApp.OpenDoc6(sPathX, 3, 0, "", longstatus, longwarnings)

          end if

           

          Except, as I said, you should probably use FSO to figure out if things exist or not.

          • Re: How to check a file path is valid by macro?
            Danniel Sims

            I agree with Josh.  You may have to set a reference to Microsoft Scripting Runtime in your project.

            It's under Tools==> References

             

            Adding-reference-to-vba-3.png

             

            Dim fso As Object,

            Set fso = CreateObject("Scripting.FileSystemObject")

             

            If fso.Fileexists ([YOURFILEPATH & YOURFILENAME]) = True Then

                 Set Part = swApp.OpenDoc6 (path1)

            Else

                 Set Part = swApp.OpenDoc6 (path2)

            End If

             

             

            I have another method if you need to go through multiple filepaths.  I use this method for a different purpose, but it looked like a possible fit for what you need to do.

             

            Dest = ""C:\ET-VAULT\Automation\Projects\PRJ-0532\CAD Files\"|C:\ET-VAULT\Automation\Projects\PRJ-0497\CAD Files\"

            DestSplit = Split(Dest, "|")

            DestCnt = 0

             

            Do Until DestCnt = UBound(DestSplit) + 1

                 If fso.Fileexists (DestSplit(Destcnt)) = True Then

                      Set Part = swApp.OpenDoc6(DestSplit(Destcnt) & sPath & ".SLDDRW", 3, 0, "", longstatus, longwarnings)

                      Exit Do

                 End If

            DestCnt = DestCnt + 1

            Loop

            • Re: How to check a file path is valid by macro?
              Murali B,B.

              Please Check,

              if(File.Exists && FileInfo fiileio !=fiileio.IsReadOnly)

              {

              // This is valid file.

              }

              • Re: How to check a file path is valid by macro?
                Solid Air

                Simplest method I know would be to use VBA command DIR (path/filename).  If it returns "" the file is not there.