top of page

Keeping the Code Navigator in step with your code

4 days ago
4 min read

Updated: 2 days ago

This is the second of three posts about VBE_Extras' new Code Navigator. If you missed the first, Introducing the Code Navigator explains what it is and how to get around with it.


The declarations shown in the Code Navigator's "tree" are only useful if they match your code. This post is about how the Code Navigator stays up to date, and why it is careful about when it does.


Refreshing


The Code Navigator's "tree" shows your Projects as they were when it was last refreshed. Click the Refresh (the circular arrow icon, in the Code Navigator's toolbar) button and it reads them again. A refresh runs in the background, so you can carry on working while it does. For a large Project, a full refresh can take a few seconds; while it runs, the button becomes Cancel (which, if clicked, stops the refresh and leaves the tree as it was).


Most refreshes read only the Modules that have changed. And even with automatic refreshing switched off, the Code Navigator still refreshes when you:


  • Use a VBE_Extras command that changes your code (Rename, or Make 'Option Explicit', for example)

  • Show the Code Navigator when it was previously hidden (it checks for changes first)

  • Use the 'Show in Code Navigator' command

  • Click into the Code Navigator when a refresh is already waiting


If you use a node whose declaration no longer exists (because you deleted or changed it since the last refresh), a dialog box will be shown saying so.


Refresh automatically


Check Refresh automatically in the Options (the gear icon, in the Code Navigator's toolbar) and the Code Navigator keeps itself up to date as you work. It notices:


  • Projects and Modules being added, removed or renamed

  • Changes to the code of the Module you are working in

  • You coming back to the VBE from the host application (Excel, Access etc) ... or any other program

  • VBE_Extras commands being performed


A new Sub appearing in the Code Navigator shortly after it is typed

Why it waits for the right moment

Here is something you may not know about the VBE: whenever anything reads a Module's code, the VBE first finishes off (or "commits") the line that you are editing. It removes a trailing space, adds a missing closing speech mark, and checks the line's syntax, turning it red if it is incomplete.


So if the Code Navigator refreshed while you were part-way through a line, the space you had just typed would vanish, or the line would turn red in front of you. To stop that happening, it refreshes only at a safe moment:


  • Your last action has to be one that finishes a line: Enter, an arrow key, Tab, Home, End, Page Up, Page Down, Backspace, Delete, or a mouse click

  • You have paused for about two seconds

  • The line the cursor is on does not look unfinished ... for example, it doesn't end with a space (or a number of other characters that can't "legally" come at the end of a line); it has no string missing its closing speech mark; and it does not have more "(" than ")" (comments are ignored, and for a statement continued over several lines, the whole statement is checked)

  • Your code is not running or stopped at a breakpoint, the Code Navigator is showing, and the VBE is the active window


A line can be finished and still look unfinished: a line that is already red because it ends with "+", for example. While the cursor stays on a line like that, automatic refreshing waits ... move to another line and it carries on.


With both Refresh automatically and Follow the code pane switched off, the Code Navigator does no work in the background at all.


The 'Refresh automatically' option in the Code Navigator's Options menu

Following the code pane


Three options make the tree follow what you are doing in the code pane:


  • Follow the code pane keeps the tree's selection on the Module you are working in, and on the declaration the cursor is in, as far as the tree shows it

  • Expand to show the active Module expands the Project (and its Modules group) when you move to a different Module, so that the Module's node can be selected. It never collapses anything

  • Follow the cursor when you expand a node - when you expand the node that is selected, the selection moves down to the declaration the cursor is in (or, for example, in a UserForm designer, to the selected Control)


The second and third options apply only while "Follow the code pane" is checked.


The Code Navigator's selection following the cursor in the code pane

While your code is running


While your VBA code is running, or is stopped at a breakpoint, the Code Navigator does not refresh, and the tree does not update. You can still show and hide the Code Navigator.


When the code finishes, or you stop it, the Code Navigator catches up: by itself if "Refresh automatically" is checked, otherwise when you next click Refresh.


Next


The last post in this series is about making the Code Navigator your Project Explorer (and making it look the way you want). Everything in this post is also in the "Code Navigator" chapter of the user guide.

Comments


bottom of page