Setting Current Working Directory
Setting Current Working Directory
(OP)
I am trying to set my current working directory to a network drive as shown below, but I am getting an error saying that there is no object defined. I didnt think I needed an object if I am just changing a setting. The point of this macro is to load/unload an addin without going through the tools menu. Thanks in advance for any help
Sub main()
Set swApp = Application.SldWorks
Const workDir = "\\Home\SolidModels\Integration\SolidWorks\"
swApp.SetCurrentWorkingDirectory (workDir)
retval = swApp.LoadAddIn(SW_Adept.dll)
If retval = 2 Then
swApp.UnloadAddIn (SW_Adept.dll)
End If
End Sub
Sub main()
Set swApp = Application.SldWorks
Const workDir = "\\Home\SolidModels\Integration\SolidWorks\"
swApp.SetCurrentWorkingDirectory (workDir)
retval = swApp.LoadAddIn(SW_Adept.dll)
If retval = 2 Then
swApp.UnloadAddIn (SW_Adept.dll)
End If
End Sub






RE: Setting Current Working Directory
I made a sub very much like yours, and it worked- until
I *removed* the function return value from the "SetCurrentWorkingDirectory" call.
WORKED:success& = swApp.SetCurrentWorkingDirectory (NewWorkPath)
FAILED:swApp.SetCurrentWorkingDirectory (NewWorkPath)
What happens then goes something like this:
1)By removing the "success&" (or any value used to store the return value from the function) you are now calling the routine much like a SUBROUTINE instead of a function. But with a SUBROUTINE, you do not enclose the params in parentheses. The fact that it did not error out means that VBA is assuming the value is being passed by reference. (a constant being passed by reference).
2) By not using return value, you were not able to see the call fail.
So either add the param to catch the return value, or remove the parentheses around your parameter.
RE: Setting Current Working Directory
RE: Setting Current Working Directory
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long
Dim PathLen As Long
Public Directory As String
Dim retval As Variant
Sub main()
Set swApp = CreateObject("SldWorks.Application")
Set Part = swApp.ActiveDoc
Directory = Part.GetPathName() 'full path including file name
PathLen = Len(Directory)
'strip directory from full path
For p = PathLen To 1 Step -1
If Right(Directory, 1) <> "\" Then
Directory = Left(Directory, p - 1)
Else
Exit For
End If
Next
'reset working directory
boolstatus = swApp.SetCurrentWorkingDirectory(Directory)
End Sub
RE: Setting Current Working Directory
Hope it helps
-Tic[k]
RE: Setting Current Working Directory