break links not working

amineact

New Member
Joined
Apr 12, 2022
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hello there. I am in a bit of a pickle. I'm trying to write a vba code that allows the user to loop through multpile files in a directory and break all their links.
When doing it by hand breaking links seems to work but with vba some files return errors.
How is that possible
Here's the code :
VBA Code:
Sub kill_links()
Dim vLinks As Variant
Dim MyPath As String
Dim Myfile As String
Dim wb As Workbook
Dim ncp As Workbook
Dim ws As Worksheet
Dim rg1 As String
Application.ScreenUpdating = False
Set wb = Workbooks("Scén.xlsx")
rg1 = wb.Sheets("Scén").Range("C6").Value
MyPath = "C:\home\myfiles\"
Myfile = Dir(MyPath & "\*.xl*")
Do While Myfile <> ""
   Set ncp = Workbooks.Open(Filename:=MyPath & "\" & Myfile)
     Application.DisplayAlerts = False
     Application.AskToUpdateLinks = False
     ncp.activate
     vLinks = ncp.LinkSources(xlLinkTypeExcelLinks)
     If Not IsEmpty(vLinks) Then
        for i =1 TO Ubound((vLinks)
        wb.BreakLink Name:=vLinks(i), Type:=xlLinkTypeExcelLinks
        Next 
     End If

     ncp.Close SaveChanges:=True
    End If
  
    
 Myfile = Dir
 Loop
     
Application.ScreenUpdating = True

End Sub
Thank you
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I made some slight changes to your code (I didn't test it, so hopefully it works).
SQL:
Sub kill_links()
Dim vLinks As Variant
Dim MyPath As String
Dim Myfile As String
Dim wb As Workbook
Dim ncp As Workbook
Dim ws As Worksheet
Dim rg1 As String
Application.ScreenUpdating = False
Set wb = Workbooks("Scén.xlsx")
rg1 = wb.Sheets("Scén").Range("C6").Value
MyPath = "C:\home\myfiles\"
Myfile = Dir(MyPath & "\*.xl*")
Do While Myfile <> ""
   Set ncp = Workbooks.Open(Filename:=MyPath & "\" & Myfile)
     Application.DisplayAlerts = False
     Application.AskToUpdateLinks = False
     ncp.Activate
     vLinks = ncp.LinkSources(xlLinkTypeExcelLinks)
     If Not IsEmpty(vLinks) Then
        For i = 1 To UBound(vLinks)
        ncp.BreakLink Name:=vLinks(i), Type:=xlLinkTypeExcelLinks
        Next
     End If

     ncp.Close SaveChanges:=True
    End If
  
    
 Myfile = Dir
 Loop
     
Application.ScreenUpdating = True

End Sub

Changes made:
  1. Deleted an extra ")" after ubound(vlinks)
  2. Changed the workbook the breaklink method was pointing towards (from wb to ncp)
 
Upvote 0
I made some slight changes to your code (I didn't test it, so hopefully it works).
SQL:
Sub kill_links()
Dim vLinks As Variant
Dim MyPath As String
Dim Myfile As String
Dim wb As Workbook
Dim ncp As Workbook
Dim ws As Worksheet
Dim rg1 As String
Application.ScreenUpdating = False
Set wb = Workbooks("Scén.xlsx")
rg1 = wb.Sheets("Scén").Range("C6").Value
MyPath = "C:\home\myfiles\"
Myfile = Dir(MyPath & "\*.xl*")
Do While Myfile <> ""
   Set ncp = Workbooks.Open(Filename:=MyPath & "\" & Myfile)
     Application.DisplayAlerts = False
     Application.AskToUpdateLinks = False
     ncp.Activate
     vLinks = ncp.LinkSources(xlLinkTypeExcelLinks)
     If Not IsEmpty(vLinks) Then
        For i = 1 To UBound(vLinks)
        ncp.BreakLink Name:=vLinks(i), Type:=xlLinkTypeExcelLinks
        Next
     End If

     ncp.Close SaveChanges:=True
    End If
 
   
 Myfile = Dir
 Loop
    
Application.ScreenUpdating = True

End Sub

Changes made:
  1. Deleted an extra ")" after ubound(vlinks)
  2. Changed the workbook the breaklink method was pointing towards (from wb to ncp)
Thanks for the reply, im afraid i made some errors in my original code
However, the problem still persists. For some workbooks it does a perfect job breaking the links however for the other i get the error Method 'Break-Link' of object '_Workbook' failed".
I appreciate the help
 
Upvote 0
Hmmm, I'm unsure. It seems like this should work for all workbooks. Perhaps the sheets/cells that you're trying to break the links on is protected. You could add the below so it skips any workbooks that throw an error:

VBA Code:
On Error Resume Next
 
Upvote 0

Forum statistics

Threads
1,217,433
Messages
6,136,598
Members
450,021
Latest member
Jlopez0320

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