title: Simpe VBA sub: Error on start of function, but not after
body: <p>I have a simple function to select a fixed range and prepare for email, which works... but only after running the function the second time. The issue happens right after I open the Excel spreadsheet, I would then "end" the script and run it again, then it works like a charm.</p>
  
  <p>Your help is much appreciated, would very much like to understand why the error is happening.</p>
  
  <p>Error: <strong>Runtime Error 1004: Select method of Worksheet class failed.</strong></p>
  
  <p>On debug, the line ".Parent.Select" is then highlighted from the script below.</p>
  
  <pre><code>Sub Select_Range_now()
     Dim Sendrng As Range
     Dim EndOfLine As Integer
  
     EndOfLine = Find_First() - 1
     Set Sendrng = Worksheets("Output").Range("B1:I" &amp; EndOfLine)
  
     ActiveWorkbook.EnvelopeVisible = True
  
     With Sendrng
         .Parent.Select
         .Select
  
         With .Parent.MailEnvelope
             With .Item
                 .SentOnBehalfOfName = "groupemail@someemail.com"
                 .To = "someothergroupemail@someemail.com"
                 .CC = ""
                 .Subject = "Report"
             End With
         End With
     End With
  End Sub
  </code></pre>
  
  <h2><strong>EDIT:</strong> New find:</h2>
  
  <p>When clicking on "Mail Recipient" option, I get this msgbox: <a href="http://imagebin.org/251272" rel="nofollow">msgbox dialog</a></p>
  
  <p><strong>Email: You can send the entire workbook as an attachment to an email message or send the current sheet as the body of an email message.</strong></p>
  
  <ul>
  <li><strong>Send the entire workbook as an attachment</strong></li>
  <li><strong>Send the current sheet as the message body</strong></li>
  </ul>
  
  <p>Clicking on that button again will not prompt this again and the script works right away. I'm guessing that when running the first time it seems to have trouble handling this dialog, or something!</p>
  
  <p>In case anyone needs to know what the Find_First() function, it's used to find the text ENDOFLINE so I can calculate my selection range:</p>
  
  <pre><code>Function Find_First() As String
     Dim FindString As String
     Dim Rng As Range
     FindString = "ENDOFLINE"
  
     With Sheets("Output").Range("A:I")
         Set Rng = .Find(What:=FindString, _
                         After:=.Cells(.Cells.Count), _
                         LookIn:=xlValues, _
                         LookAt:=xlWhole, _
                         SearchOrder:=xlByRows, _
                         SearchDirection:=xlNext, _
                         MatchCase:=False)
         If Not Rng Is Nothing Then
             'Application.Goto Rng, True
             'MsgBox "row number: " &amp; Rng.Row
             Find_First = Rng.Row
         Else
             'MsgBox "Nothing found"
         End If
     End With
  End Function
  </code></pre>