Hi, I'm working on creating a script that I will provide a list of SharePoint 2007 document libraries to which will do the following:
- In Excel, find formulas within cells that link to other workbooks + sheets (XLS/XLSX)
- If found, replace the formula link http://serverold/site/doclib/ withhttp://servernew/sites/sitecollection/doclib and save
- Else, close the workbook and move onto the next
- Log full URL and filename of any changes
Using the code on this link as my starting point I cannot get the following working:
1. The regex expression to make the script detect the URL in the formulas
2. modify the script to replace the old path with the new path in the formula within cells.
3. a for each branch to deal with when the match is found (save and close) and when it's not found (just close)
I'm not going into detail on all the research I've done (info is very light on the ground), just that it is mentioned on another thread that you can enumerate these links centrally in Excel but no example or links were given and when I've tried to enumerate the links collection in PowerShell (with Excel 2010 installed) it is empty with the example workbook I'm using which I know as "links" in that sense.
Example to enumerate link collections:
$File = "C:\temp\example.xls" $Excel = New-Object -ComObject Excel.Application $Excel.visible = $true $Workbook = $Excel.workbooks.open($file) $Workbook.LinkSources
So it begs the question, which method is right?
Example Excel formula
=+'http://serverold/site/site/Work in Progress Documents/Statements/[Hierarchy2011.xls]Reports'!$AD$37+'http://serverold/site/site/Work in Progress Documents/
Script to enumerate links (from the link I mentioned as my starting point) -
$path = "C:\temp" $excelSheets = Get-Childitem -Path $path -Include *.xls,*.xlsx -Recurse $excel = New-Object -comobject Excel.Application $excel.visible = $false foreach($excelSheet in $excelSheets) { $workbook = $excel.Workbooks.Open($excelSheet) "There are $($workbook.Sheets.count) sheets in $excelSheet" For($i = 1 ; $i -le $workbook.Sheets.count ; $i++) { $worksheet = $workbook.sheets.item($i)"`tLooking for links on $($worksheet.name) worksheet" $rowMax = ($worksheet.usedRange.rows).count $columnMax = ($worksheet.usedRange.columns).count For($row = 1 ; $row -le $rowMax ; $row ++) { For($column = 1 ; $column -le $columnMax ; $column ++) { [string]$formula = $workSheet.cells.item($row,$column).formula if($formula -match "\w?:\\\w*\\\[\w*\.xls\w?\]") {"`t`t$($formula)"} } #end for $column } #end for $row $worksheet = $rowmax = $columnMax = $row = $column = $formula = $null } #end for $workbook.saved = $true $workbook.close() } #end foreach $excel.quit() $excel = $null [gc]::collect() [gc]::WaitForPendingFinalizers()
Thanks to anyone who can help and for your time.
Bests,
Ash