Saving .xltm as .xlsx fails using VBA

Kayslover

Board Regular
Joined
Sep 22, 2020
Messages
136
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.
 

Kayslover

Board Regular
Joined
Sep 22, 2020
Messages
136
Office Version
  1. 2013
Platform
  1. Windows
Fluff,

Yes. Trying to save xlsm files to xlsx.
 

Some videos you may like

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,301
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

EXCEL MAX

Active Member
Joined
Nov 11, 2020
Messages
380
Office Version
  1. 2007
Platform
  1. Windows
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.
 

Kayslover

Board Regular
Joined
Sep 22, 2020
Messages
136
Office Version
  1. 2013
Platform
  1. Windows
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,301
Office Version
  1. 365
Platform
  1. Windows
As that is a totally different question, it needs a new thread. Thanks
 

Watch MrExcel Video

Forum statistics

Threads
1,127,103
Messages
5,622,742
Members
415,925
Latest member
Ryle23

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
Top