问题
I have manually pasted a large number of linked pictures into a 2010 Excel spreadsheet using insert picture -> select picture location --> link to file. These pictures are part of a report. I update the pictures using R each quarter, and my report automatically updates. Perfect.
I now need to change the directory where the plots are kept, and need to update the links. As there are around 200 of them (its a big report), I want to do this in VBA. Whilst I can loop through the pictures ok (ActiveSheet.Pictures), I can't seem to find the links/address. Any idea how I can see the underlying file location so I might change it - the reference has to be stored somewhere (note - these don't seem to be stored as hyperlinks).
回答1:
Any idea how I can see the underlying file location so I might change it - the reference has to be stored somewhere
- Create a new folder
- Paste a copy of the .xlsx or .xlsm excel file
- Uncompress the file with a zip tool (i'm using 7-Zip)
- Delete the .xlsx or .xlsm file (optional)
- Now we have all the component parts of the original file as plain text xml files and folders
- Inside the folder xl\drawings\ _rels there are files named as drawing2.xml.rels, drawing3.xml.rels, ...
It seems that each file corresponds to a sheet and stores the paths to images in this format:
Target="file:///C:\Users\myusername\Documents\MyImageFolder\My%20Image%20Name.png"
- Change the paths with a text editor
- Compress all the contents of the folder to a .zip
- Change the extension to the original .xlsx or .xlsm
These steps could be automated with VBA, AutoIt, etc., here some references:
- An example with AutoIt and 7-zip
- http://www.jkp-ads.com/Articles/Excel2007FileFormat.asp
- http://www.jkp-ads.com/Articles/Excel2007FileFormat02.asp
- Ron de Bruin zip examples with VBA
- Read and change multiple XML files in Excel (2007) VBA
回答2:
Excel uses the Formula Bar as the link in this case, just the same as it would link between ranges in two different worksheets. When I select a linked picture, the formula below populates in the formula bar:
=[TrialWB.xlsm]Sheet1!$C$3:$E$6
You can access the Shape's formula using the code below and inserting your picture's specific name:
ActiveSheet.Pictures("Picture Name").Formula = "=[TrialWB.xlsm]Sheet1!$C$4:$E$6"
In updating the links, you'll have to change the file path in the formula. This might look like:
ActiveSheet.Pictures("Picture Name").Formula = "='C:\Reports2015\[TrialWB.xlsm]Sheet1'!$C$4:$E$6"
changing to
ActiveSheet.Pictures("Picture Name").Formula = "='C:\Reports2016\[TrialWB.xlsm]Sheet1'!$C$4:$E$6"
This question may be of some further assistance for accessing formulas: Excel: create image from cell range
And here's a useful Microsoft page for formula file path editing: https://support.office.com/en-us/article/Create-an-external-reference-link-to-a-cell-range-in-another-workbook-c98d1803-dd75-4668-ac6a-d7cca2a9b95f
来源:https://stackoverflow.com/questions/37145369/change-path-to-picture-links-in-excel