Open file, delete connection and save as

rins

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

Could anyone please help me figure out what is wrong with below VBScripting and how do I correct it?

It meant to
1) Open excel -- works
2) Refresh all data -- works
3) Remove external connection -- Dont work
4) Save as new file without any external connection -- Dont work due to 3rd step

Set oExcel = CreateObject("Excel.Application")

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

Set oWorkbook = oExcel.Workbooks.Open("file path.xlsx")

oWorkbook.RefreshAll

oExcel.ActiveWorkbook.Connections("connection name").Delete

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

oExcel.Activeworkbook.Close
oExcel.Quit
WScript.Quit
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Maybe something like this:
VBA Code:
 Do While ActiveWorkbook.Connections.Count > 0
     ActiveWorkbook.Connections.Item(ActiveWorkbook.Connections.Count).Delete
 Loop
 
Upvote 0
Solution
Maybe something like this:
VBA Code:
 Do While ActiveWorkbook.Connections.Count > 0
     ActiveWorkbook.Connections.Item(ActiveWorkbook.Connections.Count).Delete
 Loop
Hi, that worked just fine. Thank you very much for your help.

Out of curiosity and just for my knowledge, rather than deleting workbook connections- is it possible to remove external table link from the file?
so when I was recording macro below came up as to remove external table data from the file - would there be any vbs equivelant coding for below?

ActiveSheet.ListObjects( _
"My SQL data table").Unlink
 
Upvote 0
Hi, that worked just fine. Thank you very much for your help.

Out of curiosity and just for my knowledge, rather than deleting workbook connections- is it possible to remove external table link from the file?
so when I was recording macro below came up as to remove external table data from the file - would there be any vbs equivelant coding for below?

ActiveSheet.ListObjects( _
"My SQL data table").Unlink
I'm no expert on vbs ... sorry :confused:
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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