Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

How to select dimensions and export to excel or access

Status
Not open for further replies.

Dtown266

Nuclear
Oct 31, 2006
47
0
0
US

I am trying to write a macro that will allow me to Export only the selected dimensions to a table or database. Im new
to vba so much help would be greatly appreciated. Also I am using solidworks 2003.
 
Replies continue below

Recommended for you

i checked the api before i made the post. yet it was of no help consideriing i am new to vba. A little direction would be appreciated
 
Granted that the API help can be difficult to use, and I do not want to sound too critical because I was once (and still am too many times) in your shoes, but I did a search using Dimension as the keyword and came up with the "Get Component Via Display Dimension Example (VB)". This is from SW2004 API help (I no longer have access to SW2003) so you may not have it. While it is not exactly what you asked for it does show you how to get the dimension object which is only a couple steps away. With that said, I did write a very very simple example. It contains no checking to see if the objects are valid or dimensions were pre-selected. And I am by no means an Excel VBA programmer so the API calls I use probably are not the best but they did get the job done. The example is written using Early binding (see API help for definition). What this means is you will have to select Tools, References from the VBA editor menu and check the following:

Code:
SldWorks 2003 Type Library (although it may be just SldWorks Type Library in SW2003)

SolidWorks Constant Type Library

Microsoft Excel 11.0 Object Library (again may be different for you, I have Office 2003 installed).

In the code window type (or cut and paste) the following code.

Code:
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swDim As SldWorks.Dimension
Dim xlApp As Excel.Application
Dim xlWorksheet As Excel.Worksheet
Dim AtIndex As Long

Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager

Set xlApp = GetObject(, "excel.application")

Set xlWorksheet = xlApp.ActiveSheet

For AtIndex = 1 To swSelMgr.GetSelectedObjectCount

    Set swDim = swSelMgr.GetSelectedObject5(AtIndex).GetDimension
    
    xlWorksheet.Cells(1, AtIndex) = swDim.value
    
Next

End Sub

Now open a blank Excel workbook, select a few dimensions in SolidWorks and run the macro. I hope this helps.

Some final thoughts. Since you are new to SW API programming, I would suggest you do a search on "example" in the API help. There are a lot of good examples that you can learn from. You can also look in the FAQ's on this site as well as the SolidWorks web site for more examples.

SA
 
Status
Not open for further replies.
Back
Top