Want macro to save and rename instead of save and overwrite..

I have added a simple macro to take a screenshot to save as Capture.Png and it works but there is two problems

First if i change the view and want to take another screenshot it will replace the first screenshot since it's the same name

so can i rename it with anything like adding a number instead of overwriting ?
so basically want if i ran the macro to create (capture.png) and if i ran it again to create (capture(1).png
Second problem of possible is do i have to specify exact location (ex .\\desktop) for saving ? or can i assign it to save in the assembly folder ?
Thanks in advance

SolidworksApi macros
Answers 3
AB
AB 2018-05-04 Best Answer

I believe the best thing to do is check if the desired file name already exists in the location you're about to write to with the DIR function.

https://www.techonthenet.com/excel/formulas/dir.php

If it exists, then you can append the file name with "- Copy(1)" to match the windows format. Check again if the file name exists and if it does, try "- Copy(2)" and so on until you get a unique file name.

I hope that helps.

DG
DG 2018-05-04

Like ​ said, use an if condition to check the file name exists or not. And accordingly bump the value.

MA
MA 2018-05-04

Thank you for your replies
i get the idea now however not sure how to write it in the code
i read that link but still couldn't understand as this is my first macro

if that is my macro code what should i add and where exactly ?

<p><span style="font-size: 10pt;">Dim swApp As Object</span></p><p></p><p><span style="font-size: 10pt;">Dim Part As Object</span></p><p><span style="font-size: 10pt;">Dim boolstatus As Boolean</span></p><p><span style="font-size: 10pt;">Dim longstatus As Long, longwarnings As Long</span></p><p></p><p><span style="font-size: 10pt;">Sub main()</span></p><p></p><p><span style="font-size: 10pt;">Set swApp = Application.SldWorks</span></p><p></p><p><span style="font-size: 10pt;">Set Part = swApp.ActiveDoc</span></p><p><span style="font-size: 10pt;">boolstatus = swApp.SetUserPreferenceIntegerValue(swUserPreferenceIntegerValue_e.swTiffPrintDPI, 200)</span></p><p><span style="font-size: 10pt;">boolstatus = swApp.SetUserPreferenceIntegerValue(swUserPreferenceIntegerValue_e.swTiffPrintPaperSize, swDwgPaperSizes_e.swDwgPaperBsize)</span></p><p></p><p><span style="font-size: 10pt;">' Save As</span></p><p><span style="font-size: 10pt;">longstatus = Part.SaveAs3("C:\Users\3aBBaS\Desktop\200.PNG", 0, 0)</span></p><p><span style="font-size: 10pt;">End Sub</span></p>