I am lost and need help - SW to excel and back

First let me say this is my first post and I am a newbee......

Problem I am having is that I am a self taught VB writer and the company I work for is too cheap to send me to any classes, I know enough to get me in trouble.

SW2006 is what we are using. We also use a an Excel sheet that is called "Master Log" The master log has a column for Part numbers, discriptions, designer and so on..... What I am trying to do is create a new part in solid works and fill in the custom properties, then I need a macro that will copy some information from custom properties as in "Discriptions" and "Designer" and send this info to the Master Log sheet, it should fill in the next row available with this information then copy the part number from that row and send it back to the SW part I file just created and fill in the custom property part number row. I hope this makes sense

Reply to
nsciortino
Loading thread data ...

s as in "Discriptions" and "Designer"

Below is some code from a custom property excel sheet I used in the past. We currently have a custom macro to insert propertys inside of SW, so I no longer use Excel.

Hope this helps.

Sub Macro1() Dim swApp As Object Dim Part As Object Dim retval As Boolean Dim EmptyStr As String

Set swApp = CreateObject("SldWorks.Application") Set Part = swApp.ActiveDoc

'**************************************************************************************** ' This macro will insert up to 100 custom doc properties into an active part, assembly ' or drawing. ' 'Create an excel sheet, column A will be the custom property name, column B will be its value 'Start at Row 4 (i=4) ' ' This macro will not create configuration specific doc properties. ' ' All custom doc properties are inserted as text ' ''*****************************************************************************************

i = 4 Do While i < 103

If Range("A" + Format(i)).Value EmptyStr Then retval = Part.DeleteCustomInfo(Range("A" + Format(i)).Value) retval = Part.AddCustomInfo3(EmptyStr, Range("A" + Format(i)).Value, 30, Range("B" + Format(i))) End If

i = i + 1 Loop

End Sub

Reply to
SW Monkey

'****************************************************************************************

''*****************************************************************************************

Thanks, from what I am reading in this code, it says it will populate a SW file, or am I reading it wrong and if I am, Sorry?

What I need is to read a SW custom property value and fill the information into an Excel sheet on the next available row, then pull a part number that is assigned to this row!

thanks again, Nick

Reply to
nsciortino

'****************************************************************************************

''*****************************************************************************************

What I need is to read a SW custom property value and fill the information into an Excel sheet on the next available row, then pull a part number that is assigned to this row and fill the SW custom property location for it!

Nick

Reply to
nsciortino

'****************************************************************************************

''*****************************************************************************************

Ok, I thought you need to go both ways.

Here is some code that will extract the CP data from a document. Maybe you can use some of it for your custom macro.

Dim swApp As Object Dim Part, ConfigMgr As Object Dim numConfigs, numProps As Long Dim vConfName, vPropName, vPropValue, vPropType, vNames As Variant Dim retval As Boolean Dim EmptyStr, StrValue As String

'**************************************************************************************** ' ' ' This macro will list all custom doc properties and configuration specific doc properties from parts, assemblies and drawings. ' ' ' '*****************************************************************************************

Sub Macro3()

Set swApp = CreateObject("SldWorks.Application") Set Part = swApp.ActiveDoc Set ConfigMgr = Part.ConfigurationManager

Range("A3", "A1003").Value = EmptyStr: Range("B3", "B1003").Value = EmptyStr Range("C3", "C1003").Value = EmptyStr

vNames = Part.GetCustomInfoNames2(EmptyStr)

For n = 0 To UBound(vNames) StrValue = Part.GetCustomInfoValue(configuration, vNames(n)) Range("B" + Format(n + 3)).Value = vNames(n) Range("C" + Format(n + 3)).Value = StrValue Next n

c = n + 3

numConfigs = Part.GetConfigurationCount() vConfName = Part.GetConfigurationNames()

For i = 0 To UBound(vConfName)

Set ConfigMgr = Part.GetConfigurationByName(vConfName(i)) numProps = ConfigMgr.GetCustomProperties(vPropName, vPropValue, vPropType) If numProps 0 Then Range("A" + Format(c)).Value = vConfName(i) For j = 0 To numProps - 1 Range("B" + Format(c)).Value = vPropName(j) Range("C" + Format(c)).Value = vPropValue(j): c = c + 1 Next j Next i

Set Part = Nothing Set swApp = Nothing

End Sub

Reply to
SW Monkey

'****************************************************************************************

''*****************************************************************************************

'****************************************************************************************

'*****************************************************************************************

Thanks again, You were right, I do need to go back and forth. SW to Excel then back to SW...... Custom Prop info to Excel Then a number from Excell back to SW custom property.....

Please do not get frustrated with me, I am not knowlegable in this very much. I am taking what you wrote so far and trying to decifer it.

thanks again for all your time,

Nick

Reply to
nsciortino

When searching for api help it is best if you are as specific as possible. This helps people understand your problem and offer the correct explanation. If possible provide a short outline like you did in your last post (learning already! =D).

So your macro needs to:

  1. Read SW Custom Info
  2. Write that info to an Excel file
  3. Read Excel info
  4. Write that info to a SW File.

To do this you need to use the api help. Think of it like a tree where you need to start at the top and work your way down to obtain what you want.

Assuming you know how to connect to solidworks object and to get the active modeldoc.

  1. Use ModelDoc2::GetCustomInfoValue to retrieve the information you want from the file and store it in a variable. You will have to call this each time you want a new piece of information.

  1. Connect to Excel, you may have to launch it yourself, and open your master document. Make sure you have write access.

  2. Write the information to excel. Find the last available row with ForEach Cell in Range... If not "" then write your data next cell

  1. retrieve your data somehow from excel (i dont know what you are getting)

  2. write this data to solidworks using ModelDoc2::AddCustomInfo3

Note that the SW api calls ask you to specify a configuration. If you don't want the data to be configuartion specific then enter configuration as empty string, ""

Reply to
Mr. Who

PolyTech Forum website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.