It certainly is possible. You can use IModelDoc2::CustomInfo to check & get the properties. You would then use the API to open Excel fill in the appropriate cells, and grab the Filenumber. Then you can use IModelDoc2::SaveAs to save the file with the new name. You could even use the API to delete the old file but you would need to update the any references of any documents that used the one you are working on.
As a beginner you may want to take it one step at a time. Start by transferring property values to a couple of string variables. For a simplified prompt method for empty properties you can use the native VBA input box.
I've been doing a few Google / Solidworks forum searches and I've been able to get my information transfer into excel now I just need to get Solidworks call the macro in excel to run. This is my code so far but it's only working up till line "xlBook.RunMacro "AddNewPart3"" which i'm working on right now. I guess the biggest question I have is, how exactly does this work "IModelDoc2::SaveAs"?
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim swModelDocExt As ModelDocExtension
Dim swCustProp As CustomPropertyManager
Dim val As String
Dim valout As String
Dim bool As Boolean
Dim FileNumber As String
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xRow As Integer
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Open("Excel File")
Set xlSheet = xlBook.Sheets("FNS")
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swModelDocExt = swModel.Extension
Set swCustProp = swModelDocExt.CustomPropertyManager("")
bool = swCustProp.Get4("Category Number", False, val, valout)
xlSheet.Cells(2, 1).Value = val
bool = swCustProp.Get4("B/P No.", False, val, valout)
xlSheet.Cells(2, 2).Value = val
bool = swCustProp.Get4("Description", False, val, valout)
xlSheet.Cells(2, 3).Value = val
FileNumber = xlBook.Sheets("FNS").Cells(1, 1)
swModel.AddCustomInfo3 "", "File Number", swCustomInfoText, FileNumber
It works the save as if you were to do a normal save as. It saves the currently open document as a new document.
Open the API help also. There is a lot more information in there and much easier to find what you are looking for.
Okay I've looked in the API help, I've found the information and I have my code as the following
Dim instance as IModelDoc
Dim bool as Boolean
bool = instance.SaveAs(FileNumber)
Now the issue is I haven't set "instance" and I'm not to sure how to.
Set instance = Application.SldWorks.SaveAs
That didn't work.
Wouldn't it be the same model document you were retrieving the property values from. Use:
The reason you got the error you did is the SaveAs returns a boolean True or False. You can't set a ModelDoc2 object equal to it. You have 2 options:
Set instance = swModel
bool = instance.SaveAs(FileNumber)
bool = swModel.SaveAs(FileNumber)
The only reason to use the "bool = " part of the code is if you wish to verify the SaveAs function actually saved. If bool was True, then it worked. If bool is False, then it didn't. The line I wrote before ("swModel.SaveAs FileNumber") is what you would type if you weren't going to verify the save action.
I've tried both ways, I've even found new code, my code runs but it doesn't actually save anything. Here is the last set of code I tried.
PathName = swModel.GetPathName
Filepath = Left(PathName, InStr(PathName, "\"))
If (swModel.GetType = swDocPART) Then
swModel.SaveAs (Filepath + FileNumber + ".sldprt")
ElseIf (swModel.GetType = swDocASSEMBLY) Then
swModel.SaveAs (Filepath + FileNumber + ".sldasm")
If you are trying to save it in the same folder as it currently is in, use:
Filepath = Left(PathName, InStrRev(PathName, "\"))
Otherwise, you are saving in the root of the drive from which you are currently working.
Never Mind, changed my path name and it worked perfect. Thanks.