As part of a data migration process, I need a script/macro to access all files in the source location and check for interlinked documents so that those links can be re-established post migration (already completed). Apart from direct links, if a file has a macro that is trying to access another file, that link should also be restored post migration(need a solution for this).
So basically, is there a way the access the contents of a macro in a file from another script/macro (to check for the documents the first macro is trying to access so that it works even post migration)?
Yes. You can actually read the contents of a module (or any VBA project component) by programming the Visual Basic Editor (VBE) itself. You can do so by following below steps.
Once successful, you can then write a code to retrieve the lines of code from another module or project in another workbook. Something like below which prints codes found in a standard module:
You can use the
Find
method if you need specific lines only:Not sure if this answers your specific question but hope this gets you started.
Btw, it is important to tick Trust access to the VBA project object model under Developer Macro Settings to make this work.
You can find above under Developer Tab > Code > Macro Security.
And of course the project should be unlocked.