title: Opening or activating workbook in Excel from VBS
body: <p>I am writing a small VBScript to do the following:</p>
  
  <ol>
  <li>Check if Excel is open; if not open it.</li>
  <li>If Excel is open, check if a specific workbook is open.</li>
  <li>If workbook is open, make it active; if not, open it.</li>
  </ol>
  
  <p>So far I have been able to write the following code:</p>
  
  <pre><code>ExcelFileName = "....xlsx"
  
  On Error Resume Next
  Set xl = GetObject(, "Excel.Application")
  IF Err Then
      If Err.Number = 429 Then
      WScript.Echo "Workbook not open (Excel is not running)."
      Else
      WScript.Echo Err.Description &amp; " (0x" &amp; Hex(Err.Number) &amp; ")"
      End If
      WScript.Quit 1
  End If
  On Error Goto 0
  
  Set wb = Nothing
  For Each obj In xl.Workbooks
      If obj.Name = ExcelFileName Then
      Set wb=obj
      xl.DisplayAlerts = False
      wb.Save
      Exit For
      End If
  Next
  
  If wb Is Nothing Then
      xl.Workbooks.Open("C:\...")
  End If
  
  Set xl = Nothing
  Set wb = Nothing
  </code></pre>
  
  <p>But if Excel is not already open, it silently fails to open a new instance.</p>