View Single Post
 
Old 12-19-2020, 08:19 AM
gmaxey gmaxey is offline Windows 10 Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,598
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

John,


No offense and as we are simply sharing opinions, I don't wish you to take offense either. We each have our own styles and I don't know which code another user might prefer. However, lbl_Exit: is simply a label and Exist Sub is a relatively common and often used line of code. In fact, you used it in your example. Neither, would I call "obscure."

As a standard practice (in fact I have a autocomplete function) to start all procedures that I write) like this:



Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey


lbl_Exit:
Exit Sub
End Sub



I then give is a name and go from there. Now if I wrote:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Msgbox "Hello John"

lbl_Exit:
Exit Sub
End Sub


Then understandably someone might ask, "What is the purpose of the lbl_Exit: and Exit sub lines. Well, with my style, I always Exit Sub. That is the purpose of that line and again in this case, other than style, lbl_Exit has no purpose.

Now, let's consider your use of Exit Sub. In your procedure, you have the following two lines:

Set wdDoc as Nothing
Application.ScreenUpdating = True


Now I assume that since you included those two lines, that you wanted to execute them. However, you also use Exit Sub above those lines so if Exit Sub runs then those two lines are bypassed. A novice, intermediate or even wizard might wander why you wrote those lines if you don't want them to execute? So using my style, I would include those two lines after the lbl_Exit line

lbl_Exit:
Set wdDoc = Nothing
Application.ScreenUpdating = True
Exit Sub

End Sub


and use GoTo lbl_Exit where you used Exit Sub.


Or when my procedures have an error handler I might do something like this:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oDoc as Document
Set oDoc = ActiveDocument

On Error GoTo Err_Demo
Err.Raise 6

lbl_Exit:
Set oDoc = Nothing

Exit Sub
Err_Demo:
Resume lbl_Exit

End Sub


Make sense?
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote