Folder files save as with _ in existing file name

rins

New Member
Joined
May 17, 2021
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi,

My VBScript below works perfectly fine but just silly thing I cant figure it out -- can anyone please help me to improve results?

Script meant to --
1) get all files from the specific folder -- works
2) Delete external connections on all of them -- works (thank to this site)
3) New file Save As with exisitng fName -- works

Only thing is this script randomly saves new file in different drives so I want to specify a folder name for it to save but where do I specify a folder name if I want to save as files to a different location? I suppose this is silly question but I tired many things however without sucess.

The Script:
MyDateFormat = Right("0" & Day(now), 2)& "-" &Right("0" & Month(Now), 2)& "-"&_
Year(now)

dir = "R:\Reports\1"

Set oExcel = CreateObject("Excel.Application")
Set fso = CreateObject("Scripting.FileSystemObject")

oExcel.Application.Visible = True
oExcel.DisplayAlerts = False
oExcel.AskToUpdateLinks = False
oExcel.AlertBeforeOverwriting = False

For Each file In fso.GetFolder(dir).Files
If LCase(fso.GetExtensionName(file)) = "xlsx" Then
fName = fso.GetBaseName(file)

Set oWorkbook = oExcel.Workbooks.Open(file)

Do While oExcel.ActiveWorkbook.Connections.Count > 0
oExcel.ActiveWorkbook.Connections.Item(oExcel.ActiveWorkbook.Connections.Count).Delete
Loop

oExcel.Activeworkbook.SaveAs fName & "_" &_
MyDateFormat &".xlsx"

oExcel.Activeworkbook.Close
oExcel.Quit

End if
Next

Set fso = Nothing
Set oWorkbook = Nothing
Set oExcel = Nothing
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Forum statistics

Threads
1,214,534
Messages
6,120,080
Members
448,943
Latest member
sharmarick

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