VBA code being embedded in a host document might be very practical for certain aspects of both development and deployment, but let’s face it, it also makes using source control (e.g. git, SVN, mercurial, etc.) with VBA projects rather frustrating. As a developer, committing source code to a repository is usually a very simple task, because the code files live in the file system, and git can track changes and additions. With VBA, we commit the code that’s exported on the file system, the host document may contain different code, and merging remote changes implies exporting your code again, working out any merge conflicts with the exported code, then re-importing the merged changes into the host document.
Which wouldn’t be so bad… if the VBE had a nice way of exporting more than one single file at a time, and if importing files had an option to replace modules when they already exist… instead of importing the module with a “1” suffix as if that were something anyone ever needed to do!
Did You Know?
The VBE’s “Import File…” command doesn’t make it very obvious, but it does support importing multiple files at once. Simply select multiple files when prompted for what file to import!
Another little known feature of the VBE (one of the few Rubberduck hasn’t enhanced yet) is that its Project Explorer toolwindow is a drag-and-drop destination that can accept files you dragged from the Windows Explorer (⊞+E).
Rubberduck’s Export Project… command prompts for a folder, and then proceeds to export all modules there – overwriting any existing files in that folder. By default, the hotkey for that command is Ctrl+Shift+E, but it can be reconfigured to any key combination you like.
The context menu of Rubberduck’s Code Explorer toolwindow has a Sync Project sub-menu that offers two commands:
- Update Components from Files…
- Replace Contents from Files…
Rubberduck in general needs more documentation, but exactly what these “Sync Project” commands do is something that goes well beyond just using Rubberduck and they really deserve all the attention they can get, since they exist to facilitate an actual development workflow that looks something like this:
This command prompts for source code files to import into your project.
- If the project already contains a module with the same name as one of the imported files, the module is considered the same, and replaced with the imported version.
- If the project does not already contain the imported modules, they’re simply added to the project.
- If the project contains modules with different names than the imported files, these modules remain in the project.
This command also prompts for source code files to import into your project, but the selected files will replace everything in the current project. Because this command is potentially destructive, a confirmation is required.
- The entire project becomes the selected files.
- If the project contained (non-document) modules before, they are removed before the import is performed.
We have a number of open issues (here, here, and here) about getting the “export project” command to take the
@Folder annotations into account, and transpose the virtual folder hierarchy into an actual folder hierarchy on the file system, which would play nicely with version control and would help better organize a VBA repository.
3 thoughts on “Synchronizing your VBA project with files in a folder”
Not only can files be imported from windows explorer, they can be dragged from one open project to another in PE.
Thanks for another great article… looking forward to the next on MVVM (nudge, nudge, wink, wink)
LikeLiked by 1 person
Is it possible to add an option to have Rubberduck convert win1251 text files to utf-8 in src folder on export, and reverse converting utf-8 to win-1251 on import?
I’m not sure what the use case might be (it makes exported files *require* Rubberduck /conversion to re-import), but I wouldn’t reject a pull request that introduces such an option; feel free to open an issue to describe the feature!