#16
|
|||
|
|||
Hi, Gmaxey! Thank you for the information on status flag. It looks as if Vivka had never stopped learnig. Some (like myself) are much slower. And thank you for the suggestions and the new code, which works perfectly on its own but misses the penultimate line of verse when inserted (with obvious modifications) in a larger macro. All the best. |
#17
|
|||
|
|||
RobiNew, thank you again for good words about my modest help! Greg, thank you for sharing your precious knowledge! Regarding your comments: 1) I thought that 'as Range' was optional when delcaring a range (from now on I will remember that a range MUST be explicitely declared); 2) I fully agree that exiting the sub leaves the final commands unexecuted. Now it's clear why you keep using lbl_exit. Thank you for one more lesson!
|
#18
|
|||
|
|||
vivka,
You thought correctly. It is optional. All variables that are implicitly declared are of type Variant. Variables of type Variant require more memory resources than most other variables. While in the case we have at present, it probably doesn't mater but generally your code will be more efficient if you declare variables explicitly and with a specific data type. Code:
Sub ScratchMacro() 'A basic Word Macro coded by Gregory K. Maxey 'Illustrates an implicit variable (not declared using a declaration statement e.g., Dim) Set oRng = ActiveDocument.Range oRng.Select lbl_Exit: Exit Sub End Sub Sub ScratchMacroII() 'A basic Word Macro coded by Gregory K. Maxey 'Illustrates an two explicit variables 1) The compilier resolves as variant 2) Is declared as variant Dim oRng Dim oRng2 As Variant Set oRng = ActiveDocument.Range 'Notice after you type oRng. that the properties and methods of the Word range object do not appear. oRng.Select Set oRng2 = ActiveDocument.Range 'Notice after you type oRng2. that the properties and methods of the Word range object do not appear. oRng2.InsertAfter "more text" lbl_Exit: Exit Sub End Sub Sub ScratchMacroIII() 'A basic Word Macro coded by Gregory K. Maxey 'Illustrates an explicit variable set to a specific data type Dim oRng As Range Set oRng = ActiveDocument.Range 'Notice after you type oRng. that the properties and methods of the Word range object do not appear. oRng.Select 'Note: I typically use the OptionExplicit statement at the top of my code window to force me to 'explicity declare all variables. For example, your code would not compile because you have 'an implicit variable "Para" lbl_Exit: Exit Sub End Sub Sub ScratchMacroIV() 'A basic Word Macro coded by Gregory K. Maxey 'Why to I used the lbl_Exit and Exit Sub lines. MsgBox "Mainly as a matter of style and practice." & vbCr + vbCr _ & "I find myself updating a lot of code written by others weeks, years or decaces ago" _ & " and I use those two lines as my marker that I have finished revising a procedure." MsgBox "Secondarily it is my escape clause from a procedure." & vbCr + vbCr _ & "One of my earlier mentors told me that while it didn't matter anymore (if it ever did) he just never liked to reach" _ & " the End Sub line in procedure." _ & " I also use it to escape err handling labels and as a exit point after error handling." On Error GoTo Err_Handler Err.Raise 6 lbl_Exit: Exit Sub Err_Handler: Resume lbl_Exit End Sub |
#19
|
|||
|
|||
Thank you, Greg, for your detailed explanation! I will carefully analyze your examples. By the way, your site is one of my bookmarks. A few days ago I recommended it to a visitor of a Ukrainian computer forum.
|
#20
|
|||
|
|||
Glad to help. Thanks
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Format Word Document - Remove timestamps on individual lines | lbeck | Word | 4 | 06-21-2023 12:47 PM |
How do I format indented bulleted lines??? HELP PLEASE!!! | nicholeproffitt | Word | 1 | 02-05-2015 05:42 PM |
Print table and on verse form vba | LaercioNB | Excel | 1 | 08-09-2013 06:03 PM |
How to remove unwanted lines - I cannot even find how/where they are inserted! | nickib | Word | 7 | 08-09-2013 06:01 AM |
Find and Replace multiple lines/paragraphs | jcw | Word | 1 | 11-18-2011 11:47 AM |