So I have the following macro which automates the "save as DWG" operation by also configuring specific options and then creates a directory to store the DWG file in.
This macro works 100% of the time when the "Export" folder doesn't already exist. But when the "Export" folder already exists, the script may run successfully, but sometimes, it errors out at the following line...
I can't figure out why it randomly works/doesn't work if the directory already exists. Is there a way to rewrite this to check if the directory exists, and if it does exist, to not make the directory, but instead use the sPathName? I'm just not fluent enough to write that out myself.
<p>Dim swApp As SldWorks.SldWorks</p><p>Dim swModel As SldWorks.ModelDoc2</p><p>Dim sFileName As String</p><p>Dim sPathName As String</p><p>Dim strFullPath As String</p><p>Dim bRet As Boolean</p><p>Dim nErrors As Long</p><p>Dim nWarnings As Long</p><p>Dim CustomMap As String</p><p>Dim instance As ISldWorks</p><p>Dim UserPreferenceValue As Integer</p><p>Dim OnFlag As Boolean</p><p></p><p>Sub main()</p><p></p><p>Set swApp = Application.SldWorks</p><p>Set swModel = swApp.ActiveDoc</p><p></p><p></p><p>sFileName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\\") + 1)</p><p>sFileName = Left(sFileName, InStrRev(sFileName, ".") - 1)</p><p></p><p>sPathName = Environ("USERPROFILE") & "\\Desktop\\Export\\"</p><p></p><p>If Dir(sPathName) = "" Then</p><p>MkDir sPathName</p><p>End If</p><p></p><p>sPathName = sPathName & sFileName & ".DWG"</p><p></p><p>CustomMap = "C:\\EPDM_Vault\\Templates\\DWG Mapping Files\\cem-mapping"</p><p></p><p>' Show current settings</p><p>Debug.Print "DxfMapping = " & swApp.GetUserPreferenceToggle(swDxfMapping)</p><p>Debug.Print "DXFDontShowMap = " & swApp.GetUserPreferenceToggle(swDXFDontShowMap)</p><p>Debug.Print "DxfVersion = " & swApp.GetUserPreferenceIntegerValue(swDxfVersion)</p><p>Debug.Print "DxfOutputFonts = " & swApp.GetUserPreferenceIntegerValue(swDxfOutputFonts)</p><p>Debug.Print "DxfMappingFileIndex = " & swApp.GetUserPreferenceIntegerValue(swDxfMappingFileIndex)</p><p>Debug.Print "DxfOutputLineStyles = " & swApp.GetUserPreferenceIntegerValue(swDxfOutputLineStyles)</p><p>Debug.Print "DxfOutputNoScale = " & swApp.GetUserPreferenceIntegerValue(swDxfOutputNoScale)</p><p>Debug.Print "DxfMappingFiles = " & swApp.GetUserPreferenceStringListValue(swDxfMappingFiles)</p><p>Debug.Print "DxfOutputScaleFactor = " & swApp.GetUserPreferenceDoubleValue(swDxfOutputScaleFactor)</p><p>Debug.Print ""</p><p></p><p>' Set specific Version</p><p>swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swDxfVersion, 6</p><p>' Set specific Fonts</p><p>swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swDxfOutputFonts, 1</p><p>' Set specific Fonts</p><p>swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swDxfOutputLineStyles, 1</p><p>' Enable specific Custom Map SOLIDWORKS to DXF/DWG</p><p>swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swDxfMapping, True</p><p>' Custom Map SOLIDWORKS to DXF/DWG - Don't show mapping on each save</p><p>swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swDXFDontShowMap, True</p><p>' Custom Map SOLIDWORKS to DXF/DWG - Map file</p><p>swApp.SetUserPreferenceStringListValue swUserPreferenceStringListValue_e.swDxfMappingFiles, CustomMap</p><p>' Enable Scale output 1:1</p><p>swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swDxfOutputNoScale, True</p><p>' Scale output 1:1 - Base scale - NOT AVAILABLE IN SW2017 API</p><p>' Scale output 1:1 - Warn me if enabled - NOT AVAILABLE IN SW2017 API</p><p>' Enable End Point Merging</p><p>swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swDxfEndPointMerge, True</p><p>' End Point Merging Tolerance</p><p>swApp.SetUserPreferenceDoubleValue swUserPreferenceDoubleValue_e.swDxfMergingDistance, 0</p><p>' End Point Merging - High quality DWG export</p><p>swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swDXFHighQualityExport, False</p><p>' Spline export options - Export all splines as splines</p><p>swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swDxfExportSplinesAsSplines, True</p><p>' Spline export options - Export all splines as polylines</p><p>swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swDxfExportSplinesAsSplines, False</p><p>' Multiple sheet drawing - Export active sheet only</p><p>swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swDxfMultiSheetOption, True</p><p>' Multiple sheet drawing - Export all sheets to separate files</p><p>swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swDxfMultiSheetOption, False</p><p>' Multiple sheet drawing - Export all sheets to one file</p><p>swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swDxfMultiSheetOption, False</p><p>' Multiple sheet drawing - Export all drawing sheets to paper space</p><p>swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swDxfExportAllSheetsToPaperSpace, False</p><p></p><p></p><p>'Do you want to export entities on all layers? <Yes/No></p><p>swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swDXFExportHiddenLayersOn, True</p><p>'Do not ask again</p><p>swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swDXFExportHiddenLayersWarnIsOn, True</p><p></p><p></p><p>' Use Solidworks Layers</p><p>swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swDxfUseSolidworksLayers, True</p><p></p><p></p><p>' Turn off showing of map</p><p>bShowMap = swApp.GetUserPreferenceToggle(swDXFDontShowMap)</p><p>swApp.SetUserPreferenceToggle swDXFDontShowMap, False</p><p></p><p>swApp.SetUserPreferenceStringListValue swDxfMappingFiles, CustomMap</p><p>Index = swApp.GetUserPreferenceIntegerValue(swDxfMappingFileIndex)</p><p></p><p>If (Index = -1) Then</p><p>swApp.SetUserPreferenceIntegerValue swDxfMappingFileIndex, 0</p><p>End If</p><p></p><p>bRet = swModel.SaveAs4(sPathName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings)</p><p></p><p>If bRet = False Then</p><p>nRetval = swApp.SendMsgToUser2("Problems saving file.", swMbWarning, swMbOk)</p><p>End If</p><p></p><p>' Restore old setting</p><p>swApp.SetUserPreferenceToggle swDXFDontShowMap, bShowMap</p><p></p><p></p><p>End Sub</p><p></p>SolidworksApi/macros
Shaun,
Just looking at it, I see a possible issue if you run the macro twice, sPathName doesn't appear get reset, so it may be carrying the DWG with it on the second run.
bRet = swModel.SaveAs4(sPathName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings)
I also prefer using File Scripting Object for folders/files existing. You need to set a reference to Microsoft Scripting Runtime.
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FolderExists(sPathName ) = False Then
fs.CreateFolder sPathName
End If
My suggestion would be to not reuse sPathName and maybe create a new variable for it. sPathDWG or something.
EDIT: Sorry... I see the reset at the top, so maybe not the issue I thought. fso may still help.
Yep, that fixed it. Noticed I made an error with Deepaks code above too, but after fixing that your suggestion resolved the problem. Thanks.
Finished code for anyone to use/recycle.
<p> Dim swApp As SldWorks.SldWorks</p><p> Dim swModel As SldWorks.ModelDoc2</p><p> Dim sFileName As String</p><p> Dim sPathName As String</p><p> Dim strFullPath As String</p><p> Dim bRet As Boolean</p><p> Dim nErrors As Long</p><p> Dim nWarnings As Long</p><p> Dim CustomMap As String</p><p> Dim instance As ISldWorks</p><p> Dim UserPreferenceValue As Integer</p><p> Dim OnFlag As Boolean</p><p> </p><p> Sub main()</p><p> </p><p> Set swApp = Application.SldWorks</p><p> Set swModel = swApp.ActiveDoc</p><p> </p><p> </p><p> sFileName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)</p><p> sFileName = Left(sFileName, InStrRev(sFileName, ".") - 1)</p><p> </p><p> sPathName = Environ("USERPROFILE") & "\Desktop\Export"</p><p> </p><p> If Dir(sPathName, vbDirectory) = "" Then</p><p> MkDir sPathName</p><p> End If</p><p></p><p></p><p> sPathName = sPathName & "\" & sFileName & ".DWG"</p><p> </p><p> CustomMap = "C:\EPDM_Vault\Templates\DWG Mapping Files\cem-mapping"</p><p> </p><p> ' Show current settings</p><p> Debug.Print "DxfMapping = " & swApp.GetUserPreferenceToggle(swDxfMapping)</p><p> Debug.Print "DXFDontShowMap = " & swApp.GetUserPreferenceToggle(swDXFDontShowMap)</p><p> Debug.Print "DxfVersion = " & swApp.GetUserPreferenceIntegerValue(swDxfVersion)</p><p> Debug.Print "DxfOutputFonts = " & swApp.GetUserPreferenceIntegerValue(swDxfOutputFonts)</p><p> Debug.Print "DxfMappingFileIndex = " & swApp.GetUserPreferenceIntegerValue(swDxfMappingFileIndex)</p><p> Debug.Print "DxfOutputLineStyles = " & swApp.GetUserPreferenceIntegerValue(swDxfOutputLineStyles)</p><p> Debug.Print "DxfOutputNoScale = " & swApp.GetUserPreferenceIntegerValue(swDxfOutputNoScale)</p><p> Debug.Print "DxfMappingFiles = " & swApp.GetUserPreferenceStringListValue(swDxfMappingFiles)</p><p> Debug.Print "DxfOutputScaleFactor = " & swApp.GetUserPreferenceDoubleValue(swDxfOutputScaleFactor)</p><p> Debug.Print ""</p><p> </p><p> ' Set specific Version</p><p> swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swDxfVersion, 6</p><p> ' Set specific Fonts</p><p> swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swDxfOutputFonts, 1</p><p> ' Set specific Fonts</p><p> swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swDxfOutputLineStyles, 1</p><p> ' Enable specific Custom Map SOLIDWORKS to DXF/DWG</p><p> swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swDxfMapping, True</p><p> ' Custom Map SOLIDWORKS to DXF/DWG - Don't show mapping on each save</p><p> swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swDXFDontShowMap, True</p><p> ' Custom Map SOLIDWORKS to DXF/DWG - Map file</p><p> swApp.SetUserPreferenceStringListValue swUserPreferenceStringListValue_e.swDxfMappingFiles, CustomMap</p><p> ' Enable Scale output 1:1</p><p> swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swDxfOutputNoScale, True</p><p> ' Scale output 1:1 - Base scale - NOT AVAILABLE IN SW2017 API</p><p> ' Scale output 1:1 - Warn me if enabled - NOT AVAILABLE IN SW2017 API</p><p> ' Enable End Point Merging</p><p> swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swDxfEndPointMerge, True</p><p> ' End Point Merging Tolerance</p><p> swApp.SetUserPreferenceDoubleValue swUserPreferenceDoubleValue_e.swDxfMergingDistance, 0</p><p> ' End Point Merging - High quality DWG export</p><p> swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swDXFHighQualityExport, False</p><p> ' Spline export options - Export all splines as splines</p><p> swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swDxfExportSplinesAsSplines, True</p><p> ' Spline export options - Export all splines as polylines</p><p> swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swDxfExportSplinesAsSplines, False</p><p> ' Multiple sheet drawing - Export active sheet only</p><p> swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swDxfMultiSheetOption, True</p><p> ' Multiple sheet drawing - Export all sheets to separate files</p><p> swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swDxfMultiSheetOption, False</p><p> ' Multiple sheet drawing - Export all sheets to one file</p><p> swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swDxfMultiSheetOption, False</p><p> ' Multiple sheet drawing - Export all drawing sheets to paper space</p><p> swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swDxfExportAllSheetsToPaperSpace, False</p><p></p><p></p><p> 'Do you want to export entities on all layers? <Yes/No></p><p> swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swDXFExportHiddenLayersOn, True</p><p> 'Do not ask again</p><p> swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swDXFExportHiddenLayersWarnIsOn, True</p><p></p><p></p><p> ' Use Solidworks Layers</p><p> swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swDxfUseSolidworksLayers, True</p><p></p><p></p><p> ' Turn off showing of map</p><p> bShowMap = swApp.GetUserPreferenceToggle(swDXFDontShowMap)</p><p> swApp.SetUserPreferenceToggle swDXFDontShowMap, False</p><p> </p><p> swApp.SetUserPreferenceStringListValue swDxfMappingFiles, CustomMap</p><p> Index = swApp.GetUserPreferenceIntegerValue(swDxfMappingFileIndex)</p><p> </p><p> If (Index = -1) Then</p><p> swApp.SetUserPreferenceIntegerValue swDxfMappingFileIndex, 0</p><p> End If</p><p> </p><p> bRet = swModel.SaveAs4(sPathName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings)</p><p> </p><p> If bRet = False Then</p><p> nRetval = swApp.SendMsgToUser2("Problems saving file.", swMbWarning, swMbOk)</p><p> End If</p><p> </p><p> ' Restore old setting</p><p> swApp.SetUserPreferenceToggle swDXFDontShowMap, bShowMap</p><p> </p><p> </p><p> End Sub</p>After some looking around other forums, it appears the following should fix your issue.
Add ", vbDirectory" to your Dir statement to tell it you're checking if the directory exists. Otherwise, I believe it looks for files as a default so it returns that the file doesn't exist and tries to create a directory that is already there causing the error.
Hi Danniel:
I attempted your suggestion but I can't get it to work. Any recommendations?
Highlighted is what I changed/added.
Deepak:
Yeah, the
sPathNamesPathName
was a typo... it's not like that in my code.
I did make the other changes; however, but I get the same error.
If I press debug, I get this...
Regards,
Shaun
Not sure if this is issue or it popped up while you pasted/formatted the codes
Also try changing these two line (in bold below) to
<p><strong>sPathName = Environ("USERPROFILE") & "\Desktop\Export\" </strong></p><p> </p><p>If Dir(sPathName) = "" Then </p><p>MkDir sPathName </p><p>End If </p><p> </p><p><strong>sPathName = sPathName & sFileName & ".DWG" </strong></p>to
<p>sPathName = Environ("USERPROFILE") & "\Desktop\<strong>Export"</strong> </p><p> </p><p>If Dir(sPathName) = "" Then </p><p>MkDir sPathName </p><p>End If </p><p> </p><p>sPathName = sPathName <strong>& "\" &</strong> sFileName & ".DWG" </p>