cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Save and restore "workspaces" (which documents are open, and where they are on the screen)

Save and restore "workspaces" (which documents are open, and where they are on the screen)

It would be great to have a utility for saving "workspaces", i.e., which documents are open and where they are on the screen. This would be great for picking up where you left off after a crash, restart, or switching projects.

 

This would only save which documents (parts, assemblies, drawings) are open and where they are on the screen; it would not save or control the location of UI elements such as the Model Browser and iLogic panel. I've created a separate request for that here: Save and restore UI Layouts. Keeping these separate allows you to switch UI layouts without accidentally closing and opening documents, or load a workspace (set of files) without accidentally messing up the UI layout.

1 Comment
el_jefe_de_steak
Collaborator

Great suggestion! Count my vote in!

 

I have actually created something that is similar for myself in VBA. It doesn't save where your windows are, but it does save which documents are open in a CSV file and opens them again.

It has 3 functions:

  1. SaveOpenDocuments - saves all documents you have open and documents them in the CSV file
  2. SaveAndCloseAll - same as SaveOpenDocuments but also closes all of the documents after they are saved
  3. OpenSavedDocuments - reads the CSV file and opens all files that were documented. Works best when SaveAndCloseAll has been used.

 

The code is below. Here are some current limitations and important things to know:

  • Running the code saves all files with the "silentoperation" option turned on. This will not prompt you to save anything - it will just go right ahead and save it. This saves lots of time when you have many files open.
  • The code creates a CSV file and saves it as a hidden file in the workspace directory. This works well for us because we work with lots of single-user projects and you can have a different file set for each project.
  • The code will error out if you are performing an edit-in-place operation. It doesn't specify the exact error, but will tell you that it hasn't saved anything.
  • The code will throw an error if any open documents have not yet been saved. This is to prevent accidentally saving new files into undesired locations.

 

Option Explicit
'write all open documents to a .csv file, then save and close all documents
Public Sub SaveAndCloseAll()
   
    'exit code if no documents open in foreground
    If ThisApplication.Documents.VisibleDocuments.Count = 0 Then
        Exit Sub
    End If
   
    Dim fso As New Scripting.FileSystemObject
   
    'declare/initialize local variables
    Dim oAssemblyDoc As AssemblyDocument
    Dim mustSaveFirst As Boolean
    mustSaveFirst = False
    Dim filesToSave As String
    Dim oOpenDoc As Document
    Dim saveFilePath As String
    saveFilePath = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath & "\LastOpenDocuments.csv"
     
    If fso.FileExists(saveFilePath) = True Then
        SetAttr saveFilePath, 0
    End If
   
    'open .csv file to save file names to. This command erases what's already in there
    Open saveFilePath For Output As #1
   
    'close the output file in case of error so that Windows does not hold it open in the background.
    On Error GoTo CloseOutput:
   
    'check to see if files have never been saved
    For Each oOpenDoc In ThisApplication.Documents.VisibleDocuments
        If oOpenDoc.FileSaveCounter = 0 Then
            filesToSave = filesToSave & oOpenDoc.DisplayName & vbCr
            mustSaveFirst = True
        End If
    Next oOpenDoc
   
    'display message and exit if any files have never been saved
    If mustSaveFirst Then
        MsgBox ("The following file(s) must be saved before performing this action:" & vbCr & filesToSave)
        GoTo CloseOutput:
    End If
   
    'sets to save files without prompts
    ThisApplication.SilentOperation = True
   
    'save file path to .csv, and design view representation if assembly
    For Each oOpenDoc In ThisApplication.Documents.VisibleDocuments
       
       
        If oOpenDoc.DocumentType = kAssemblyDocumentObject Then
            'executes if assembly document. The purpose is to save the design view representation as well
            Set oAssemblyDoc = oOpenDoc
            Write #1, oOpenDoc.FullFileName, oAssemblyDoc.ComponentDefinition.RepresentationsManager.ActiveDesignViewRepresentation.Name
        Else
            'executes if not assembly document
            Write #1, oOpenDoc.FullFileName
        End If
       
        'save and close document
        oOpenDoc.Save
        oOpenDoc.Close
                       
    Next oOpenDoc
   
    'close .CSV file
    Close #1
   
    SetAttr saveFilePath, vbHidden
   
    'clean-up any documents open in background
    ThisApplication.Documents.CloseAll
    ThisApplication.SilentOperation = False
   
    Exit Sub

'close the output file and exit the sub
CloseOutput:
    Close #1
    ThisApplication.SilentOperation = False
    MsgBox ("An error occurred and the operation was canceled.")
Exit Sub
           
End Sub

