AnsweredAssumed Answered

Save Files Into New Folder:Solidworks VBA Macro

Question asked by Andrew Lamoreaux on Aug 14, 2020
Latest reply on Aug 18, 2020 by Andrew Lamoreaux

I'm looking to make a macro that generates parts and saves them to a new folder. I've figured out how to define the part and folder names based on user input, but I cannot find a way to create a new folder in Solidworks API. Is there a command that can do this? Looking online I found this link How to: Create a Directory - Visual Basic | Microsoft Docs, which has this code below for creating folders. I cannot find an in context example showing other details needed to use this code.


My.Computer.FileSystem.CreateDirectory( "C:\Documents and Settings\All Users\Documents\NewDirectory")  


The file below was my attempt at using this command but i get a an error "Run-time error '424': object required"


Also, This seems like it should be a really basic thing to do with code but I couldn't find a good resource for this. Is there a good place to go to fine basic information like this?


Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()
'initialive program
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc

'define part and folder names
Dim FolderName As String
FolderName = "Job Folder"
Dim FileName As String
FileName = "job file"

'Create string variables for saving
Dim Savetofolder As String
Dim Saveto As String
Savetofolder = "C:\Users\shawnk\Desktop\" & FolderName
Saveto = "C:\Users\shawnk\Desktop\" & FolderName & "\" & FileName & ".SLDPRT"

'Create New Folder
My.Computer.FileSystem.createdirectory ("Savetofolder")

'Save part
longstatus = Part.SaveAs3("Saveto", 0, 0)
Part.ClearSelection2 True
End Sub



Thank you for reading!