Bwaggoner
Mechanical
- Apr 9, 2007
- 23
This is an example from the Doc. Mgr API help file. We have several files with the custom property of by_date filled out as a text type. I need to change this to a date type. This code works but I am having an issue. If I run the code and check the results by RMB clicking on the file in Win Explorer and selecting properties then custom. I see the new value. If I try to change that back to a text type I get an error "changes could not be saved. The file is marked read-only or may be in use by another app". Then it blows all of the properties away. They do remain in the part file though.
I can't imagine why our users would change the Property this way but I'm guessing someone will.
Any advice would be appreciated.
I can't imagine why our users would change the Property this way but I'm guessing someone will.
Any advice would be appreciated.
Code:
'Option Explicit
Sub main()
On Error GoTo ErrorHandler
' Trap all errors that would cause the program to halt.
Dim Classfac As SwDMClassFactory
Dim swDocMgr As SwDMApplication
Dim swDoc As SwDMDocument
Dim nretval As Long
' Instantiate a SolidWorks Document Manager session.
Set Classfac = CreateObject("SwDocumentMgr.SwDMClassFactory")
Set swDocMgr = Classfac.GetApplication("DM Key") 'Specify your license key
Set swDoc = swDocMgr.GetDocument("Path to part file", swDmDocumentPart, False, nretval) ' Open the file as read-write.
Debug.Print nretval ' Examine this variable to ensure there were no errors opening the file.
Dim vnames As Variant
vnames = swDoc.GetCustomPropertyNames
' Examine vnames in the debugger.
myval = swDoc.GetCustomProperty("by_date", swDmCustomInfoText)
swDoc.DeleteCustomProperty ("by_date")
Debug.Print swDoc.AddCustomProperty("by_date", swDmCustomInfoDate, myval) ' Should print out True, unless the property already exists.
Debug.Print swDoc.GetCustomProperty("by_date", swDmCustomInfoDate) ' Should print out 3/2/1943.
swDoc.Save ' Save the changes made to the custom properties.
swDoc.CloseDoc ' Close the document before exiting.
ErrorHandler:
Debug.Print Err.Description
If Not swDoc Is Nothing Then
swDoc.CloseDoc
End If
swDoc.CloseDoc
Set swDoc = Nothing
Set swDocMgr = Nothing
Set Classfac = Nothing
End Sub