'open all documents listed in .csv file
Public Sub OpenSavedDocuments()

    'declare local variables
    Dim LineFromFile As String
    Dim LineItems() As String
    Dim FileName As String
    Dim DesignViewRep As String
    Dim openFilePath As String
    openFilePath = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath & "\LastOpenDocuments.csv"
   
    On Error GoTo FileNotFound:
   
    Open openFilePath For Input As #1
   
    'close the input file in case of error so that Windows does not hold it open in the background.
    On Error GoTo CloseInput:
   
    'saves all files without prompt
    ThisApplication.SilentOperation = True
   
    'loop through the .csv file and open files contained inside
    Do Until EOF(1)
        'read line and split into array
        Line Input #1, LineFromFile
        LineItems = Split(LineFromFile, """,""")
       
        'open files
        If getArrayLength(LineItems) = 1 Then
            'runs if document is not assembly
           
            FileName = Replace(LineItems(0), """", "")
            ThisApplication.Documents.Open (FileName)
        Else
            'runs if document is assembly
           
            FileName = Replace(LineItems(0), """", "")
            DesignViewRep = Replace(LineItems(1), """", "")
           
            'assume that document is assembly and sets design view representation
            Dim newAssemblyDoc As AssemblyDocument
            Set newAssemblyDoc = ThisApplication.Documents.Open(FileName)
           
            'set design view representation, on error do not set to anything
            On Error Resume Next
            newAssemblyDoc.ComponentDefinition.RepresentationsManager.DesignViewRepresentations.Item(DesignViewRep).Activate
           
            'save document with design view rep activated (so files aren't marked as "dirty" by previous switch of design view rep)
            newAssemblyDoc.Save
        End If
    Loop
   
   
    'close input .csv file
    Close #1
    ThisApplication.SilentOperation = False
   
    Exit Sub
   
'close the input file and exit the sub
CloseInput:
    Close #1
    ThisApplication.SilentOperation = False
    MsgBox ("An unknown error occurred and the operation was canceled.")
Exit Sub

FileNotFound:
    MsgBox "There is no list of recently saved documents to open!" & vbCr & "Are you sure you are in the correct project?"
Exit Sub

End Sub

'write all open documents to a .csv file, then save and close all documents
Public Sub SaveOpenDocuments()
   
    'exit code if no documents open in foreground
    If ThisApplication.Documents.VisibleDocuments.Count = 0 Then
        Exit Sub
    End If
   
    Dim fso As New Scripting.FileSystemObject
   
    'declare/initialize local variables
    Dim oAssemblyDoc As AssemblyDocument
    Dim mustSaveFirst As Boolean
    mustSaveFirst = False
    Dim filesToSave As String
    Dim oOpenDoc As Document
    Dim saveFilePath As String
    saveFilePath = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath & "\LastOpenDocuments.csv"
     
    If fso.FileExists(saveFilePath) = True Then
        SetAttr saveFilePath, 0
    End If
   
    'open .csv file to save file names to. This command erases what's already in there
    Open saveFilePath For Output As #1
   
    'close the output file in case of error so that Windows does not hold it open in the background.
    On Error GoTo CloseOutput:
   
    'check to see if files have never been saved
    For Each oOpenDoc In ThisApplication.Documents.VisibleDocuments
        If oOpenDoc.FileSaveCounter = 0 Then
            filesToSave = filesToSave & oOpenDoc.DisplayName & vbCr
            mustSaveFirst = True
        End If
    Next oOpenDoc
   
    'display message and exit if any files have never been saved
    If mustSaveFirst Then
        MsgBox ("The following file(s) must be saved before performing this action:" & vbCr & filesToSave)
        GoTo CloseOutput:
    End If
   
    'sets to save files without prompts
    ThisApplication.SilentOperation = True
   
    'save file path to .csv, and design view representation if assembly
    For Each oOpenDoc In ThisApplication.Documents.VisibleDocuments
       
       
        If oOpenDoc.DocumentType = kAssemblyDocumentObject Then
            'executes if assembly document. The purpose is to save the design view representation as well
            Set oAssemblyDoc = oOpenDoc
            Write #1, oOpenDoc.FullFileName, oAssemblyDoc.ComponentDefinition.RepresentationsManager.ActiveDesignViewRepresentation.Name
        Else
            'executes if not assembly document
            Write #1, oOpenDoc.FullFileName
        End If
       
        'save and close document
        oOpenDoc.Save
                       
    Next oOpenDoc
   
    'close .CSV file
    Close #1
   
    SetAttr saveFilePath, vbHidden

    ThisApplication.SilentOperation = False
    MsgBox ("All open documents were successfully saved and documented.")
    Exit Sub

'close the output file and exit the sub
CloseOutput:
    Close #1
    ThisApplication.SilentOperation = False
    MsgBox ("An error occurred and the operation was canceled.")
Exit Sub
           
End Sub
Private Function getArrayLength(arr As Variant) As Integer
    getArrayLength = UBound(arr) - LBound(arr) + 1
End Function

Can't find what you're looking for? Ask the community or share your knowledge.

Submit Idea