2 Replies Latest reply on Dec 18, 2012 1:10 PM by George Bon

    EPDM Tasks: How to add macro code to a convert task

    George Bon

      Hi all,

       

      I would like to create an EPDM task that inserts a block on SW drawings and then exports them to a DWG format.

       

      We have a macro that inserts the block working on its own, and we have the EPDM convert to DWG task working as well.

       

      The problem is when trying to insert the macro code in the task, things do not work as expected and it looks like there is no way of debugging the task code. I am getting a log error as follows:

      Error while converting file 'C:\EPDM\2012048\IPD\Drawings\380\ML380-C-045.SLDDRW':

      An unexpected error occurred while executing the generated script. Script syntax error?

      Error number: 438

      Error description: 'Object doesn't support this property or method'

       

      Here is the modified convert task code (the sub 'Applystamp' is the only modification):

       

      Dim swApp As Object
      Dim swModel As SldWorks.ModelDoc2
      Dim swDrawing As SldWorks.DrawingDoc
      Dim swAssembly As SldWorks.AssemblyDoc
      Dim swExtension As SldWorks.ModelDocExtension
      Dim swConfMgr As SldWorks.ConfigurationManager
      Dim swPDFExport As SldWorks.ExportPdfData
      Dim swDocSpecification As SldWorks.DocumentSpecification
      Dim errors As Long
      Dim warnings As Long

      Private Declare Function SHCreateDirectoryEx Lib "shell32" Alias "SHCreateDirectoryExA" (ByVal hwnd As Long, ByVal pszPath As String, ByVal psa As Any) As Long
      Private Declare Function PathIsRelative Lib "shlwapi.dll" Alias "PathIsRelativeA" (ByVal pszPath As String) As Long

      Function PathAppend(path, more) As String
          If Not Right(path, 1) = "\" Then
              path = path & "\"
          End If
          If Left(more, 1) = "\" Then
              more = Mid(more, 2)
          End If
          PathAppend = path & more
      End Function

      Sub Log(message)
          Dim errorLogFolder As String
          Dim errorLogPath As String
          ' Determine error log output path
          errorLogFolder = "[ErrorLogPath]"
         
          ' Trim \ from the start
          If Left(errorLogFolder, 1) = "\" Then
              errorLogFolder = Mid(errorLogFolder, 2)
          End If

          ' Build full root
          If PathIsRelative( errorLogFolder ) = 1 Then
              errorLogPath = PathAppend("<VaultPath>", errorLogFolder)
          Else
              errorLogPath = errorLogFolder
          End If
         
          ' Create directory if not exists
          SHCreateDirectoryEx ByVal 0&, errorLogPath, ByVal 0&
          errorLogPath = PathAppend(errorLogPath, "<TaskInstanceGuid>.log")

          ' Write error to output file
          Open errorLogPath For Append As #1
          Print #1, message
          Close #1
      End Sub

      Sub CreatePath(path)
          ' Create directory if not exists
          If SHCreateDirectoryEx(ByVal 0&, path, ByVal 0&) = 0 Then
              ' Create temp file
              Open path & "~$holder" For Append As #1
              Close #1
          End If
      End Sub

      Sub KillHolder(path)
          On Error Goto Ignore
          Kill path & "~$holder"
      Ignore:
      End Sub

      Function GetExtension(docType, fileFormat)
          first = InStr(1, fileFormat, "(")
          last = InStr(first, fileFormat, ")")
          extensions = Mid(fileFormat, first + 1, last - first - 1)
         
          If InStr(1, extensions, ";") > 0 Then
              Dim all As Variant
              all = Split(extensions, ";")
            
              If UBound(all) >= docType - 1 Then
                  ext = all(docType - 1)
              Else
                  ext = "*." ' Nothing
              End If
          Else
              ext = extensions
          End If
         
          GetExtension = Mid(Trim(ext), 2)
      End Function

      Sub SetConversionOptions(ext)
          ' PDF options
          If LCase(ext) = ".pdf" Then
              swApp.SetUserPreferenceToggle swPDFExportInColor, [PdfInColor]
              swApp.SetUserPreferenceToggle swPDFExportEmbedFonts, [PdfEmbedFonts]
              swApp.SetUserPreferenceToggle swPDFExportHighQuality, [PdfHighQuality]
              swApp.SetUserPreferenceToggle swPDFExportPrintHeaderFooter, [PdfPrintHeaderFooter]
              swApp.SetUserPreferenceToggle swPDFExportUseCurrentPrintLineWeights, [PdfUsePrinterLineWeights]
          ' IGES
          ElseIf LCase(ext) = ".igs" Then
              swApp.SetUserPreferenceToggle swIGESExportSolidAndSurface, [IgesExportSolidSurface]
              swApp.SetUserPreferenceIntegerValue swIGESRepresentation, [IgesRepresentation]
              swApp.SetUserPreferenceToggle swIGESExportAsWireframe, [IgesExportWireframe]
              swApp.SetUserPreferenceIntegerValue swIGESCurveRepresentation, [IgesCurveRepresentation]
              swApp.SetUserPreferenceIntegerValue swIGESSystem, [IgesSystem]
              swApp.SetUserPreferenceToggle swIGESExportFreeCurves, [IgesExportFreeCurves]
              swApp.SetUserPreferenceToggle swIGESExportSketchEntities, [IgesExportSketchEntities]
              swApp.SetUserPreferenceToggle swIGESHighTrimCurveAccuracy, [IgesHighCurveAccuracy]
              swApp.SetUserPreferenceToggle swIGESComponentsIntoOneFile, [IgesComponentsIntoOneFile]
              swApp.SetUserPreferenceToggle swIGESFlattenAssemHierarchy, [IgesFlattenAssemblyHierarchy]
          ' ACIS
          ElseIf LCase(ext) = ".sat" Then
              swApp.SetUserPreferenceIntegerValue swAcisOutputGeometryPreference, [AcisGeometry]
              swApp.SetUserPreferenceIntegerValue swOutputVersion, [AcisVersion]
              swApp.SetUserPreferenceIntegerValue swAcisOutputUnits, [AcisOutputAsUnit]
          ' STEP
          ElseIf LCase(ext) = ".step" Then
              swApp.SetUserPreferenceIntegerValue swAcisOutputGeometryPreference, [StepGeometry]
              swApp.SetUserPreferenceIntegerValue swStepAP, [StepVersion]
          ' Parasolid
          ElseIf LCase(ext) = ".x_t" Or LCase(ext) = ".x_b" Then
              swApp.SetUserPreferenceIntegerValue swParasolidOutputVersion, [ParasolidVersion]
              swApp.SetUserPreferenceToggle swXTAssemSaveFormat, [ParasolidFlattenHierarchy]
          ' VRML
          ElseIf LCase(ext) = ".wrl" Then
              swApp.SetUserPreferenceIntegerValue swExportVrmlUnits, [VrmlOutputAsUnit]
              swApp.SetUserPreferenceToggle swExportVrmlAllComponentsInSingleFile, [VrmlSaveAssemblyAsOneFile]
          ' STL
          ElseIf LCase(ext) = ".stl" Then
              swApp.SetUserPreferenceToggle swSTLBinaryFormat, [StlOutputAs]
              swApp.SetUserPreferenceIntegerValue swExportStlUnits, [StlOutputAsUnit]
              swApp.SetUserPreferenceIntegerValue swSTLQuality, [StlQuality]
              swApp.SetUserPreferenceToggle swSTLDontTranslateToPositive, [StlDontTranslatePositive]
              swApp.SetUserPreferenceToggle swSTLComponentsIntoOneFile, [StlComponentsIntoOneFile]
              swApp.SetUserPreferenceToggle swSTLCheckForInterference, [StlCheckForInterferences]
          ' TIF or PSD
          ElseIf LCase(ext) = ".tif" Or LCase(ext) = ".psd" Then
              swApp.SetUserPreferenceIntegerValue swTiffImageType, [TifImageType]
              swApp.SetUserPreferenceIntegerValue swTiffCompressionScheme, [TifCompressionScheme]
          ' eDrawings
          ElseIf LCase(ext) = ".eprt" Or LCase(ext) = ".easm" Or LCase(ext) = ".edrw" Then
              swApp.SetUserPreferenceToggle swEDrawingsOkayToMeasure, [EdrwOkayToMeasure]
              swApp.SetUserPreferenceToggle swEDrawingsExportSTLOkay, [EdrwAllowExportOfSTL]
              swApp.SetUserPreferenceToggle swEDrawingsSaveShadedDataInDrawings, [EdrwSaveShadedData]
              swApp.SetUserPreferenceToggle swEDrawingsSaveBOM, [EdrwSaveBOM]
              swApp.SetUserPreferenceToggle swEDrawingsSaveAnimationOkay, [EdrwSaveMotionStudies]
              swApp.SetUserPreferenceToggle swEDrawingsSaveAnimationToAllConfigs, [EdrwSaveMotionStudiesToAllConfs]
              swApp.SetUserPreferenceToggle swEDrawingsSaveAnimationRecalculate, [EdrwRecalcMotionStudies]
          End If
      End Sub

      Function GetFullFileName(convFileName, conf, i, itemCount)
          ' Configuration name may include backslash. Remove it since otherwise saving will
          ' fail due a missing directory
          conf = Replace(conf, "\", "")
          conf = Replace(conf, "/", "")
         
          finalFileName = Replace(convFileName, "<Configuration>", conf)
         
          ' If no configuration
          If finalFileName = convFileName And itemCount > 0 Then
              finalFileName = Left(convFileName, InStrRev(convFileName, ".") - 1) & "_" & i & Mid(convFileName, InStrRev(convFileName, "."))
          End If
         
          ' Remove illegal characters from filename
          finalFileName = Replace(finalFileName, "<", "")
          finalFileName = Replace(finalFileName, ">", "")
          finalFileName = Left(finalFileName, 2) + Replace(finalFileName, ":", "", 3) ' Don't start from begin since drive has :
          finalFileName = Replace(finalFileName, "*", "")
          finalFileName = Replace(finalFileName, "?", "")
          finalFileName = Replace(finalFileName, """", "")
          finalFileName = Replace(finalFileName, "|", "")
         
          GetFullFileName = finalFileName
      End Function

      Sub Convert(docFileName)
         
          ' Constants for some SolidWorks error/warning returns that may be encountered during a convert operation.       
          Const swerr_InvalidFileExtension = 256   ' the file extension differs from the SW document type.
          Const swerr_SaveAsNotSupported = 4096    ' the options selected for this convert aren't supported, output may be incomplete.
          Const swwarn_MissingOLEObjects = 512     ' the document contains OLE objects and must be opened and converted in SolidWorks.

          ' Determine type of SolidWorks file based on file extension
          If LCase(Right(docFileName, 7)) = ".sldprt" Or LCase(Right(docFileName, 4)) = ".prt" Then
              docType = swDocPART
          ElseIf LCase(Right(docFileName, 7)) = ".sldasm" Or LCase(Right(docFileName, 4)) = ".asm" Then
              docType = swDocASSEMBLY
          ElseIf LCase(Right(docFileName, 7)) = ".slddrw" Or LCase(Right(docFileName, 4)) = ".drw" Then
              docType = swDocDRAWING
          Else
              docType = swDocNONE
               If bIsSupporterExtension(Mid(docFileName, InStrRev(docFileName, ".") + 1)) = False Then
                   Log "The file extension '" & Mid(docFileName, InStrRev(docFileName, ".") + 1) & "' is not supported."
                   Exit Sub
               End If       
          End If
             
          ' Open document
          If docType = swDocNONE Then
              Set swModel = swApp.LoadFile4(docFileName, "", Nothing, errors)
              docType = swModel.GetType
          Else 
              Set swDocSpecification = swApp.GetOpenDocSpec(docFileName)
              swDocSpecification.DocumentType = docType
              swDocSpecification.ReadOnly = True
              swDocSpecification.Silent = True
              swDocSpecification.ConfigurationName = ""
             swDocSpecification.DisplayState = ""
              Set swModel = swApp.OpenDoc7(swDocSpecification)
              errors = swDocSpecification.Error

             ' Set swModel = swApp.OpenDoc6(docFileName, docType, swOpenDocOptions_Silent Or swOpenDocOptions_ReadOnly, "", errors, warnings)
          End If
         
          If errors = swFutureVersion Then
              Log "Document '" & docFileName & "' is future version."
              Exit Sub
          End If

          ' Load failed?
          If swModel Is Nothing Then
              Log "Method call ModelDoc2::OpenDoc7 for document '" & docFileName & "' failed. Error code " & errors & " returned."
              Exit Sub
          End If
         
          If Val(Left(swApp.RevisionNumber, 2)) >= 18 Then
            swApp.Frame.KeepInVisible = True
          End If

          swApp.ActivateDoc2 docFileName, True, errors
          modelPath = swModel.GetPathName()
          modelFileName = Mid(modelPath, InStrRev(modelPath, "\") + 1)
          modelFileName = Left(modelFileName, InStrRev(modelFileName, ".") - 1)
          modelExtension = Mid(modelPath, InStrRev(modelPath, ".") + 1)

          ' Build destination filenames
          convFileName = "[OutputPath]"
         
          Dim convFileName2 As String
          convFileName2 = "[OutputPath2]"
          Dim convFilePath2 As String
          Dim convFileNameTemp2 As String
         
          Dim bSecondOutput As Boolean
          bSecondOutput = False
          If (Len(convFileName2) > 0) Then
              bSecondOutput = True
          End If
             
          ext = GetExtension(docType, "[FileFormat]")
         
          convFileName = Replace(convFileName, "<Filename>", modelFileName)
          convFileName = Replace(convFileName, "<Extension>", modelExtension)

          convFilePath = Left(convFileName, InStrRev(convFileName, "\"))
          CreatePath convFilePath
          convFileName = convFileName & ext
         
          If bSecondOutput = True Then
              convFileName2 = Replace(convFileName2, "<Filename>", modelFileName)
              convFileName2 = Replace(convFileName2, "<Extension>", modelExtension)

              convFilePath2 = Left(convFileName2, InStrRev(convFileName2, "\"))
              CreatePath convFilePath2
              convFileName2 = convFileName2 & ext
          End If
         
          ' Set conversion options
          SetConversionOptions ext
         
          Set swExtension = swModel.Extension
         
          If docType = swDocDRAWING Then
              Dim vSheetNames As Variant
              Set swDrawing = swModel
             
              ' All sheets?
              If ([OutputSheets] And 2) = 2 Then
                  vSheetNames = swDrawing.GetSheetNames
              ' Last active sheet?
              ElseIf ([OutputSheets] And 4) = 4 Then
                  ReDim vSheetNames(0 to 0) As Variant
                  vSheetNames(0) = swDrawing.GetCurrentSheet.GetName()
              ' Named sheet
              ElseIf ([OutputSheets] And 8) = 8 Then
                  Dim vSheetNamesTemp As Variant
                  vSheetNamesTemp = swDrawing.GetSheetNames
                  removed = 0
                 
                  For i = 0 To UBound(vSheetNamesTemp)
                      vSheetNamesTemp(i-removed) = vSheetNamesTemp(i)
                      sheetName = vSheetNamesTemp(i)
                     
                      If Not sheetName Like "[NamedSheet]" Then
                          removed = removed + 1
                      EndIf
                  Next i
                 
                  If (UBound(vSheetNamesTemp) - removed) >= 0 Then
                      ReDim Preserve vSheetNamesTemp(0 To (UBound(vSheetNamesTemp) - removed))
                      vSheetNames = vSheetNamesTemp
                  End If
              End If

              If Not IsEmpty(vSheetNames) Then
                  ' Save sheets one per file
                  If ([FileSheets] And 4) = 4 Then
                      For i = 0 To UBound(vSheetNames)
                          Dim varSheetName        As Variant
                          swDrawing.ActivateSheet vSheetNames(i)

                          convFileNameTemp = GetFullFileName(convFileName, vSheetNames(i), i, UBound(vSheetNames))

                          If LCase(ext) = ".pdf" Then
                              Set swPDFExport = swApp.GetExportFileData(1)
                              varSheetName = vSheetNames(i)
                              swPDFExport.SetSheets swExportData_ExportSpecifiedSheets, varSheetName
                          ElseIf LCase(ext) = ".edrw" Then
                              swApp.SetUserPreferenceIntegerValue swEdrawingsSaveAsSelectionOption, swEdrawingSaveActive 
                          End If

                          ' Convert the document
                          Success = swExtension.SaveAs(convFileNameTemp, swSaveAsCurrentVersion, swSaveAsOptions_Silent, swPDFExport, errors, warnings)
                         
                          ' Save failed?
                          If Success = False Then
                              If errors = swerr_InvalidFileExtension Then
                                  Log "The file '" & docFileName & "' and sheet '" &  vSheetNames(i) & "' can't be converted to the file extension '" & ext & "'."
                              Else
                                  Log "Method call ModelDocExtension::SaveAs for document '" & convFileNameTemp & "' and sheet '" & vSheetNames(i) & "' failed. Error code " & errors & " returned."
                                  If (((errors And swerr_SaveAsNotSupported) <> 0) And ((warnings And swwarn_MissingOLEObjects) <> 0)) Then
                                     Log "This document contains OLE objects. Such objects can't be converted outside of SolidWorks. Please open the document and perform the conversion from SolidWorks."
                                  End If
                              End if 
                          End If
                         
                          If bSecondOutput = True Then
                              convFileNameTemp2 = GetFullFileName(convFileName2, vSheetNames(i), i, UBound(vSheetNames))
                              Success = swExtension.SaveAs(convFileNameTemp2, swSaveAsCurrentVersion, swSaveAsOptions_Silent, swPDFExport, errors, warnings)
                              ' Save failed?
                              If Success = False Then
                                  If errors = swerr_InvalidFileExtension Then
                                      Log "The file '" & docFileName & "' and sheet '" &  vSheetNames(i) & "' can't be converted to the file extension '" & ext & "'."
                                  Else
                                      Log "Method call ModelDocExtension::SaveAs for document '" & convFileNameTemp2 & "' and sheet '" & vSheetNames(i) & "' failed. Error code " & errors & " returned."
                                      If (((errors And swerr_SaveAsNotSupported) <> 0) And ((warnings And swwarn_MissingOLEObjects) <> 0)) Then
                                          Log "This document contains OLE objects. Such objects can't be converted outside of SolidWorks. Please open the document and perform the conversion from SolidWorks."
                                      End If
                                  End if
                              End If
                          End If
                      Next i
                  ' Save PDF sheets to one file
                  ElseIf ([FileSheets] And 2) = 2 Then
                 
                      If LCase(ext) = ".pdf" Then
                          Set swPDFExport = swApp.GetExportFileData(swExportPdfData)
                          swPDFExport.SetSheets swExportData_ExportSpecifiedSheets, vSheetNames
                      ElseIf LCase(ext) = ".edrw" Then
                          If ([OutputSheets] And 2) = 2 Then ' All sheets?
                              swApp.SetUserPreferenceIntegerValue swEdrawingsSaveAsSelectionOption, swEdrawingSaveAll
                          ElseIf ([OutputSheets] And 4) = 4 Then ' Last active sheet?
                              swApp.SetUserPreferenceIntegerValue swEdrawingsSaveAsSelectionOption, swEdrawingSaveActive 
                          ElseIf ([OutputSheets] And 8) = 8 Then ' Named sheet
                              swApp.SetUserPreferenceIntegerValue swEdrawingsSaveAsSelectionOption, swEdrawingSaveSelected
                              selectedSheets = Join(vSheetNames, vbLf)
                              swApp.SetUserPreferenceStringListValue swEmodelSelectionList, Trim(selectedSheets)
                          End If
                     End If
                     
                      convFileNameTemp = GetFullFileName(convFileName, "All", 0, 0)
                     
                      ' Convert the document
                      Success = swExtension.SaveAs(convFileNameTemp, swSaveAsCurrentVersion, swSaveAsOptions_Silent, swPDFExport, errors, warnings)
                     
                      ' Save failed?
                      If Success = False Then
                          Log "Method call ModelDocExtension::SaveAs for document '" & convFileNameTemp & "' failed. Error code " & errors & " returned."
                      End If
                     
                      If bSecondOutput = True Then
                          convFileNameTemp2 = GetFullFileName(convFileName2, "All", 0, 0)
                          Success = swExtension.SaveAs(convFileNameTemp2, swSaveAsCurrentVersion, swSaveAsOptions_Silent, swPDFExport, errors, warnings)
                          ' Save failed?
                          If Success = False Then
                              Log "Method call ModelDocExtension::SaveAs for document '" & convFileNameTemp2 & "' failed. Error code " & errors & " returned."
                          End If
                      End If
                  End If
              Else
                  Log "Document '" & docFileName & "' didn't contain any sheets named '[NamedSheet]'."
              End If
          Else
              Dim vConfNames As Variant
              Set swConfMgr = swModel.ConfigurationManager
             
              ' All configurations?
              If ([OutputConfs] And 2) = 2 Then
                  vConfNames = swModel.GetConfigurationNames
              ' Last active conf?
              ElseIf ([OutputConfs] And 4) = 4 Then
                  ReDim vConfNames(0 to 0) As Variant
                  vConfNames(0) = swConfMgr.ActiveConfiguration.Name
              ' Named confs
              ElseIf ([OutputConfs] And 8) = 8 Then
                  Dim vConfNamesTemp As Variant
                  vConfNamesTemp = swModel.GetConfigurationNames
                  removed = 0
                 
                  For i = 0 To UBound(vConfNamesTemp)
                      vConfNamesTemp(i-removed) = vConfNamesTemp(i)
                      confName = vConfNamesTemp(i)
                     
                      If Not confName Like "[NamedConf]" Then
                          removed = removed + 1
                      EndIf
                  Next i
                 
                  If (UBound(vConfNamesTemp) - removed) >= 0 Then
                      ReDim Preserve vConfNamesTemp(0 To (UBound(vConfNamesTemp) - removed))
                      vConfNames = vConfNamesTemp
                  End If
              End If
             
              If Not IsEmpty(vConfNames) Then
                  If ([FileConfs] And 4) = 4 Then
                      ' Save configurations
                      For i = 0 To UBound(vConfNames)
                          swModel.ShowConfiguration vConfNames(i)

                          convFileNameTemp = GetFullFileName(convFileName, vConfNames(i), i, UBound(vConfNames))

                          ' Convert the document
                          Success = swExtension.SaveAs(convFileNameTemp, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, errors, warnings)
                     
                          ' Save failed?
                          If Success = False Then
                              If errors = swerr_InvalidFileExtension Then
                                  Log "The file '" & docFileName & "' and configuration '" & vConfNames(i) & "' can't be converted to the file extension '" & ext & "'."
                              Else
                                  Log "Method call ModelDocExtension::SaveAs for document '" & convFileNameTemp & "' and configuration '" & vConfNames(i) & "' failed. Error code " & errors & " returned."
                                  If (((errors And swerr_SaveAsNotSupported) <> 0) And ((warnings And swwarn_MissingOLEObjects) <> 0)) Then
                                      Log "This document contains OLE objects. Such objects can't be converted outside of SolidWorks. Please open the document and perform the conversion from SolidWorks."
                                  End If
                              End If
                          End If
                         
                          If bSecondOutput = True Then
                              convFileNameTemp2 = GetFullFileName(convFileName2, vConfNames(i), i, UBound(vConfNames))
                              Success = swExtension.SaveAs(convFileNameTemp2, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, errors, warnings)
                              ' Save failed?
                              If Success = False Then
                                  If errors = swerr_InvalidFileExtension Then
                                      Log "The file '" & docFileName & "' and configuration '" & vConfNames(i) & "' can't be converted to the file extension '" & ext & "'."
                                  Else
                                      Log "Method call ModelDocExtension::SaveAs for document '" & convFileNameTemp2 & "' and configuration '" & vConfNames(i) & "' failed. Error code " & errors & " returned."
                                      Log "warnings1 = " & warnings
                                      If (((errors And swerr_SaveAsNotSupported) <> 0) And ((warnings And swwarn_MissingOLEObjects) <> 0)) Then
                                          Log "This document contains OLE objects. Such objects can't be converted outside of SolidWorks. Please open the document and perform the conversion from SolidWorks."
                                      End If
                                  End If
                              End If
                          End If
                      Next i
                  ElseIf ([FileConfs] And 2) = 2 Then
                      If LCase(ext) = ".eprt" Or LCase(ext) = ".easm" Then
                          If ([OutputConfs] And 2) = 2 Then ' All confs?
                              swApp.SetUserPreferenceIntegerValue swEdrawingsSaveAsSelectionOption, swEdrawingSaveAll
                          ElseIf ([OutputConfs] And 4) = 4 Then ' Last active conf?
                              swApp.SetUserPreferenceIntegerValue swEdrawingsSaveAsSelectionOption, swEdrawingSaveActive 
                          ElseIf ([OutputConfs] And 8) = 8 Then ' Named confs
                              swApp.SetUserPreferenceIntegerValue swEdrawingsSaveAsSelectionOption, swEdrawingSaveSelected
                              selectedConfs = Join(vConfNames, vbLf)
                              swApp.SetUserPreferenceStringListValue swEmodelSelectionList, Trim(selectedConfs)
                          End If
                      End If
                     
                      convFileNameTemp = GetFullFileName(convFileName, "All", 0, 0)
                     
                      ' Convert the document
                      Success = swExtension.SaveAs(convFileNameTemp, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, errors, warnings)
                     
                      ' Save failed?
                      If Success = False Then
                          Log "Method call ModelDocExtension::SaveAs for document '" & convFileNameTemp & "' failed. Error code " & errors & " returned."
                      End If
                     
                      If bSecondOutput = True Then
                          convFileNameTemp2 = GetFullFileName(convFileName2, "All", 0, 0)
                          Success = swExtension.SaveAs(convFileNameTemp2, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, errors, warnings)
                          ' Save failed?
                          If Success = False Then
                              Log "Method call ModelDocExtension::SaveAs for document '" & convFileNameTemp2 & "' failed. Error code " & errors & " returned."
                          End If
                      End If
                  End If
              Else
                  Log "Document '" & docFileName & "' didn't contain any configurations named '[NamedConf]'."
             End If
          End If
         
          KillHolder convFilePath
          KillHolder convFilePath2
         
          ' Process virtual components
          If docType = swDocASSEMBLY Then
              Dim vComponents As Variant
              Set swAssembly = swModel
             
              vComponents = swAssembly.GetComponents(True)
             
              If Not IsEmpty(vComponents) Then
                For i = 0 To UBound(vComponents)
                    Dim swComponent As SldWorks.Component2
                    Set swComponent = vComponents(i)
                   
                    If swComponent.IsVirtual Then
                        Convert swComponent.GetPathName()
                    End If
                Next i
            End If
          End If

          ' Close document
          swApp.QuitDoc swModel.GetTitle
      End Sub

      Function bIsSupporterExtension(oExtension) As Boolean
         
          If docFileName = "prt" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "asm" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "drw" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "dxf" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "dwg" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "psd" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "ai" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "lfp" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "sldlfp" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "prtdot" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "asmdot" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "drwdot" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "x_t" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "x_b" Then
            bIsSupporterExtension = True
          ElseIf docFileName = "xmt_txt" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "xmt_bin" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "igs" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "iges" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "step" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "stp" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "sat" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "vda" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "wrl" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "stl" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "cgr" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "wrl" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "xpr" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "xas" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "ipt" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "iam" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "par" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "psm" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "ckd" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "emn" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "brd" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "bdf" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "idb" Then
             bIsSupporterExtension = True
          ElseIf docFileName = "3dm" Then
             bIsSupporterExtension = True
          Else
              bIsSupporterExtension = False
          End If
            
      End Function

      Sub Applystamp()

          Dim swSktManager As SldWorks.SketchManager
          Dim swMathUtility As SldWorks.MathUtility
          Dim swMathPoint As SldWorks.MathPoint
          Dim PointCoords(2) As Double
          Dim swSktBlkDef As SldWorks.SketchBlockDefinition
          Dim currentsheet As Sheet
          Dim Width As Double
          Dim Height As Double
          Dim sheetsize As Integer
          Dim value As Integer
          Dim vSheetProperties As Variant
          Dim scale1 As Double
          Dim scale2 As Double
          Dim PI As Double
         
          Set swDrawing = swApp.ActiveDoc
          Set swSketchManager = swDrawing.SketchManager
          Set swMathUtility = swApp.GetMathUtility

          ' Set PI
          PI = 4 * Atn(1)

          ' Get sheet size
          Set currentsheet = swDrawing.GetCurrentSheet
          value = currentsheet.GetSize(Width, Height)

          ' Get sheet scale
          vSheetProperties = currentsheet.GetProperties
          scale1 = vSheetProperties(2)
          scale2 = vSheetProperties(3)

          If Width > Height Then
              PointCoords(0) = (Width - 0.2) * scale2 / scale1
              PointCoords(1) = (0.25 * Height) * scale2 / scale1
              Set swMathPoint = swMathUtility.CreatePoint(PointCoords)
              Set swSktBlkDef = swSketchManager.MakeSketchBlockFromFile(swMathPoint, "C:\EPDM\Consultec Library\Blocks\Issue Tags\Block-Issued.Uncontrolled.Document.SLDBLK", False, Width / 30, PI / 6)
          Else
              PointCoords(0) = (0.25 * Width) * scale2 / scale1
              PointCoords(1) = (0.2) * scale2 / scale1
              Set swMathPoint = swMathUtility.CreatePoint(PointCoords)
              Set swSktBlkDef = swSketchManager.MakeSketchBlockFromFile(swMathPoint, "C:\EPDM\Consultec Library\Blocks\Issue Tags\Block-Issued.Uncontrolled.Document.SLDBLK", False, Height / 30, 5 * PI / 3)
          End If

      End Sub


      Sub main()
         
          On Error GoTo Fail:

          docFileName = "<Filepath>"
         
          ' Get SW interface object
          Set swApp = Application.SldWorks

          Applystamp

          Convert docFileName
         
          Exit Sub
            
      Fail:
          Log "Error while converting file '" & docFileName & "': " & vbCrLf & _
              "An unexpected error occurred while executing the generated script. Script syntax error?" & vbCrLf & _
              "Error number: " & Err.Number & vbCrLf & _
              "Error description: '" & Err.Description & "'" & vbCrLf
             
      End Sub

      I was able to determine (by commenting out and then uncommenting code in the Applystamp sub), that the issue comes from this line:

      Set currentsheet = swDrawing.GetCurrentSheet

      which is working fine when the macro is running on its own.

       

      What I am wondering is:

       

      • Are there limitations to what can be added to the task code?
      • How are we supposed to batch run macros on files using EPDM tasks? Is it as simple as a code copy paste?

       

       

      Any suggestion to solve that greatly appreciated.