Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

SW API - How do I use GetTitle()

Status
Not open for further replies.

pyroclasm

Computer
May 17, 2005
24
CA
Can some one please help me, I am new to SW API. I want to be able to Get the File Name of an Active Document in SW to appear in a TextBox. I've tried multiple different variations of the code below, I've been at it for days, but can't figure out why it won't work. What am I overlooking?

Robert


--------------------------------
Private Sub UserForm_initialize()

Dim swApp As SldWorks.SldWorks

Dim swModelDoc As SldWorks.ModelDoc2

Dim swDocTitle As String
Dim swActiveComponent As String

Set swModelDoc = GetTitle(swDocTitle)

swActiveComponent = swModelDoc

txtAC.Text = swActiveComponent


End Sub

---------------
 
Replies continue below

Recommended for you

It's hard to tell from your code example and post how to best help you here. Are you new to object-oriented programming? It sort of looks like you might be. Let me know if this is the case and I'll see what I can do to explain. The API help is really good if you already understand VB/A. However, if you've been working on this for days it sounds like you could really use an intro tutorial to VB or VBA. I'm sure Google would turn up a few that could get you started.

Here are the lines you'll need to add (in bold) in order for this to work. I'm assuming this code is supposed to run inside SolidWorks.

Code:
Private Sub UserForm_initialize()

Dim swApp As SldWorks.SldWorks
Dim swModelDoc As SldWorks.ModelDoc2
Dim swDocTitle As String
Dim swActiveComponent As String

[b]Set swApp = Application.SldWorks
'If you are running this from Access or some other 
'application, you will need to replace the line above with:
'Set swApp = CreateObject("SldWorks.Application")
Set swModelDoc = swApp.ActiveDoc



swDocTitle = swModelDoc.GetTitle

txtAC.Text = swDocTitle
[/b]
'Set swModelDoc = GetTitle(swDocTitle)  'This line will not work

'swActiveComponent = swModelDoc 'This line will not work

'txtAC.Text = swActiveComponent  'This line will not work


End Sub
---------------
 
Are you trying to get the name of a part to show in a dwg, or a different type of doc?

Chris
SolidWorks 06 5.1/PDMWorks 06
AutoCAD 06
ctopher's home (updated 01-18-07)
 
It appears he's trying to put the name of the active document in a text box control on a user form somewhere. It's not apparent from the code whether the form is part of a SW macro or another program. The name of the text box (textAC) might indicate Access.
 
GetTitle returns a string. You are trying to place the string into an object. Try placing the result into a string or variant.

[bat]I could be the world's greatest underachiever, if I could just learn to apply myself.[bat]
-SolidWorks API VB programming help
 
We probably should tell him to use swModelDoc.GetPathName rather than swModelDoc.GetTitle (since that one is dependant on a Windows setting.)

Ken
 
Wow, a lot of you responded pretty quick, thank you. I was afraid no one would read my post.

handleman, the code worked perfectly. Sorry for the confusion, I guess I was a little broad. I'm working on making a form for my work that gets/sets information for any component (ASM or PRT) in Solidworks. So idealy I would press a button on the User Interface and the form pops up with the info.

I am new to Visual Basic and had actually bought a really good(expensive) book, but Solidworks API code seems so different. I wondered if reading the book would help. I did some online training at and thought it would be enough to get crack'n with the code.

Thanks everyone for your help.

Robert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top