Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

Access "Author" and "Comments" properties in VBA macro 3

Status
Not open for further replies.

dogarila

Mechanical
Oct 28, 2001
594
0
0
CA
How can I get and set the values of "Author" and "Comments" properties from a macro in SolidWorks?
 
Replies continue below

Recommended for you

Thank yoy Deepak, I've already figured it out. I hope you don't mind that I post your code here for reference:

Code:
' Get and Set Summary Info----------------------- 04/13/11
'
' Will not overwrite existing values for Author and Comemnts
'
' ------------------------------------------------------------------------------
' Written by: Deepak Gupta ([URL unfurl="true"]http://gupta9665.wordpress.com/)[/URL]
' ------------------------------------------------------------------------------
    Dim swApp               As SldWorks.SldWorks
    Dim swModel             As SldWorks.ModelDoc2
    Dim bRet                As Boolean
    Dim value               As String
    Dim svalue              As String
Sub main()

    Set swApp = CreateObject("SldWorks.Application")
    Set swModel = swApp.ActiveDoc
   
    Debug.Print "File = " + swModel.GetPathName
 
'Get and set the Author KeyWord in the File Summary Info
value = swModel.SummaryInfo(swSumInfoAuthor)
If value = "" Then
swModel.SummaryInfo(swSumInfoAuthor) = "Author name here"

'Get and set the Comment KeyWord in the File Summary Info
svalue = swModel.SummaryInfo(swSumInfoComment)
If svalue = "" Then
swModel.SummaryInfo(swSumInfoComment) = "Comments Here"

Else

End If

End If

End Sub
 
Thank you as well Deepak! You wouldn't be able to explain how to modify the code so that it can get and set the customs tab (Author, Description, Material ect.) would you?

I have been trying this for a couple days now, I got your code to work, but I am just getting to learn VBA, so programming is a little difficult right now lol

Thanks again



Eric Roe
Electro-Mechanical Engineer
Airfloat LLC
 
Check if this helps

Code:
' Get and Set Summary Info----------------------- 04/13/11
'
' Will not overwrite existing values for Author and Comemnts
'
' ------------------------------------------------------------------------------
' Written by: Deepak Gupta ([URL unfurl="true"]http://gupta9665.wordpress.com/)[/URL]
' ------------------------------------------------------------------------------
    Dim swApp               As SldWorks.SldWorks
    Dim swModel             As SldWorks.ModelDoc2
    Dim bRet                As Boolean
    Dim value               As String
    Dim svalue              As String
Sub main()

    Set swApp = CreateObject("SldWorks.Application")
    Set swModel = swApp.ActiveDoc
   
    Debug.Print "File = " + swModel.GetPathName
 
'Get and set the Author KeyWord in the File Summary Info
value = swModel.SummaryInfo(swSumInfoAuthor) ' this lines gets the info
If value = "" Then
swModel.SummaryInfo(swSumInfoAuthor) = "Author name here" ' this line sets the info, so put the author name what you want to display

'Get and set the Comment KeyWord in the File Summary Info
svalue = swModel.SummaryInfo(swSumInfoComment)
If svalue = "" Then
swModel.SummaryInfo(swSumInfoComment) = "Comments Here"

Else

End If

End If

End Sub


' Similarly you can use codes for setting up info for

' Title = swModel.SummaryInfo(swSumInfoTitle)

' Subject = swModel.SummaryInfo(swSumInfoSubject)

' Keywords = swModel.SummaryInfo(swSumInfoKeywords)


' For getting the info for follwoing information

' SavedBy = swModel.SummaryInfo(swSumInfoSavedBy)

' CreateDate = swModel.SummaryInfo(swSumInfoCreateDate)

' SaveDate = swModel.SummaryInfo(swSumInfoSaveDate)

Deepak Gupta
SW 2010 SP4.0 & 2011 SP3.0
Boxer's SolidWorks™ Blog
 
Deepak, Thank you for the updated program.

But this program will only fill in the Summary Tab of the Summary Information dialog box correct?

What if I would like to fill out the Custom Tab of the Summary Information dialog box? (where the part Description, StockNumber, Routing, and Material Description are)

I tried replacing the Author with Description, but that did not work..

Eric Roe
Electro-Mechanical Engineer
Airfloat LLC
 
Here you go. test and let me know if you need more details:

Code:
Sub main()

Dim swApp As Object
Dim MyProp(1, 5) As String ' If you add more properties, just increment the second number in the bracket
Dim m As Integer

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

'Define custom property names here and add more line for more properties. Just increment the second number in the bracket

MyProp(0, 0) = "File Name"
MyProp(0, 1) = "Material"
MyProp(0, 2) = "Finish"
MyProp(0, 3) = "Weight"
MyProp(0, 4) = "Description"

'Define Property values here add more line for more properties values added above. Just increment the second number in the bracket

MyProp(1, 0) = "$PRP:" & Chr(34) & "SW-File Name" & Chr(34)
MyProp(1, 1) = Chr(34) & "SW-Material" & Chr(34)
MyProp(1, 2) = "-"
MyProp(1, 3) = Chr(34) & "SW-Mass" & Chr(34)
MyProp(1, 4) = "Plate"

For m = 0 To 5 ' If you add more properties, just increment the second number i.e 5 here

    retval = Part.AddCustomInfo3("", MyProp(0, m), 30, MyProp(1, m))
Next m
            
End Sub

Deepak Gupta
SW 2010 SP4.0 & 2011 SP3.0
Boxer's SolidWorks™ Blog
 
Deepak, you are awesome! Thanks a lot for the program. Unfortunately I am going out of town for the rest of the week, so I will not be able to test your code until Monday. I will let you know then!

Thanks again

Eric Roe
Electro-Mechanical Engineer
Airfloat LLC
 
Well I played around with the program today, and I would like to thank Deepak for his knowledge and pretty much giving making a program for me. I did, however, change a little of the program:


Sub main()

Dim swApp As Object
Dim MyProp(1, 5) As String ' If you add more properties, just increment the second number in the bracket
Dim m As Integer

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

'Define custom property names here and add more line for more properties. Just increment the second number in the bracket

MyProp(0, 0) = "File Name"
MyProp(0, 1) = "Material"
MyProp(0, 2) = "Finish"
MyProp(0, 3) = "Weight"
MyProp(0, 4) = "Description"

'Define Property values here add more line for more properties values added above. Just increment the second number in the bracket

MyProp(1, 0) = "$PRP:" & Chr(34) & "SW-File Name" & Chr(34)
MyProp(1, 1) = Chr(34) & "SW-Material" & Chr(34)
MyProp(1, 2) = "2543"
MyProp(1, 3) = Chr(34) & "SW-Mass" & Chr(34)
MyProp(1, 4) = "Plate"

For m = 0 To 5 ' If you add more properties, just increment the second number i.e 5 here

Part.CustomInfo(MyProp(0, m)) = MyProp(1, m)
Next m

End Sub
------------

For some reason, when I kept the original line:
retval = Part.AddCustomInfo3("", MyProp(0, m), 30, MyProp(1, m))

If I tried to change the value of a property that has already been set, it would not overwrite it. So I did some research and canged the line to:
Part.CustomInfo(MyProp(0, m)) = MyProp(1, m)

This way the value will be overwritten every time.

Thanks a lot Deepak, by sharing these two programs with me, I have learned so much more about programming in SW compared to the week I spent trying to figure it out using only the API help browser.

Eric Roe
Electro-Mechanical Engineer
Airfloat LLC
 
I have yet another question...

Is there any way I can get a macro to run automatically when the user performs a certain task in SW?

The company I work for uses a laser cutter to cut all of our sheet metal parts, so we have a template already made that sets everything up with our laser program (pronest). The only tedious part about using the laser template is that in order for the laser to determine which type of sheet metal to cut the part, the user has to manually enter the stock number of the material (we have a list of about 15 different types of sheet metal we use as our standard material).

With the help of Deepak, I am able to automatically update the stock number of the part with a macro. This works great, but the macro has to be run every time someone changes the thickness (All of our stock material has a different thickness, so the program I made is driven by the thickness of the part) or else the stock number will be incorrect.

I have made a custom message box that pops up when the macro is run, and the user can choose from a list of materials that we keep on stock.

I would like to make the macro run every time someone edits the Base-Flange1 feature under the template we use (and only under this template). So when the user clicks to edit the feature, the box pops up, then the user selects the material, and the macro will assign the correct thickness, stock number, material, ect.., and then exit out of the Base-Flange feature.

The macro is already working (except for setting the material thickness, which I think will not be that hard), I just need to get it to run when the user selects to edit the Base-Flange1 feature...

Thanks for all the help,

Eric Roe
Electro-Mechanical Engineer
Airfloat LLC
 
Look into creating a macro feature. I believe that these get run each time the model is rebuilt. You could have it check the thickness of the base-flange1 feature and if that did not match the thickness for the stock number, then you could automatically change the stock number or pop up a box asking if it was ok to change the stock number or pop up a box asking the user to select the stock number.

Eric
 
Well it has been a while since I have updated what I am doing with the program. I have been really busy with work, so I have only been working on it here and there, but I am almost finished with it (I believe) and I wanted to share what I have so far.

I made a custom message box that will open when a user runs the macro. This box lists all of the stock materials that my company uses. So all the user has to do is select the material he wants to use, and the program will automatically set the thickness, stock number, material description, and change to the correct material.

I have attached a screen shot of the message box. I added a user entry field if the part is not stock (but I do not have that working yet).

I have everything working right now, except:
I have not figured out a way to edit the thickness of the material (I think I am close, but any help via similar programs would be awesome). I found a way to edit the base-Flange feature using VBA, but I am still trying to figure out how to change the thickness value.

The program is a little disorganized. At first I wanted to enter all the data I need into an array, but I was having problems with getting it to work, so right now there is no easy way to edit our stock material. If a stock number changes, I would have to search through the program and find the button press that is attached to that specific stock number and change it. I would like to have a table in a csv or excel file that the program could reference to.

And the last update that I want to do, but have not even started trying to figure out is getting the message box to open automatically. I would like in the future for the box to open up only if the user is working with a sheet metal part, and tries to edit the base-flange1 feature. I imagine that there would have to be a macro that starts up when SW starts, and waits (if a macro can wait) until it sees the user trying to edit the base-flange1 feature, then when the program enters the feature edit, the message box will pop up allowing the user to enter the desired stock material.

I am still working on some bugs in the program, but as soon as I finish, I will post.

Eric Roe
Electro-Mechanical Engineer
Airfloat LLC
 
 http://files.engineering.com/getfile.aspx?folder=c355648b-0694-440b-9299-df9663af5fcb&file=pick_box.png
Change Dimension Example (VBA)

Personally I've never seen a macro auto-run in SW. It seems like a security hole to me anyway.

To reference an external file, would be some direct VB.NET code.
That is a good reference. But then I need to ask:
Does anyone know if a SW Macro can access external files that are not SW related?

Devon Murray, EIT [Mechanical]
Solidworks 2011 SP 2.0
 
Status
Not open for further replies.
Back
Top