Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #18  
Old 11-08-2023, 06:44 AM
gmaxey gmaxey is offline Find and format lines of verse Windows 10 Find and format lines of verse Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
 



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
Find and format lines of verse 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 format lines of verse Find and Replace multiple lines/paragraphs jcw Word 1 11-18-2011 11:47 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:09 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft