Why Convert from Early Binding to Late BindingI have found that developing using Early Binding is quicker than using Late Binding, mainly because I depend on Intellisense to prompt me..
I have also found that when I distribute with Early Binding there can be problems because I have a client who was ahead of me in releases of MS-Office, i.e. they had 2013 while my company was still using 2010. Each copied is configured for the user: Directory structures for referencing files and auxiliary programs are in a supporting worksheet. That all worked well when I moved it up, but not when I moved it back to me.
Some reading indicates that Early Bound project files are larger than late bound. I have not confirmed this, but I'll take them at their word. They're more experienced than I.
Refreshers: Binding, Early and Late BindingA VBA project is developed using a host Object Library, e.g. MS-Excel. Binding is tying to that host Object Library a "foreign" Object Library (e.g. for MS-Word) in order to use its features and manipulate files that are native to the foreign environment.
With Early Binding, the foreign, or "hosted", Object Library is selected using the Tools...References dialogue in the Virtual Basic Environment (VBE). This makes the hosted Object Library available for Intellisense and at compile time.
When Late Binding is used the hosted Object Library is unchecked in the Tools...References dialogue. The project is made aware of the foreign Object Library as the VBA code is executed, i.e. at run time.
Conversion StepsConverting from Early to Late Binding is more tedious than hard.
- Remove the Reference to the hosted Object Library.
That was the easy part.
Now comes the tedious part.
- Change data types in declarations for hosted Object Library objects to "as Object".
- Compile to identify any declarations that were not changed to "as Object".
- If there were any compiler errors, return to Step 2.
- Once compilation completes without errors, run the code. This will
Remove the Reference to the hosted Object Library
- Access the Visual Basic Environment (VBE)
- Click on the Tools menu and select References
Opening the References dialogue
- Uncheck the Object Library to remove, in this case "Microsoft Word 14.0 Object Library"
References List with Hosted Object Library
- Click OK then save the file
Change Data Types to "as Object"
- For each module, scan for Dim, Public, and Global declarations of object variables.
- Identify whether each is a native or foreign Data Type, e.g. "as Document" in an Excel project would be foreign whereas "as Workbook" or "as Worksheet" would be native.
- Change to "as Object"
NOTE: Be aware that some Object Type-names occur in multiple Object Libraries. For example, "Range" is used in both the Word and Excel Object Libraries but they have different Methods and Properties.
How to make Early-to-Late Binding Easier
- Highlight the "as <object>" portion of the declaration
- Click Ctl-F to show the Find/Replace dialogue
- Click the Replace button
- Type "as object" in the Replace With: text box
- Choose whether to perform the action in the Current Procedure. Current Module, or Current Project.
- Click the "Replace" or "Replace All" button, depending on confidence that the phrase "As Documents" does not appear elsewhere and in a context where it should not be changed, e.g. in comments.
- Unbind the foreign Object Library using the Tools-References dialogue
- Change to the "as Object" declarations.
- Compile and run to test.
- Rebind the foreign Object Library using the Tools-References dialogue.
- Continue development in Early Bound mode.