13 Replies Latest reply on Jun 29, 2018 11:59 AM by Alex Caicedo

    Dir not working on drawings I never opened

    Alex Caicedo

      Hey guys,

      I have written a macro to batch print PDFs (using the one passed around here with some updates);however, there is one problem I keep having. Anytime the macro tries to open a part from PDM that I have never opened/viewed in SolidWorks before, the Dir function returns a False value and the variable is blank. I actually cut down the code to just the line that contained the Dir function so I could test out a theory:

       

      Dim swApp As Object

      Dim Filename As String

      Sub main()

       

      Filename = Dir("C:\PAS-EPDM\A Systems\ 2018\plastics\CAD\" & "360446" & "*.slddrw")

      MsgBox Filename

       

      'Set swApp = Application.SldWorks

      End Sub

       

      When I run this (knowing that that part is absolutely in that file path), the message box comes out blank. But then, when I open that part in SolidWorks from that location, close the part, then re-run the exact same macro, the message box returns "360446.slddrw" and I can open it with additional code. I tested this on several parts I've never opened before (even running the macro a couple times before opening the part) and the same thing happens.

      Is there any way to get the Dir function to recognize files that are in the file path that I never opened or am I stuck only using this on Dwgs that I have opened previously?

       

      Any help would be greatly appreciated!

       

      Thanks

        • Re: Dir not working on drawings I never opened
          Artem Taturevych

          This file is not cached in PDM local folder so it doesn't exist on your drive - that's why it is failing. You need to create local copies. Either use IEdmFile5::GetFileCopy or IEdmBatchGet from PDM API or just get the local cache from user interface in your vault.

          • Re: Dir not working on drawings I never opened
            Jason Kerns

            Dim vault As EdmVault5

            Dim Nfile As IEdmFile5

            Dim Vfolder As IEdmFolder5

             

            Sub main()

             

            Set vault = New EdmVault5

             

            'CHANGE THIS TO YOUR VAULT NAME

            If Not vault.IsLoggedIn Then

            vault.LoginAuto "PDM VAULT NAME GOES HERE", 0

            End If

             

             

            Set Nfile = vault.GetFileFromPath("FilePath", vfolder)

            Nfile.GetFileCopy 0, 0

             

            'do stuff

             

            end sub

              • Re: Dir not working on drawings I never opened
                Alex Caicedo

                Thanks Jason,

                I updated the code and it seems to run fine, but not sure what to do after that... I'm not experienced with EDM or GetFileFromPath functions. Do I just substitute "Nfile" from your code for "Filename" in my original code? For example, to open the part file I was using:

                 

                Set swApp = Application.SldWorks

                Set swObj = swApp.OpenDoc6(Filename, Ftype, swOpenDocOptions_Silent, "", nErrors, nWarnings)

                 

                I replaced "Filename" with "Nfile" but getting a type mismatch.... do you know how I can open the model after the line Nfile.GetFileCopy?

                 

                thanks,

                  • Re: Dir not working on drawings I never opened
                    Jason Kerns

                    Hi Alex,

                     

                    If you can send me, or post your code I can update it.

                    "Filename" does not need to be replaced with "Nfile".

                    "Filename" should be assigned before the "Set Nfile ..." line then you have a cached file from PDM.

                      • Re: Dir not working on drawings I never opened
                        Alex Caicedo

                        Hey Jason,

                        See below. The bold items are what I just added from your first post... just not sure where/how to use the Nfile.

                         

                        Sub BatchPDFconvert()

                            Load PNlist_userform

                            PNlist_userform.Show

                            HelpMsg = ""

                           

                            PNarray = Split(PNlist_userform.PNinput, vbCrLf)

                            PNlength = UBound(PNarray)

                           

                            sPath = SelectFolder("Select Folder Where Drawings Are Located", "")

                            If sPath = "" Then

                                End

                            End If

                            sPath = sPath + "\"

                           

                            dPath = SelectFolder("Select Folder to Store PDFs", "")

                            If dPath = "" Then

                                End

                            End If

                             dPath = dPath + "\"

                         

                        Dim vault As EdmVault5

                        Dim Nfile As IEdmFile5

                        Dim Vfolder As IEdmFolder5

                         

                        If Not vault.IsLoggedIn Then

                            vault.LoginAuto "PAS-EPDM", 0

                        End If

                           

                            Set swApp = Application.SldWorks

                           

                            For i = 0 To PNlength

                                sFilename = Dir(sPath & PNarray(i) & ".slddrw")  ''NOT OPENING FILES THAT HAVE NEVER BEEN VIEWED

                               

                             Set Nfile = vault.GetFileFromPath(sFilename)

                             Nfile.GetFileCopy 0, 0

                         

                                If sFilename = "" Then

                                    HelpMsg = HelpMsg & PNarray(i) & vbNewLine

                                    GoTo NextIncr

                                End If

                               

                                Set swDraw = swApp.OpenDoc6(sPath + sFilename, swDocDRAWING, swOpenDocOptions_Silent, "", nErrors, nWarnings)

                                Set swDraw = swApp.ActiveDoc

                                dFileName = Mid(swDraw.GetPathName, 1 + InStrRev(swDraw.GetPathName, "\"))

                                dFileName = Left(dFileName, InStrRev(dFileName, ".") - 1)

                                swDraw.SaveAs3 dPath & dFileName & ".PDF", 0, 0

                                swApp.QuitDoc swDraw.GetPathName

                                Set swDraw = Nothing

                         

                         

                        NextIncr:

                            Next i

                         

                        If HelpMsg <> "" Then

                                MsgBox "Drawings that were not found in file path " & vbNewLine & sPath & vbNewLine _

                                    & HelpMsg & "Re-run macro with new location for drawings above.", , "Error Report"

                            End If

                          • Re: Dir not working on drawings I never opened
                            Jason Kerns

                            I see this is missing. replace your line with mine.

                             

                            Set Nfile = vault.GetFileFromPath(sFilename, Vfolder)

                              • Re: Dir not working on drawings I never opened
                                Alex Caicedo

                                I am still getting an error on the Nfile.GetFileCopy line: "Object variable or With block variable not set."

                                I condensed the code so I can just work on this section.. any ideas?

                                 

                                Dim vault As EdmVault5

                                Dim Nfile As IEdmFile5

                                Dim Vfolder As IEdmFolder5

                                Dim swApp As Object

                                Dim sFilename As String

                                Dim sPath As String

                                 

                                Sub main()

                                 

                                Set vault = New EdmVault5

                                 

                                'CHANGE THIS TO YOUR VAULT NAME

                                If Not vault.IsLoggedIn Then

                                    vault.LoginAuto "PAS-EPDM", 0

                                End If

                                 

                                sPath = "C:\PAS-EPDM\Aquatic Systems\ 2018\CLEANERS\CAD\"

                                sFilename = Dir(sPath & "360400" & ".slddrw")

                                'MsgBox sFilename

                                Set Nfile = vault.GetFileFromPath(sFilename, Vfolder)

                                Nfile.GetFileCopy 0, 0 'ERROR

                                 

                                Set swApp = Application.SldWorks

                                Set swDraw = swApp.OpenDoc6(sPath + sFilename, swDocDRAWING, swOpenDocOptions_Silent, "", nErrors, nWarnings)

                                Set swObj = swApp.ActiveDoc

                                 

                                'do stuff

                                 

                                End Sub

                                  • Re: Dir not working on drawings I never opened
                                    Artem Taturevych

                                    Change

                                     

                                    sFilename = Dir(sPath & "360400" & ".slddrw")

                                     

                                    to

                                     

                                     

                                    sFilename = sPath & "360400" & ".slddrw"

                                     

                                     

                                      • Re: Dir not working on drawings I never opened
                                        Alex Caicedo

                                        So I think that fixed the error on the Nfile.GetFileCopy, but I am now getting an error on the line that is supposed to open the drawing... its a type mismatch error.

                                         

                                        Set swDraw = swApp.OpenDoc6(sPath + sFilename, swDocDRAWING, swOpenDocOptions_Silent, "", nErrors, nWarnings)

                                         

                                        Is there a better way to open the drawing?

                                         

                                        thanks,

                                          • Re: Dir not working on drawings I never opened
                                            Artem Taturevych

                                            Set swDraw = swApp.OpenDoc6(sPath + sFilename, swDocDRAWING, swOpenDocOptions_Silent, "", nErrors, nWarnings)

                                             

                                            Should be

                                             

                                            Set swDraw = swApp.OpenDoc6(sFilename, swDocDRAWING, swOpenDocOptions_Silent, "", nErrors, nWarnings)

                                              • Re: Dir not working on drawings I never opened
                                                Alex Caicedo

                                                I am still getting a Type mismatch error on the Set swDraw line. Just to make sure I don't have anything else incorrect, here is what I am running.

                                                 

                                                Dim vault As EdmVault5
                                                Dim Nfile As IEdmFile5
                                                Dim Vfolder As IEdmFolder5
                                                Dim swApp As Object
                                                Dim sFilename As String
                                                Dim sPath As String
                                                Dim swDraw     As Object

                                                 

                                                Sub main()

                                                Set vault = New EdmVault5

                                                 

                                                'CHANGE THIS TO YOUR VAULT NAME
                                                If Not vault.IsLoggedIn Then
                                                    vault.LoginAuto "PAS-EPDM", 0
                                                End If

                                                 

                                                sPath = "C:\PAS-EPDM\Aquatic Systems\ 2018\CLEANERS\CAD\"
                                                sFilename = sPath & "371998" & ".slddrw"
                                                'MsgBox sFilename
                                                Set Nfile = vault.GetFileFromPath(sFilename, Vfolder)
                                                Nfile.GetFileCopy 0, 0

                                                 

                                                Set swApp = Application.SldWorks
                                                Set swDraw = swApp.OpenDoc6(sFilename, swDocDRAWING, swOpenDocOptions_Silent, "", nErrors, nWarnings)

                                                'do stuff

                                                 

                                                End Sub

                                                  • Re: Dir not working on drawings I never opened
                                                    Jason Kerns

                                                    Dim vault As EdmVault5

                                                    Dim Nfile As IEdmFile5

                                                    Dim Vfolder As IEdmFolder5

                                                    Dim swApp As Object

                                                    Dim sFilename As String

                                                    Dim sPath As String

                                                    Dim swDraw     As SldWorks.ModelDoc2

                                                    Dim nErrors As Long

                                                    Dim nWarnings As Long

                                                     

                                                     

                                                    Sub main()

                                                     

                                                    Set vault = New EdmVault5

                                                     

                                                     

                                                    'CHANGE THIS TO YOUR VAULT NAME

                                                    If Not vault.IsLoggedIn Then

                                                        vault.LoginAuto "PAS-EPDM", 0

                                                    End If

                                                     

                                                     

                                                     

                                                    sPath = "C:\PAS-EPDM\Aquatic Systems\ 2018\CLEANERS\CAD\"

                                                    sFilename = sPath & "371998" & ".slddrw"

                                                    Debug.Print sFilename

                                                    Set Nfile = vault.GetFileFromPath(sFilename, Vfolder)

                                                    Nfile.GetFileCopy 0, 0

                                                     

                                                     

                                                     

                                                    Set swApp = Application.SldWorks

                                                    Set swDraw = swApp.OpenDoc6(sFilename, swDocDRAWING, swOpenDocOptions_Silent, "", nErrors, nWarnings)

                                                     

                                                    'do stuff

                                                     

                                                     

                                                    End Sub