×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Setting Current Working Directory

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

RE: Setting Current Working Directory

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.

RE: Setting Current Working Directory

(OP)
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

RE: Setting Current Working Directory

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) <> "\" 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

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

Hope it helps
-Tic[k]

All this machinery making modern music can still be open-hearted.

RE: Setting Current Working Directory

(OP)
There we go, it works.  Thanks a lot for your help.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources