Saving .xltm as .xlsx fails using VBA

Kayslover

Board Regular
Joined
Sep 22, 2020
Messages
168
Office Version
  1. 2013
Platform
  1. Windows
Hi All,

I have a template which is Macro Enabled ending with .xltm

Through VBA (in a macro), I am attempting to save the template as an ordinary file with a new name. The file is currently defined as the Active Workbook and I am using the following command:-

VBA Code:
ActiveWorkbook.SaveAs fPath & ".xlsx", 51.

I have also tried using the following command:-

VBA Code:
ActiveWorkbook.SaveAs Filename:=fPath & ActiveWorkbook.Name&".xlsx", FileFormat:=xlOpenXMLWorkbook

I get a run-time error 1004.

Please note
VBA Code:
fPath is defined as Dim fPath As String, fPath = ThisWorkbook.Path & "\"

Where am I going wrong? I am a newbie to VBA so please be gentle.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
In that case try
VBA Code:
   Dim fPath As String
   fPath = ThisWorkbook.Path & "\"
   With ActiveWorkbook
      Application.DisplayAlerts = False
      .SaveAs fPath & Split(.Name, ".")(0) & ".xlsx", 51
      Application.DisplayAlerts = True
   End With
 
Upvote 0
Solution
At beginning try to create new workbook as macro enabled file (.xlsm) instead to do that in macro enabled template(.xltm).
When you trying to get file path from macro enabled template with fPath = ThisWorkbook.Path you get empty file path. You need to specify path.
When you using macro enabled file(.xlsm) you can get this path.
 
Upvote 0
Fluff,

Worked like a dream, Thank you very very much.

One further question, how can delete all file from the folded which have an extension of ".xltx" in one statement. I have to delete 4 of these and I am doing them one by one.
 
Upvote 0
As that is a totally different question, it needs a new thread. Thanks
 
Upvote 0

Forum statistics

Threads
1,214,848
Messages
6,121,917
Members
449,055
Latest member
KB13

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