dejhantulip
Board Regular
- Joined
- Sep 9, 2015
- Messages
- 58
- Office Version
- 365
- Platform
- 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.
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
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