-
Re: Run a macro on all files in a folder
Andreas Rhomberg Feb 14, 2017 9:42 AM (in response to Axel Hedman)have you looked at TASK, http://centralinnovation.com.au/ for writing all the Properties to the files.
It is a free program and works well.
-
Re: Run a macro on all files in a folder
John Stoltzfus Feb 14, 2017 9:51 AM (in response to Axel Hedman)Don't you use Custom Property Tab Builder ??
I would use the attached macro written by Markku Lehtola
-
Re: Run a macro on all files in a folder
Deepak Gupta Feb 14, 2017 10:02 AM (in response to Axel Hedman)Axel, yes you should be able to use the #TASK tool to run your own macros and can run on the entire folder (can also set which file type to run the macro on).
I've also reduced the size of your macro. Add3 will add/update the properties with the required values.
Option Explicit Dim swApp As SldWorks.SldWorks Dim swModel As SldWorks.ModelDoc Dim swCustProp As CustomPropertyManager Sub main() Set swApp = Application.SldWorks Set swModel = swApp.ActiveDoc Set swCustProp = swModel.Extension.CustomPropertyManager("") 'Check and Add/Update Properties swCustProp.Add3 "Number", 30, swModel.GetTitle, 1 swCustProp.Add3 "DrawnBy", 30, "AHe / Devex", 1 swCustProp.Add3 "Surface finish", 30, "Ready for paint", 1 swCustProp.Add3 "General Tolerances", 30, "SS-ISO 2768-1-m", 1 swCustProp.Add3 "Project", 30, "P0988", 1 swModel.Save End Sub
-
Re: Run a macro on all files in a folder
Axel Hedman Feb 14, 2017 10:47 AM (in response to Axel Hedman)Okay I tried #TASK but so far it does not seem to work. It only says that the part was not modified saving is skipped.
However, when searching I found that this macro are supposed to open all parts in a specific folder
- Dim swApp As SldWorks.SldWorks
- Dim swDoc As ModelDoc2
- Dim fileerror As Long
- Dim filewarning As Long
- Const folder As String = "C:\SolidWorks Training Files\Essentials\Lesson01\Case Study\"
- Dim files As Variant
- Sub main()
- Set swApp = Application.SldWorks
- files = Dir(folder & "*.sldprt", vbNormal)
- Do While files <> ""
- swApp.OpenDoc6 folder & files, swDocPART, 0, "", fileerror, filewarning
- files = Dir
- Loop
- End Sub
But I don´t get what is what, can i put my macro somwhere in that macro?
That macro is from this thread: Run Macro in Task Scheduler
-
Re: Run a macro on all files in a folder
Deepak Gupta Feb 14, 2017 11:09 AM (in response to Axel Hedman)Not sure why #TASK would fail. Anyway try these codes
Option Explicit Dim swApp As SldWorks.SldWorks Dim swModel As SldWorks.ModelDoc2 Dim sFileName As String Dim Path As String Dim nErrors As Long Dim nWarnings As Long Dim swCustProp As CustomPropertyManager Sub main() On Error Resume Next Set swApp = Application.SldWorks Path = "C:\SolidWorks Training Files\Essentials\Lesson01\Case Study\" 'Change path here and make sure you include \ at end of path. sFileName = Dir(Path & "*.sldprt") Do Until sFileName = "" Set swModel = swApp.OpenDoc6(Path + sFileName, swDocPART, swOpenDocOptions_Silent, "", nErrors, nWarnings) Set swModel = swApp.ActiveDoc Set swCustProp = swModel.Extension.CustomPropertyManager("") 'Check and Add/Update Properties swCustProp.Add3 "Number", 30, swModel.GetTitle, 1 swCustProp.Add3 "DrawnBy", 30, "AHe / Devex", 1 swCustProp.Add3 "Surface finish", 30, "Ready for paint", 1 swCustProp.Add3 "General Tolerances", 30, "SS-ISO 2768-1-m", 1 swCustProp.Add3 "Project", 30, "P0988", 1 swModel.Save3 swSaveAsOptions_Silent, nErrors, nWarnings swApp.CloseDoc swModel.GetTitle Set swModel = Nothing sFileName = Dir Loop End Sub
-
Re: Run a macro on all files in a folder
Artem Taturevych Feb 14, 2017 5:39 PM (in response to Axel Hedman)Hi Axel,
#TASK only saves the file when it sees it was modified. The AddCustomInfo2 API doesn't mark the model as dirty (i.e. modified). You can check this by running the macro on a active saved model and you will see that the * symbol is not added to the name and SOLIDWORKS doesn't ask you to save the model when you close it. You can forcibly set the dirty flag using SetSaveFlag API. In this case #TASK will successfully save the modified file. You simply need to add one line to the end of your macro.
If swModel.CustomInfo("Project") = "" Then
swModel.AddCustomInfo2 "Project", swCustomInfoText, "P0988"
Else
swModel.CustomInfo("Project") = "P0988"
End If
swModel.SetSaveFlag
Thanks,
Atrem
-
Re: Run a macro on all files in a folder
Axel Hedman Feb 17, 2017 8:09 AM (in response to Axel Hedman)Alright! I have started to get things going here!
I use the neat code i got from Deepak Gupta
- Option Explicit
- Dim swApp As SldWorks.SldWorks
- Dim swModel As SldWorks.ModelDoc
- Dim swCustProp As CustomPropertyManager
- Sub main()
- Set swApp = Application.SldWorks
- Set swModel = swApp.ActiveDoc
- Set swCustProp = swModel.Extension.CustomPropertyManager("")
- 'Check and Add/Update Properties
- swCustProp.Add3 "Number", 30, swModel.GetTitle, 1
- swCustProp.Add3 "DrawnBy", 30, "AHe / Devex", 1
- swCustProp.Add3 "Surface finish", 30, "Ready for paint", 1
- swCustProp.Add3 "General Tolerances", 30, "SS-ISO 2768-1-m", 1
- swCustProp.Add3 "Project", 30, "P0988", 1
- swModel.Save
- End Sub
And by adding the swModel.SetSaveFlag I got from Artem Taturevych I got it to work with #TASK
However! Now I want to make a new nuber from the existing part number:
I want 012.0067-00 to become 120067S0 ie I want to remove the first "0", then remove the "." and then change the "-0" to "S"
I have seen that its possible but i don´t get how it works :/
-
Re: Run a macro on all files in a folder
Deepak Gupta Feb 19, 2017 12:10 PM (in response to Axel Hedman)You can use Replace function.
Replace(Replace(Replace(swModel.GetTitle, "-0", "S"), ".", ""), "0", "", 1, 1)
-
Re: Run a macro on all files in a folder
Axel Hedman Feb 20, 2017 7:15 AM (in response to Deepak Gupta)Oh okay, but how do I implement that in a swCustProp.Add3 statement?
-
Re: Run a macro on all files in a folder
Deepak Gupta Feb 20, 2017 8:32 AM (in response to Axel Hedman)Here is how you can use it
swCustProp.Add3 "Number", 30, Replace(Replace(Replace(swModel.GetTitle, "-0", "S"), ".", ""), "0", "", 1, 1), 1
-
-