I have a template that I update weekly with new data. I've written code to create a "report" for broad distribution each week from this template. The report creation works great except for the fact that the links to the template and another workbook that i use as an image databse won't break. I've run into problems with the links to the workbook that has the images because I am using named ranges and lookup formulas in the named ranges to populate the template. Any thoughts?
Sub NewReport()
Dim Wb1 As Workbook
Dim wb2 As Workbook
Dim RN As String
RN = Range("d1").Value
Dim WMWK As String
WMWK = Sheets("fineline name").Range("j4").Value
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
End With
Set Wb1 = ActiveWorkbook
Set wb2 = Application.Workbooks.Add(1)
Wb1.Sheets(Array(Wb1.Sheets(1).Name, Wb1.Sheets(2).Name, Wb1.Sheets(3).Name, Wb1.Sheets(4).Name)).Copy Before:=wb2.Sheets(1)
wb2.Sheets(wb2.Sheets.Count).Delete
wb2.SaveAs ThisWorkbook.Path & "\" & RN & " WK " & WMWK, FileFormat:=52
Call BreakLinks(wb2)
wb2.Close savechanges:=True
With Application
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
End With
End Sub
Sub BreakLinks(ByRef wb2 As Workbook)
Dim LoseLinks As Variant
On Error Resume Next
LoseLinks = wb2.LinkSources(Type:=xlLinkTypeExcelLinks)
On Error GoTo 0
If Not IsEmpty(LoseLinks) Then
For i = 1 To UBound(LoseLinks)
wb2.BreakLink _
Name:=LoseLinks(i), _
Type:=xlLinkTypeExcelLinks
Next i
Code:
Dim Wb1 As Workbook
Dim wb2 As Workbook
Dim RN As String
RN = Range("d1").Value
Dim WMWK As String
WMWK = Sheets("fineline name").Range("j4").Value
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
End With
Set Wb1 = ActiveWorkbook
Set wb2 = Application.Workbooks.Add(1)
Wb1.Sheets(Array(Wb1.Sheets(1).Name, Wb1.Sheets(2).Name, Wb1.Sheets(3).Name, Wb1.Sheets(4).Name)).Copy Before:=wb2.Sheets(1)
wb2.Sheets(wb2.Sheets.Count).Delete
wb2.SaveAs ThisWorkbook.Path & "\" & RN & " WK " & WMWK, FileFormat:=52
Call BreakLinks(wb2)
wb2.Close savechanges:=True
With Application
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
End With
End Sub
Sub BreakLinks(ByRef wb2 As Workbook)
Dim LoseLinks As Variant
On Error Resume Next
LoseLinks = wb2.LinkSources(Type:=xlLinkTypeExcelLinks)
On Error GoTo 0
If Not IsEmpty(LoseLinks) Then
For i = 1 To UBound(LoseLinks)
wb2.BreakLink _
Name:=LoseLinks(i), _
Type:=xlLinkTypeExcelLinks
Next i
Code: