Consulting

Results 1 to 3 of 3

Thread: MACRO TO SAVE ACTIVE DOCUMENT TO THUMB DRIVE

  1. #1

    MACRO TO SAVE ACTIVE DOCUMENT TO THUMB DRIVE

    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

  2. #2
    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.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Thank you. I was able to modify the code using FileCopy to accomplish the same goal.

    I appreciate the advice.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •