AnsweredAssumed Answered

Running Excel code from SW

Question asked by Danny Holpin on Sep 12, 2019
Latest reply on Sep 12, 2019 by Danny Holpin

I am trying to import Acad .dwg files then sort them in to a folder. The current work flow is run the task scheduler to import .DWG files to .SLDRW. Then I have written some code in excel that runs from a button to sort all the .dwg (plus any .err/.log files that were created during the import) into a new folder.

 

I think its possible to run this code from solid works so that I can use the task scheduler to run it out of hours, along side the import?

 

I tried to copy the code from excel to SW but i get the "user defined type not defined" error on declaring the "Dim diaFolder As FileDialog" Line. I guess this feature is not available in SW?

 

Any suggestions of a better way of doing this? Also would it be possible to run the code on each sub folder? rather than having to visit each folder manually?

 

Here is my excel code:

Private Sub CommandButton1_Click()
'move all specified files from FromPath to ToPath.
'Note: It will create the folder ToPath for you
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim FileExt As String
Dim FNames As String

Dim diaFolder As FileDialog
Dim selected As Boolean
Dim FldCheck As String

' Open the file dialog
Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
diaFolder.AllowMultiSelect = True
selected = diaFolder.Show

'If selected Then
' MsgBox diaFolder.SelectedItems(1)
'End If

FromPath = diaFolder.SelectedItems(1)



'-----

ToPath = FromPath & "\Original DWGs " & Format(Date, "dd-mm-yy") '<< Change only the destination folder


Debug.Print ToPath


FileExt = "*.dwg" '<< Change

If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If

FNames = Dir(FromPath & FileExt)
If Len(FNames) = 0 Then
MsgBox "No .dwg files in " & FromPath
'Exit Sub
GoTo Err
End If

Set FSO = CreateObject("scripting.filesystemobject")


If FSO.FolderExists(ToPath) = False Then
FSO.CreateFolder (ToPath)
End If

FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath
'MsgBox "You can find the files from " & FromPath & " in " & ToPath

'----

Err:
FileExt = "*.err" '<< Change

If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If

FNames = Dir(FromPath & FileExt)
If Len(FNames) = 0 Then
'MsgBox "No .err files in " & FromPath
'Exit Sub
GoTo Bak
End If

Set FSO = CreateObject("scripting.filesystemobject")

If FSO.FolderExists(ToPath) = False Then
FSO.CreateFolder (ToPath)
End If

FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath


'---
Bak:
FileExt = "*.bak" '<< Change

If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If

FNames = Dir(FromPath & FileExt)
If Len(FNames) = 0 Then
'MsgBox "No .bak files in " & FromPath
'Exit Sub
GoTo Log
End If

Set FSO = CreateObject("scripting.filesystemobject")

If FSO.FolderExists(ToPath) = False Then
FSO.CreateFolder (ToPath)
End If

FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath

'---
Log:
FileExt = "*.log" '<< Change

If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If

FNames = Dir(FromPath & FileExt)
If Len(FNames) = 0 Then
'MsgBox "No .log files in " & FromPath
Exit Sub
End If

Set FSO = CreateObject("scripting.filesystemobject")

If FSO.FolderExists(ToPath) = False Then
FSO.CreateFolder (ToPath)
End If

FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath


'Call Shell("explorer.exe " & ToPath, vbNormalFocus) 'opens new foder to check
'FldCheck = VBA.FileSystem.Dir("\\sbs\Drawings\DRAWINGS-MASTERS\ARCHIVE\CIRCUIT DRAWINGS\E2000 Circuit Drawings\85100004 ELECTRICAL CIRCUIT\GERMAN\Original DWGs 05-09-19")

'If FldCheck = VBA.Constants.vbNullString Then
'MsgBox "Something went wobbly"
'Else
'MsgBox "Folder created and files moved successfully"
'End If

Set diaFolder = Nothing
End Sub

Outcomes