×
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

API Question

API Question

API Question

(OP)
All,

I have recorded the following macro for use ...

'Global Variable Declarations

Dim swApp As Object
Dim Part As Object
Dim SelMgr As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Feature As Object

' Main() call

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.OpenDoc6("C:\Documents and Settings\begas\My Documents\Work-2.0\SolidWorks\SolidworksDrawings\1 INCH CAM\1 inch cam A ear plate.SLDPRT", 1, 0, "", longstatus, longwarnings)
swApp.OpenDoc6 "C:\Documents and Settings\begas\My Documents\Work-2.0\SolidWorks\SolidworksDrawings\1 INCH CAM\1 inch cam A ear plate.SLDPRT", 1, 0, "", longstatus, longwarnings
Set Part = swApp.ActivateDoc2("1 inch cam A ear plate.SLDPRT", False, longstatus)
swApp.ActiveDoc.ActiveView.FrameLeft = 0
swApp.ActiveDoc.ActiveView.FrameTop = 0
swApp.ActiveDoc.ActiveView.FrameState = 1
Part.Extension.InsertScene "\scenes\02 studio scenes\66 light cards.p2s"
swApp.ActiveDoc.ActiveView.FrameState = 1
Set Part = Nothing

End Sub

... I need to have a UserForm with a browse button on it when the users call this macro.

Can someone please tell me how to call a UserForm?

Thank you,

Naerwen

RE: API Question

If your form's name is

NaerwensUserForm

then your code would be:

CODE

NaerwensUserForm.Show

-handleman, CSWP (The new, easy test)

RE: API Question

Of course, first you need to create the user form...

If your are going to browse for folders or files, you will need to use Windows API to gain access to the file dialog objects.

RE: API Question

Windows API is only needed to browse for folders or to enable multi-selection. Browsing for single files can be accomplished with SolidWorks API's GetOpenFilename function.

-handleman, CSWP (The new, easy test)

RE: API Question

My API kung fu is getting dated.

RE: API Question

(OP)
All,

Ok ... so ... Handleman's initial response was good to go and got me to the point where I have a pre-recorded macro that is now tweeked. However, for creating VBAs in solidworks I would like to know what the best practices are ... for instance ... here is the code in the 'Module 1' area of the SW IDE ...

Sub main()

UserForm1.Show

End Sub


... and here is the code for the form that I have created FOR TEST PURPOSES ONLY ...

Private Sub CommandButton1_Click()

Dim swApp As Object
Dim Part As Object
Dim SelMgr As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Feature As Object

Set swApp = Application.SldWorks

Set Part = swApp.OpenDoc6("C:\Documents and Settings\begas\My Documents\Work-2.0\SolidWorks\SolidworksDrawings\1 INCH CAM\1 inch cam A ear plate.SLDPRT", 1, 0, "", longstatus, longwarnings)

swApp.OpenDoc6 "C:\Documents and Settings\begas\My Documents\Work-2.0\SolidWorks\SolidworksDrawings\1 INCH CAM\1 inch cam A ear plate.SLDPRT", 1, 0, "", longstatus, longwarnings

Set Part = swApp.ActivateDoc2("1 inch cam A ear plate.SLDPRT", False, longstatus)

swApp.ActiveDoc.ActiveView.FrameLeft = 0

swApp.ActiveDoc.ActiveView.FrameTop = 0

swApp.ActiveDoc.ActiveView.FrameState = 1

Part.Extension.InsertScene "\scenes\02 studio scenes\66 light cards.p2s"

swApp.ActiveDoc.ActiveView.FrameState = 1

Set Part = Nothing


End Sub

Private Sub CommandButton2_Click()

Dim swApp As Object

Set swApp = Application.SldWorks

swApp.CloseDoc "1 inch cam A ear plate.SLDPRT"

End Sub

Private Sub UserForm_Click()

Dim swApp As Object
Dim Part As Object
Dim SelMgr As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Feature As Object

End Sub

... Is this practice correct??? What I am referring to is the code's structure on 'paper' ... as it were. That is, is this correct ... to force the 'Module 1' area of the IDEs project to call the form and then have the form handle the code ... or should I/ can I  manipulate the objects on UserForm1 from the 'Module 1' object in the IDE?

I hope this is not too confusing ... if it is ... remember ... your guys king fu is way better than mine ... for now. =P

Thanks for all the help you two have given thus far. I truly appreciate it.

Naerwen
 

RE: API Question

You can manipulate objects on the form from a code module.  You can also write functions/subs in a module and call them from a form.  It really depends on what you need to do with your code.  In the case of your example, where you are opening a document and closing it, that is just fine to do in the form.  Your entire "UserForm_Click()" routine does nothing, by the way.  You can delete it.  Also, the two lines containing OpenDoc6 both do the same thing.  Keep the one that begins "Set Part =" and delete the other one.

-handleman, CSWP (The new, easy test)

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