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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
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,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

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