![]() |
|
#14
|
|||
|
|||
|
Hello everybody, after reading this forum and searching for myself I've decided to create a VBA macro for opening and restoring Word session that wouldn't close and save them in the process, because I couldn't find it anywhere else on the internet. Otherwise you can use an excellent gmayor add-in.
Here is a brief description of what the macro does:
Code:
Sub SessionSave()
' Created 04.02.2017
' Updated 05.02.2017
'
' Macro for word opened files session saving
'
' Microsoft scripting runtime (scrrun.dll) must be enabled in references
'
' This procedure saves current word session (opened windows) in a text file
' for later loading of the session.
' Declare variables
Dim Saved As Integer
Dim Unsaved As Integer
Dim fso As New FileSystemObject
'the file we're going to write to
Dim ts As TextStream
Dim FileName As String
Dim UnsavedName As String
'open this file to write to it, where file name is composed of the current date and time
'file with the same name will be overwritten
FileName = Year(Now) & Format(Now, "mm") & Format(Now, "dd") & "_" & _
Format(Now, "hh") & Format(Now, "nn") & Format(Now, "ss") & ".txt"
Set ts = fso.CreateTextFile("C:\vba\" & FileName, True)
'These two variable should say how many opened windows were saved and which remained unsaved
'unsaved are those that are not saved on the disk yet
Saved = 0
Unsaved = 0
'This cycle will loop through opened files and saves them into the file
For Each adoc In Documents
Debug.Print adoc.Path&; "\" & adoc.Name
If adoc.Path <> "" Then
ts.WriteLine (adoc.Path & "\" & adoc.Name)
Saved = Saved + 1
ElseIf adoc.Path = "" Then
Unsaved = Unsaved + 1
UnsavedName = UnsavedName + adoc.Name + ","
Else
Debug.Print ("Error saving file path")
End If
Debug.Print adoc.Path&; "\" & adoc.Name
Next adoc
'closes the file
ts.Close
'informs user on the result of the procedure
MsgBox "Saving of the session" & FileName & " was successful. There are " & _
Saved & " files recorded. Unrecorded files number is " & Unsaved & "(" & UnsavedName & ")." _
, vbInformation, "Session save result"
End Sub
Sub OpenSession()
' Created 04.02.2017
' Updated 05.02.2017
'
' Open saved session of word files Macro
'
' Microsoft scripting runtime (scrrun.dll) must be enabled in references
'
' This procedure opens saved sessions (opened windows) from text files
' created by the SessionSave procedure.
'
Dim fso As New FileSystemObject
'the file we're going to read from
Dim ts As TextStream
Dim FilePath As String
Dim ThisLine As String
Dim i As Integer
Dim FilesNotFound As String
'only allow the user to select one file
'make the file dialog visible to the user
With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = False
.Show
If (.SelectedItems.Count = 0) Then
Exit Sub
'// dialog dismissed with no selection
Else
FilePath = .SelectedItems(1)
End If
End With
'... we can open a text file
Set ts = fso.OpenTextFile(FilePath, ForReading)
'keep reading in lines till no more
FilesNotFound = ""
i = 0
Do Until ts.AtEndOfStream
ThisLine = ts.ReadLine
i = i + 1
'Debug.Print "Line " & i, ThisLine
If (Dir(ThisLine) > "") Then 'check if the file exists
Documents.Open FileName:=ThisLine
Else 'if it doesn't exist then save the filename
FilesNotFound = FilesNotFound + ThisLine + ", "
End If
Loop
'close down the file
ts.Close
'Tell the user if the opening of the session was successful; if not then tell, which files couldn't be opened
If FilesNotFound = "" Then
MsgBox "Session " & FilePath & ": All the files were opened.", vbInformation
Else
MsgBox "Session " & FilePath & ": Those files could not be opened (path not found): " _
& FilesNotFound, vbExclamation
End If
End Sub
5kldqw Last edited by 5kldqw; 02-05-2017 at 11:03 AM. |
| Tags |
| restore, session |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
How to Restore Heading 1, Heading 2, etc. within a Word Document
|
cheech1981 | Word | 9 | 01-11-2017 02:14 AM |
| Word ask to save template whenever i save a derived document | jorbjo | Word | 3 | 10-04-2012 10:52 AM |
PPT save issue - can't restore previous copy?
|
GrahamBerry | PowerPoint | 1 | 04-23-2012 10:33 AM |
How to Restore Page Margin Page Number Styles in Word 2010
|
cheech1981 | Word | 5 | 11-15-2011 04:16 AM |
| Restore .pst from .bak? | markg2 | Outlook | 0 | 04-12-2010 05:58 AM |