Save Workbook Timestamped copy is saving my Addin file location instead of actual workbook?

dejhantulip

Board Regular
Joined
Sep 9, 2015
Messages
58
Office Version
  1. 365
Platform
  1. Windows
Dear all,

First of all, happy new year!! I wish you all the best for this 2021 :)

I wanted to share a little code that I am using to save a "backup" copy of my workbook.

VBA Code:
Sub BackupWithTimeStamp()
    'Code from Officetricks
    'Define variable fields
    Dim sFolderPath As String
    Dim sFileName As String
    Dim sFileExt As String
    Dim sNewFileName As String
    
    'Set Time Stamp
    Dim sDateTimeStamp As String
    'sDateTimeStamp = VBA.Format(VBA.Now, "_dd_mmm_yy_HH_MM_SS_AM/PM")
    sDateTimeStamp = VBA.Format(VBA.Now, "_yyyymmdd_HHMMSS")
    
    'Get File Folder Path
    sFolderPath = ThisWorkbook.Path & "\"
    
    'Get Document File name
    sFileName = ThisWorkbook.Name
    
    'Split File Name & Extention
    sFileExt = VBA.Mid(sFileName, VBA.InStrRev(sFileName, ".", , vbTextCompare))
    sNewFileName = VBA.Replace(sFileName, sFileExt, "", , , vbTextCompare)
    
    'Add Date-Time Stamp to New File Name
    sNewFileName = sFolderPath & sNewFileName & sDateTimeStamp & "_backup" & sFileExt
        
    'Save the File Now
    ThisWorkbook.SaveCopyAs sNewFileName
End Sub


The thing is, I am trying to create an addin for myself so that code is being placed in a Module in my addin file (xlam). The problem is that when I invoke this code using the addin button (which triggers the macro that is inside the addin) it is the addin file that gets backed up and not the "workbook" I am currently working on... hopefully I explained that correctly.

So basically, if I am working on a workbook called MyBook.xlsm and I click on the "backup" button that runs the macro of my addin (which has the name IASA.xlam) what I am getting is the IASA.xlam file backed up in its location, and not the MyBook.xlsm file.

If someone could help me out with this, I would be very grateful!
Thank you very much!!!


ORLANDO
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You need to change every instance of ThisWorkbook to ActiveWorkbook.
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,548
Messages
6,120,141
Members
448,948
Latest member
spamiki

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top