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 Remove All Properties in a Document? 2

Status
Not open for further replies.

pyroclasm

Computer
May 17, 2005
24
CA
Hey Guys,

I need some help. I have a form with 3 Radio buttons.

Radio Buttions:
1. Document Properties
2. Configuration Properties
3. Both

And one CommandButton, "removeButton" all on a form. My aim is to be able to remove all properties from either Document Properties, Configuration Properties, or all Properties in Document. I am kinda new to programming and have tried looking through the Help Document, but can't figure this one out. Guys, please, can you help me figure this one out... what is wrong with my code?





Private Sub removeButton_Click()

Dim strConfiguration As String

Set swApp = Application.SldWorks
Set swModelDoc = swApp.ActiveDoc

strConfiguration = GetActiveConfiguration(swModelDoc)


If optDocPrp = True Then


' 'REMOVE DOC PROPERTIES LOOP
' Dim arDocPrpNames As Variant
'
' arDocPrpNames = swModelDoc.GetCustomInfoNames2("")
' MsgBox (arDocPrpNames(0))
'
' For i = 0 To arDocPrpNames.Length
' swModelDoc.DeleteCustomInfo2 "", arDocPrpNames(i)
' MsgBox i & " Document Properties Deleted"
' Next

ElseIf optConfigPrp = True Then

'REMOVE CONFIG PROPERTIES LOOP
Dim arConfigPrpNames As Variant

arConfigPrpNames = swModelDoc.GetCustomInfoNames2(strConfiguration)
MsgBox (arConfigPrpNames(0))

For i = 0 To arConfigPrpNames.Length
swModelDoc.DeleteCustomInfo2 "", arConfigPrpNames(i)
MsgBox i & " Configuration Properties Deleted"
Next
Else
'Remove Both Configuration and Document Properties
End If

End Sub
 
Replies continue below

Recommended for you

pyroclasm,

What kind of error message are you getting? That may help you figure it out!

SA
 
pyroclasm,

Now that it is later in the day, I had a chance to look over (and run) your code. One question to first: What version of VB are you programming with as I am not familar with the .Length after the arDocPrpNames and arConfigPrpNames?

First, because I could not use the .Length with VBA, I had to change your code to use the UBOUND function. Next, since you did not supply the code for the GetActiveConfiguration procedure, I had to write my own routine. I also un-commented the code under If optDocPrp = True Then to see if that worked (although I assume you commented it out because you knew that this code worked as is). Now to answer your question. The problem with the code is that under ElseIf optConfigPrp = True Then you did not specify the configuration name for the configuration parameter in the swModelDoc.DeleteCustomInfo2 line. It was trying to delete the custom properties under the Custom tab and not the Configuration Specific tab. Below is your code with my changes in red. I tested it a couple of times with no problems.

SA

Code:
Private Sub removeButton_Click()
    
    Dim strConfiguration As String

    Set swApp = Application.SldWorks
    Set swModelDoc = swApp.ActiveDoc
 
    [COLOR=green]'strConfiguration = GetActiveConfiguration(swModelDoc)[/color]
    [COLOR=red]strConfiguration = swApp.GetActiveConfigurationName(swModelDoc.GetPathName)[/color]
    
    
    If optDocPrp = True Then
    
            
            [COLOR=green]'REMOVE DOC PROPERTIES LOOP[/color]
            Dim arDocPrpNames As Variant

            arDocPrpNames = swModelDoc.GetCustomInfoNames2("")
            MsgBox (arDocPrpNames(0))

            For i = 0 To [COLOR=red]UBound(arDocPrpNames)[/color]
                swModelDoc.DeleteCustomInfo2 "", arDocPrpNames(i)
                MsgBox i & " Document Properties Deleted"
            Next
    
    ElseIf optConfigPrp = True Then
            
            [COLOR=green]'REMOVE CONFIG PROPERTIES LOOP[/color]
            Dim arConfigPrpNames As Variant
            
            arConfigPrpNames = swModelDoc.GetCustomInfoNames2(strConfiguration)
            MsgBox (arConfigPrpNames(0))
            
            For i = 0 To [COLOR=red]UBound(arConfigPrpNames)[/color]
                swModelDoc.DeleteCustomInfo2 [COLOR=red]strConfiguration[/color], arConfigPrpNames(i)
                MsgBox i & " Configuration Properties Deleted"
            Next
    Else
        [COLOR=green]'Remove Both Configuration and Document Properties[/color]
    End If

End Sub

 
I have a finished product that I never posted. I will put it on my website freeware section immediately. I will post back when ready.

[bat]I could be the world's greatest underachiever, if I could just learn to apply myself.[bat]
-SolidWorks API VB programming help
 
