Run Custom Task

You can schedule tasks to run any application, using macros or command-line arguments that you specify.

See SOLIDWORKS API and Add-Ins Help Topics for information about SOLIDWORKS macro files.
Anything that you can run from Windows Start > Run can run as a custom task in SOLIDWORKS Task Scheduler. See the Help files in the application you want to run for information about program command-line arguments and macros in that application.

Creating a Task in Run Custom Task

To create a task in Run Custom Task:

  1. Click Run Custom Task on the sidebar, or click Tasks > Run Custom Task.
  2. In the dialog box, type a new title for the task or leave the default in Task title.
  3. In Program path, type the path to the program you want to run, or click Browse to locate it.
  4. If the program requires any program arguments, type them in Program arguments.
    SOLIDWORKS Program argument Program argument definition Set by default
    /b Run the SOLIDWORKS software in the background yes
    /m Run the SOLIDWORKS macro yes
  5. To run a macro or SOLIDWORKS macro template, type the path in Macro file path, or click Browse to locate it.
    If you specify a SOLIDWORKS macro template, the token names and their types appear in the Parameter Name and Type columns under Task parameters.
  6. In each row that shows a Parameter Name and Type, click in the Parameter String column and type the parameter value.
If you specify a SOLIDWORKS macro template, create a backup copy of the macro template file before scheduling and running the task, or store the macro template file in a location other than the task working directory. Click View > Options > Task Options to see the task working directory.

Creating a SOLIDWORKS Macro Template

A SOLIDWORKS macro template is a SOLIDWORKS macro that lets you specify parameter values when you schedule it as a custom task in SOLIDWORKS Task Scheduler. A SOLIDWORKS macro template is a text file that typically contains Visual Basic code, SOLIDWORKS API calls, and token names. When you schedule a SOLIDWORKS macro template to run as a custom task in SOLIDWORKS Task Scheduler, you specify the parameter values to substitute for the token names.

Token names can be either text strings or numerics. They are formatted as follows:

Type of Parameter Format of Token Name
String $$$<token_name>$$$
Number ###<token_name>###

A SOLIDWORKS macro template must have a .swb filename extension.

To create a macro template:

  1. Open a new text file using Windows Notepad, Microsoft WordPad, or other text editor.
  2. Copy and paste a SOLIDWORKS macro into the text file.
    See SOLIDWORKS API and Add-Ins Help Topics for information about SOLIDWORKS macro files.
  3. Type token names in place of the parameters whose values you want to specify. For example:
    Original code: swApp.SetCurrentWorkingDirectory "c:\temp"
    Modified code: swApp.SetCurrentWorkingDirectory $$$TASK_WORKING_DIR$$$
  4. Save the text file with a filename extension .swb.
    Create a backup copy of the SOLIDWORKS macro template file before scheduling and running it, or store the SOLIDWORKS macro template file in a location other than the task working directory. Click View > Options > Task Options to see the task working directory. When a task using a SOLIDWORKS macro template runs, the system creates a copy of the macro template and replaces the token names with the parameter values you specified when you scheduled the task. The system saves the file in the task working directory under the same filename as the original SOLIDWORKS macro template. If the original SOLIDWORKS macro template is stored in the task working directory, the system overwrites it with the new file.
    The following code is an example of a SOLIDWORKS macro template. It opens each SOLIDWORKS drawing document in a folder and saves it as a DXF file. When you schedule the macro template as a custom task in SOLIDWORKS Task Scheduler, you specify the parameters to substitute for the token names: $$$TASK_SOURCE_DIR$$$ and $$$TASK_DESTINATION_DIR$$$.
    Dim swApp As Object
    Dim swModel As Object
    Dim ret As Boolean
    Dim openError As Long
    Dim openWarn As Long
    Dim errorFilePath As String
    Dim nErrors As Long
    Dim nWarnings As Long
    Dim destinationPath As String
    Dim sourcePath As String
    Dim modelTitle As String
    
    Sub main()
    
        sourcePath = $$$TASK_SOURCE_DIR$$$
        destinationPath = $$$TASK_DESTINATION_DIR$$$
         
        errorFilePath = destinationPath + "\" + "swTaskScheduler.error"
        ErrorOut "Start logging any errors...", errorFilePath
        
        Set swApp = CreateObject("SldWorks.Application")
        swApp.SetCurrentWorkingDirectory destinationPath
        
        ' Determine type of document
        
        openWarn = 0
        
        Dim fileName As String
        fileName = Dir(sourcePath + "\*.SLDDRW")
        
        Do While fileName <> ""
            modelTitle = ""
            Set swModel = swApp.OpenDoc6(sourcePath + "\" + fileName, 3, 1, "", openError, openWarn)
            
            
            If (openWarn And 2) Then
                ErrorOut "File is read-only.", errorFilePath
                ret = False
            ElseIf (openWarn And 4) Then
                ErrorOut "File is locked by another user.", errorFilePath
                ret = False
            ElseIf (openWarn And 512) Then
                ErrorOut "File is view-only.", errorFilePath
                ret = False
            Else
                ret = True
            End If
            
            If Not swModel Is Nothing Then
                modelTitle = swModel.GetTitle()
                If ret = True Then
                        ' Strip off SOLIDWORKS drawing file extension (.slddrw)
                        ' and add DXF file extension (.dxf)
                        Dim sPathName As String
                        sPathName = swModel.GetPathName
                        sPathName = Left(sPathName, Len(sPathName) - 6)
                        sPathName = sPathName + "dxf"
                        sPathName = Replace(sPathName, sourcePath, destinationPath)
                        ' Save as file as DXF
                        bRet = swModel.SaveAs4(sPathName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings)
                End If
                
                Set swModel = Nothing
                swApp.CloseDoc modelTitle
            End If
    
            
            fileName = Dir
        Loop
              
        ' Exit the SOLIDWORKS software
        swApp.ExitApp
        Set swApp = Nothing
        
    End Sub
    
    Function ErrorOut(errorString As String, errorFilePath As String)
        Open errorFilePath For Append As #5
        Print #5, errorString
        Close #5
    End Function
    

Scheduling the Task

To schedule the task:

  1. Under Task Schedule, set:
    Option Description
    Running mode Specifies how often the task runs. Select Once, Daily, Weekly, or Monthly.
    Start time  
    Start date  
  2. Click Finish.
    The task and its title, scheduled time, scheduled date, and status appear in the Tasks panel. The status of the task is Scheduled.
    To run a scheduled task, your computer must be on at the scheduled start time. The SOLIDWORKS Task Scheduler window does not need to be open. If your computer is off at the scheduled start time, the task runs when you turn your computer on.

    At the scheduled start time, the task is initiated. If the task uses a SOLIDWORKS macro template, the system creates a copy of the macro template and replaces the token names with the specified parameter values, then saves the file in the task working directory under the same filename as the original SOLIDWORKS macro template. The program and macro commands are executed.

    When the task completes, the status is In Progress. When the scheduled program exits, the status changes to Completed.