Thread: [Solved] Word.Range
View Single Post
 
Old 03-08-2017, 05:50 AM
gmaxey gmaxey is offline Windows 7 32bit Office 2016
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

Serpico,

Each of us has our own style of coding and our own style of posting. Some seem to take pleasure in pointing out the quirks or flaws in others and some don't.

Dim Word.Range as Range and Dim Range contained in the same project executed in Word is the same thing. Sometime I use one, sometimes the other. I guess I'm just not very ordinary.

Paul is very rarely in error. Here again he is true to form, both correct and opinionated. Technically, in the example above, lbl_Exit followed by Exit Sub is certainly unnecessary.

However, opinions aside, those two lines of code are in the autotext I use when starting a scratch procedure.

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

lbl_Exit:
Exit Sub
End Sub

... and I "ordinarily" use those two lines of code as a flag to myself that I have finished a review of existing code and as a placeholder for error escaping.

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oDoc As Document
  On Error GoTo Err_Handler
  Set oDoc = Documents(2)
lbl_Exit:
  Exit Sub
Err_Handler:
  Select Case Err.Number
    Case Is = 5941: MsgBox "There is no document 2."
    Case Else
  End Select
  Resume lbl_Exit
End Sub
Here is something for you to ponder:

Code:
Sub A()
Dim oRng As Range
  Set oRng = ActiveDocument.Range
  oRng.Text = "Find me. "
  With oRng.Find
    .Text = "Find me."
    While .Execute
      If .Found Then
        MsgBox "Some choose to use use .Found. Is it neccesary or just a quirk?  You decide."
        oRng.Text = "Found"
      End If
    Wend
  End With
End Sub
Sub B()
Dim oRng As Range
  Set oRng = ActiveDocument.Range
  oRng.Text = "Find me. "
  With oRng.Find
    .Text = "Find me."
    While .Execute
     oRng.Text = "Found"
    Wend
  End With
lblExit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/

Last edited by gmaxey; 03-08-2017 at 03:30 PM.
Reply With Quote