title: Getting selected text in active cell from Excel Interop
body: <p>I am trying to get the selected text of the active cell in Excel. For instance:</p>
  
  <p>Cell F3 Value:</p>
  
  <p>This is a string.</p>
  
  <p>The user has selected cell F3 and in the formula bar, or directly in the cell, has selected (highlight) 'is a.'</p>
  
  <p>I am interested in getting the selection, 'is a'.</p>
  
  <p>Excel Interop or VBA is acceptable.</p>
  
  <p><img src="https://i.stack.imgur.com/sZHlX.png" alt="enter image description here"></p>
  
  <p><strong>Update:</strong></p>
  
  <p>After incorporating some suggestions, I've arrived at the following:</p>
  
  <pre><code>    public void SelectionToLowerCase(Excel.Worksheet activeWorksheet)
      {
          activeWorksheet.Application.SendKeys("^c", true);
  
          var text = Clipboard.GetText();
          var replacement = text.ToLower();
          Clipboard.SetText(replacement);
  
          activeWorksheet.Application.SendKeys(replacement);
  
          // potentitally, also:
          // activeWorksheet.Application.SendKeys("^v");
      }
  </code></pre>
  
  <p>This, however, does not work quite right either. The value that is returned from <code>Clipboard.GetText()</code> is whatever was in the clipboard before the method was executed. After execution, the clipboard holds the expected value.</p>
  
  <p>My assessment is that Excel's key buffer, which is where the <code>Application.SendKeys()</code> method sends its arguments, does not get processed until control returns to Excel.</p>
  
  <p>Is anyone aware of a way to process Excel's keystroke buffer while executing code? <code>Application.KeyBuffer.Clear()</code> or some such? (That is not an actual method or property)</p>