title: How to set Excel sheet name with powershell variable?
body: <p>I'm trying to set an excel sheet name with Powershell using a variable but am getting this error:</p>
  
  <pre><code>Exception from HRESULT: 0x800A03EC
  At line:14 char:1
  + $ws.Name = $RequestTitle
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : OperationStopped: (:) [], COMException
      + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
  </code></pre>
  
  <p>Here's a snippet of the code:</p>
  
  <pre><code># create new excel object 
  $excel = New-Object -ComObject excel.application
  
  # show the excel object 
  $excel.visible = $True
  
  # add a workbook 
  $workbook = $excel.Workbooks.Add()
  
  # create a variable referencing the first sheet of the wb
  $ws= $workbook.Worksheets.Item(1)
  
  # change the name of the sheet 
  $ws.Name = $NewSheetName
  </code></pre>
  
  <p>But when I do something like: <code>$ws.Name = "some string"</code> it works fine without an error. I've also tried using the <code>$NewSheetName.ToString()</code> method to no avail.</p>
  
  <p>What is the best way to reference a variable to name a sheet? Any help would appreciated!</p>