Thanks SolidAir. I put the Length property in only because I'm used to coding in Flash and really didn't know what VBA used to do the same thing. Now I know from your example. I commented out the code because it actually caused a conflict. I don't really understand why, but I think it has to do with the "GetCustomInfoNames2("")."

I am slightly confused because when I check the Help Doc for that Method it states this:

===============================
Description

This method returns the names of the custom information defined in the document OR a specified configuration.


Syntax (OLE Automation)

retval = ModelDoc2.GetCustomInfoNames2 ( configuration )


Input:
(BSTR) configuration
Name of the configuration

Return:
(VARIANT) retval
SafeArray containing the names of the custom information

=============================

There was another Method I used to a get property and it said that to select the Custom Document Property, for the Configuration area, simply put double quotes "" and it will know. But with that method, I would already have to know the property name in order to get it. But that is not what I want.

Thanks again for the code, it works for the Configuration Removal, but the Doc one has problems.
 
Hey TheTick, thanks for the download, but it didn't work. I got an error...

Run-time error '9': Subscript out of range!

I'll sift through and study your code later. I like your web site, very useful.

Thanks,

Pyroclasm
 
Hmmm.... macro worked fine here. Written in SW2003, used in SW2004, SW2005, and SW2007. Never seen a "Run-time error '9'" before.
 
pyroclasm,

What kind of error message were you getting when you ran my revised code? Like with Tick's program, I did not have a problem running my code. It deleted the custom properties in both the custom and configuration tabs.

SA
 
Hey Guys,

Ok, there must be something wrong with my computer at work. SolidAir, your code works perfectly at home. But at work I get an error, I don't remeber the exact error, but I'll post it tomarrow when I am in the office. Other than that it works well.

Heres my finished code:

---------------------------------------------

Private Sub removeButton_Click()

Dim strConfiguration As String

Set swApp = Application.SldWorks
Set swModelDoc = swApp.ActiveDoc

'strConfiguration = GetActiveConfiguration(swModelDoc)
strConfiguration = swApp.GetActiveConfigurationName(swModelDoc.GetPathName)


If optDocPrp = True Then
'REMOVE DOC PROPERTIES LOOP
Dim arDocPrpNames As Variant

arDocPrpNames = swModelDoc.GetCustomInfoNames2("")

For i = 0 To UBound(arDocPrpNames)
swModelDoc.DeleteCustomInfo2 "", arDocPrpNames(i)
MsgBox arDocPrpNames(i) & " - Document Property Deleted"
Next

ElseIf optConfigPrp = True Then

'REMOVE CONFIG PROPERTIES LOOP
Dim arConfigPrpNames As Variant

arConfigPrpNames = swModelDoc.GetCustomInfoNames2(strConfiguration)

For i = 0 To UBound(arConfigPrpNames)
swModelDoc.DeleteCustomInfo2 strConfiguration, arConfigPrpNames(i)
MsgBox arConfigPrpNames(i) & " Configuration Properties Deleted"
Next
Else
'Remove Both Configuration and Document Properties
'REMOVE DOC PROPERTIES LOOP
Dim arDocPrpNames1 As Variant

arDocPrpNames1 = swModelDoc.GetCustomInfoNames2("")

For i = 0 To UBound(arDocPrpNames1)
swModelDoc.DeleteCustomInfo2 "", arDocPrpNames1(i)
MsgBox arDocPrpNames1(i) & " - Document Property Deleted"
Next

'REMOVE CONFIG PROPERTIES LOOP
Dim arConfigPrpNames1 As Variant

arConfigPrpNames1 = swModelDoc.GetCustomInfoNames2(strConfiguration)

For i = 0 To UBound(arConfigPrpNames1)
swModelDoc.DeleteCustomInfo2 strConfiguration, arConfigPrpNames1(i)
MsgBox arConfigPrpNames1(i) & " - Configuration Property Deleted"
Next
End If

End Sub

------------------------------------

As for TheTick, I dunno, I tried your form on my computer at home too and I got the same error message. I am running SolidWorks 2006 with the VB that comes with the program. :(

Thanks guys, you really know your coding.
 
I have not actually tested the macro in SW2006. I will have to check.
 
TheTick's macro does what you want. Have you tried Edit Macros (pick macro) > Tools > References > SLDWorks 2006 Type Library?

SW07 SP2.0

Flores
 
Hey guys, Some reason its workin' now at work. SolidAir your code works well, thanks. And TheTick, yours works too now, its a pretty neat macro you have there. The Checkbox is a good idea. I don't know why it had problems the other day.

Thanks again guys for all your help.

Pyroclasm
 
While TheTick's program is really cool. A star to SolidAir for answering pyroclasm's original question of why his program did not work. Enjoy SolidAir.

Regards,

Regg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top