Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations IDS on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting Current Working Directory

Status
Not open for further replies.

Creigbm

Mechanical
Aug 1, 2003
161
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


Code:
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
 
Replies continue below

Recommended for you

there is a very subtle error here.

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.
 
Thanks for your help. I tried to add the "success&" to capture the return value, but I am still getting an error that says "Object Required". Could you post the code that you used to get it running correct? Thanks again
 
Dim swApp As Object
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) <> &quot;\&quot; Then
Directory = Left(Directory, p - 1)
Else
Exit For
End If
Next

'reset working directory
boolstatus = swApp.SetCurrentWorkingDirectory(Directory)

End Sub
 
Above is a macro I wrote to set working directory to that of current part.

Hope it helps
-Tic[k]

[bat]All this machinery making modern music can still be open-hearted.[bat]
 
There we go, it works. Thanks a lot for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor