Michaelpfreem
Board Regular
- Joined
- Mar 14, 2008
- Messages
- 92
Hello all,
I am having an issue breaking links in a document using VBA.
Basically I have a document (report) with lots of tabs and formulas and links etc. the macro copies some of the tabs to a new workbook breaks the links, then password protects the new workbook and saves it.
That all works fine, the macro generates the file with the password protection. The problem is it only seems to break the links on the first worksheet, not all of them. Interestingly I tried to replicate the process manually copying each worksheet one at a time and breaking the links, this approach worked, but the macro won't, i even tried recording the macro as I tried the manual attempt and the macro failed to break all the links, it only worked fo rthe first worksheet.
I have tried all sorts of different approachs to get round this problem (see code below). has anyone come across this problem and is there any other way people have found to break links that i might be able to try.
Many thanks in advance for you help:
METHOD 1
****** http-equiv="Content-Type" content="text/html; charset=utf-8">****** name="ProgId" content="Word.Document">****** name="Generator" content="Microsoft Word 11">****** name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:%5CDOCUME%7E1%5Cmfr%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"><!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><style> <!-- /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:EN-GB;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:35.4pt; mso-footer-margin:35.4pt; mso-paper-source:0;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]--> Sub Export()<o></o>
<o> </o>
Dim myfilename As String<o></o>
Dim Links As Variant<o></o>
Dim i As Integer<o></o>
<o> </o>
myfilename = "C:\" & Sheets("Control").Range("BC1").Value & ".xls"<o></o>
<o></o>
Windows("report.xls").Activate<o></o>
Sheets(Array("Page1", "Page2", " Page3", " Page4", " Page5", _<o></o>
" Page6", " Page7", " Page8", " Page9", " Page10")). _<o></o>
Select<o></o>
Sheets("Page1").Activate<o></o>
Sheets(Array("Page1", "Page2", " Page3", " Page4", " Page5", _<o></o>
" Page6", " Page7", " Page8", " Page9", " Page10")). _<o></o>
Copy
<o> </o>
With ActiveWorkbook<o></o>
Links = .LinkSources(xlExcelLinks)<o></o>
If Not IsEmpty(Links) Then<o></o>
For i = 1 To UBound(Links)<o></o>
.BreakLink Links(i), xlLinkTypeExcelLinks<o></o>
Next i<o></o>
End If<o></o>
End With<o></o>
<o> </o>
ActiveWorkbook.SaveAs Filename:=myfilename, _<o></o>
FileFormat:=xlNormal, Password:="", WriteResPassword:="", _<o></o>
ReadOnlyRecommended:=False, CreateBackup:=False<o></o>
<o> </o>
<o> </o>
Sheets("Page1").Protect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True<o></o>
Sheets("Page2").Protect Password:=" password ", DrawingObjects:=True, Contents:=True, Scenarios:=True<o></o>
Sheets("Page3").Protect Password:=" password ", DrawingObjects:=True, Contents:=True, Scenarios:=True<o></o>
Sheets("Page4").Protect Password:=" password ", DrawingObjects:=True, Contents:=True, Scenarios:=True<o></o>
Sheets("Page5").Protect Password:=" password ", DrawingObjects:=True, Contents:=True, Scenarios:=True<o></o>
Sheets("Page6").Protect Password:=" password ", DrawingObjects:=True, Contents:=True, Scenarios:=True<o></o>
Sheets("Page7").Protect Password:=" password ", DrawingObjects:=True, Contents:=True, Scenarios:=True<o></o>
Sheets("Page8").Protect Password:=" password ", DrawingObjects:=True, Contents:=True, Scenarios:=True<o></o>
Sheets("Page9").Protect Password:=" password ", DrawingObjects:=True, Contents:=True, Scenarios:=True<o></o>
Sheets("Page10").Protect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True<o></o>
ActiveWorkbook.UpdateLinks = xlUpdateLinksNever<o></o>
ActiveWorkbook.Save<o></o>
ActiveWorkbook.Close<o></o>
Windows("report.xls").Activate<o></o>
<o></o>
End Sub
METHOD 2
****** http-equiv="Content-Type" content="text/html; charset=utf-8">****** name="ProgId" content="Word.Document">****** name="Generator" content="Microsoft Word 11">****** name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:%5CDOCUME%7E1%5Cmfr%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"><!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><style> <!-- /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:EN-GB;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:35.4pt; mso-footer-margin:35.4pt; mso-paper-source:0;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]--> Sub Export2()<o></o>
<o> </o>
Dim myfilename As String<o></o>
Dim bookname As String<o></o>
Dim Links As Variant<o></o>
Dim i As Integer<o></o>
<o> </o>
myfilename = "C:\" & Sheets("Control").Range("BC1").Value & ".xls"<o></o>
bookname = Sheets("Control").Range("BC1").Value & ".xls"<o></o>
<o></o>
Windows("report.xls").Activate<o></o>
Sheets("Page1").Select<o></o>
Sheets("Page1").Activate<o></o>
Sheets("Page1").Copy<o></o>
<o> </o>
With ActiveWorkbook<o></o>
Links = .LinkSources(xlExcelLinks)<o></o>
If Not IsEmpty(Links) Then<o></o>
For i = 1 To UBound(Links)<o></o>
.BreakLink Links(i), xlLinkTypeExcelLinks<o></o>
Next i<o></o>
End If<o></o>
End With<o></o>
<o> </o>
ActiveWorkbook.UpdateLinks = xlUpdateLinksNever<o></o>
<o> </o>
ActiveWorkbook.SaveAs Filename:=myfilename, _<o></o>
FileFormat:=xlNormal, Password:="", WriteResPassword:="", _<o></o>
ReadOnlyRecommended:=False, CreateBackup:=False<o></o>
<o></o>
ActiveWorkbook.Save<o></o>
ActiveWorkbook.Close<o></o>
Workbooks.Open (myfilename)<o></o>
<o></o>
Windows("report.xls").Activate<o></o>
Sheets("Page2").Select<o></o>
Sheets("Page2").Activate<o></o>
Sheets("Page2").Copy Before:=Workbooks(bookname).Sheets(1)<o></o>
<o></o>
Windows(bookname).Activate<o></o>
Sheets("Page2").Select<o></o>
<o></o>
With ActiveWorkbook<o></o>
Links = .LinkSources(xlExcelLinks)<o></o>
If Not IsEmpty(Links) Then<o></o>
For i = 1 To UBound(Links)<o></o>
.BreakLink Links(i), xlLinkTypeExcelLinks<o></o>
Next i<o></o>
End If<o></o>
End With<o></o>
<o> </o>
ActiveWorkbook.UpdateLinks = xlUpdateLinksNever<o></o>
ActiveWorkbook.Save<o></o>
ActiveWorkbook.Close<o></o>
Workbooks.Open (myfilename)<o></o>
<o></o>
((((((REPEAT FOR REST OF THE PAGES)))))
<o> </o>
ActiveWorkbook.UpdateLinks = xlUpdateLinksNever<o></o>
ActiveWorkbook.Save<o></o>
ActiveWorkbook.Close<o></o>
Windows("report.xls").Activate
<o> </o>
End Sub
Thanks again,
Mike
I am having an issue breaking links in a document using VBA.
Basically I have a document (report) with lots of tabs and formulas and links etc. the macro copies some of the tabs to a new workbook breaks the links, then password protects the new workbook and saves it.
That all works fine, the macro generates the file with the password protection. The problem is it only seems to break the links on the first worksheet, not all of them. Interestingly I tried to replicate the process manually copying each worksheet one at a time and breaking the links, this approach worked, but the macro won't, i even tried recording the macro as I tried the manual attempt and the macro failed to break all the links, it only worked fo rthe first worksheet.
I have tried all sorts of different approachs to get round this problem (see code below). has anyone come across this problem and is there any other way people have found to break links that i might be able to try.
Many thanks in advance for you help:
METHOD 1
****** http-equiv="Content-Type" content="text/html; charset=utf-8">****** name="ProgId" content="Word.Document">****** name="Generator" content="Microsoft Word 11">****** name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:%5CDOCUME%7E1%5Cmfr%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"><!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><style> <!-- /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:EN-GB;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:35.4pt; mso-footer-margin:35.4pt; mso-paper-source:0;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]--> Sub Export()<o></o>
<o> </o>
Dim myfilename As String<o></o>
Dim Links As Variant<o></o>
Dim i As Integer<o></o>
<o> </o>
myfilename = "C:\" & Sheets("Control").Range("BC1").Value & ".xls"<o></o>
<o></o>
Windows("report.xls").Activate<o></o>
Sheets(Array("Page1", "Page2", " Page3", " Page4", " Page5", _<o></o>
" Page6", " Page7", " Page8", " Page9", " Page10")). _<o></o>
Select<o></o>
Sheets("Page1").Activate<o></o>
Sheets(Array("Page1", "Page2", " Page3", " Page4", " Page5", _<o></o>
" Page6", " Page7", " Page8", " Page9", " Page10")). _<o></o>
Copy
<o> </o>
With ActiveWorkbook<o></o>
Links = .LinkSources(xlExcelLinks)<o></o>
If Not IsEmpty(Links) Then<o></o>
For i = 1 To UBound(Links)<o></o>
.BreakLink Links(i), xlLinkTypeExcelLinks<o></o>
Next i<o></o>
End If<o></o>
End With<o></o>
<o> </o>
ActiveWorkbook.SaveAs Filename:=myfilename, _<o></o>
FileFormat:=xlNormal, Password:="", WriteResPassword:="", _<o></o>
ReadOnlyRecommended:=False, CreateBackup:=False<o></o>
<o> </o>
<o> </o>
Sheets("Page1").Protect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True<o></o>
Sheets("Page2").Protect Password:=" password ", DrawingObjects:=True, Contents:=True, Scenarios:=True<o></o>
Sheets("Page3").Protect Password:=" password ", DrawingObjects:=True, Contents:=True, Scenarios:=True<o></o>
Sheets("Page4").Protect Password:=" password ", DrawingObjects:=True, Contents:=True, Scenarios:=True<o></o>
Sheets("Page5").Protect Password:=" password ", DrawingObjects:=True, Contents:=True, Scenarios:=True<o></o>
Sheets("Page6").Protect Password:=" password ", DrawingObjects:=True, Contents:=True, Scenarios:=True<o></o>
Sheets("Page7").Protect Password:=" password ", DrawingObjects:=True, Contents:=True, Scenarios:=True<o></o>
Sheets("Page8").Protect Password:=" password ", DrawingObjects:=True, Contents:=True, Scenarios:=True<o></o>
Sheets("Page9").Protect Password:=" password ", DrawingObjects:=True, Contents:=True, Scenarios:=True<o></o>
Sheets("Page10").Protect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True<o></o>
ActiveWorkbook.UpdateLinks = xlUpdateLinksNever<o></o>
ActiveWorkbook.Save<o></o>
ActiveWorkbook.Close<o></o>
Windows("report.xls").Activate<o></o>
<o></o>
End Sub
METHOD 2
****** http-equiv="Content-Type" content="text/html; charset=utf-8">****** name="ProgId" content="Word.Document">****** name="Generator" content="Microsoft Word 11">****** name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:%5CDOCUME%7E1%5Cmfr%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"><!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><style> <!-- /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:EN-GB;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:35.4pt; mso-footer-margin:35.4pt; mso-paper-source:0;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]--> Sub Export2()<o></o>
<o> </o>
Dim myfilename As String<o></o>
Dim bookname As String<o></o>
Dim Links As Variant<o></o>
Dim i As Integer<o></o>
<o> </o>
myfilename = "C:\" & Sheets("Control").Range("BC1").Value & ".xls"<o></o>
bookname = Sheets("Control").Range("BC1").Value & ".xls"<o></o>
<o></o>
Windows("report.xls").Activate<o></o>
Sheets("Page1").Select<o></o>
Sheets("Page1").Activate<o></o>
Sheets("Page1").Copy<o></o>
<o> </o>
With ActiveWorkbook<o></o>
Links = .LinkSources(xlExcelLinks)<o></o>
If Not IsEmpty(Links) Then<o></o>
For i = 1 To UBound(Links)<o></o>
.BreakLink Links(i), xlLinkTypeExcelLinks<o></o>
Next i<o></o>
End If<o></o>
End With<o></o>
<o> </o>
ActiveWorkbook.UpdateLinks = xlUpdateLinksNever<o></o>
<o> </o>
ActiveWorkbook.SaveAs Filename:=myfilename, _<o></o>
FileFormat:=xlNormal, Password:="", WriteResPassword:="", _<o></o>
ReadOnlyRecommended:=False, CreateBackup:=False<o></o>
<o></o>
ActiveWorkbook.Save<o></o>
ActiveWorkbook.Close<o></o>
Workbooks.Open (myfilename)<o></o>
<o></o>
Windows("report.xls").Activate<o></o>
Sheets("Page2").Select<o></o>
Sheets("Page2").Activate<o></o>
Sheets("Page2").Copy Before:=Workbooks(bookname).Sheets(1)<o></o>
<o></o>
Windows(bookname).Activate<o></o>
Sheets("Page2").Select<o></o>
<o></o>
With ActiveWorkbook<o></o>
Links = .LinkSources(xlExcelLinks)<o></o>
If Not IsEmpty(Links) Then<o></o>
For i = 1 To UBound(Links)<o></o>
.BreakLink Links(i), xlLinkTypeExcelLinks<o></o>
Next i<o></o>
End If<o></o>
End With<o></o>
<o> </o>
ActiveWorkbook.UpdateLinks = xlUpdateLinksNever<o></o>
ActiveWorkbook.Save<o></o>
ActiveWorkbook.Close<o></o>
Workbooks.Open (myfilename)<o></o>
<o></o>
((((((REPEAT FOR REST OF THE PAGES)))))
<o> </o>
ActiveWorkbook.UpdateLinks = xlUpdateLinksNever<o></o>
ActiveWorkbook.Save<o></o>
ActiveWorkbook.Close<o></o>
Windows("report.xls").Activate
<o> </o>
End Sub
Thanks again,
Mike