PDA

View Full Version : [SOLVED:] MACRO TO SAVE ACTIVE DOCUMENT TO THUMB DRIVE



lorrdwolf
08-28-2020, 09:10 PM
I do a lot of writing on my laptop and want a quick way to save a copy of my current project to a thumb drive as backup. I wrote the code below to figure out the active file's folder then use that as part of the save path. That all works fine but when I get to the ActiveDocument.SaveAs2 the program always brings up the SaveAs dialogue and the location is on the laptop, not the thumb drive.

My code is below. Please help :( This 'simple' project has gotten me very frustrated.


Sub Backup()

' Macro to backup writing projects.

' Declare variables
Dim strSavePath, strFileName, strFolder As String
Dim intPathLength, intFolderLength As Integer

' Assign variables
strFileName = ActiveDocument.Name
intPathLength = Len(ActiveDocument.Path)
intFolderLength = intPathLength - 39
strFolder = Mid(ActiveDocument.Path, 40, intFolderLength)

' Construct save path
strSavePath = "D:\" & strFolder & "\" & strFileName

' Save backup file to thumb drive
Application.DisplayAlerts = wdAlertsNone
ActiveDocument.SaveAs2 FileName:=strSavePath, FileFormat:=wdFormatDocumentDefault
Application.DisplayAlerts = wdAlertsAll

End Sub

gmayor
08-29-2020, 02:03 AM
It is never a good idea to save Word documents to such drives. For years it has been a regular source of data loss. It is always better to save to the local drive and copy to the thumb drive. Similarly copy the document to the hard drive before opening it. https://www.gmayor.com/SaveInTwoPlacesAddIn.htm will enable you to save the copy transparently.

lorrdwolf
08-29-2020, 05:01 AM
Thank you. I was able to modify the code using FileCopy to accomplish the same goal.

I appreciate the advice.