title: VBA: test if workbook is nothing
body: <p>Edit: My real question is how to test if object was set that was instantiated. I am not really looking to "correct" my code. Its just an example.</p>
  
  <p>I have a function that returns a workbook:</p>
  
  <p>Edit: Added code</p>
  
  <pre><code>Sub GetWb() as Workbook
  
  Application.DisplayAlerts = False
  Application.EnableEvents = False
  On Error Resume Next
  Set wM = Application.Workbooks.Open("Z:\somepath.xlsm", ReadOnly:=True)
  Application.EnableEvents = True
  Application.DisplayAlerts = True
  On Error GoTo 0
  
  end sub
  </code></pre>
  
  <p>In another sub I want to check if that object was set properly by the function. I usually do something like this with objects generally:</p>
  
  <pre><code>dim w as Workbook
  set w = GetWb
  if w is nothing then
  debug.print "no workbook"
  else
  debug.print "workbook"
  end if
  </code></pre>
  
  <p>However, the <code>is nothing</code> test does not work because the object is instantiated, but was not set so it is something, not nothing.</p>
  
  <p>I have resorted to this ugly solution, which works fine:</p>
  
  <pre><code>dim w as Workbook
  set w = GetWb
  on error goto someerrorhandling
  if w.name = "" then
  end if
  on error goto 0
  'other code here
  
  
  someerrorhandling: 
  msgbox "no workbook"
  </code></pre>
  
  <p>In other words, I check a property of the object to force an error, or not. There must be a better/cleaner way.</p>
  
  <p>I checked and this link states that the way I am doing it is the best way:
  <a href="https://stackoverflow.com/questions/8472907/vba-conditional-is-nothing">VBA: Conditional - Is Nothing</a></p>