0

I am not coding in .Net but am using System.Windows.Clipboard from the PresentationCore assembly to access the system clipboard to paste information copied in Excel.

When Excel copies to the clipboard I can retrieve it as text with System`Windows`Clipboard`GetText[]. This text follows the TSV file format which I can parse and consume in my code (Wolfram Language).

Is there a way that I can query System.Windows.Clipboard to determine if Excel is the application that pasted its current data into it?

I would like to restrict the paste operation in my code to only evaluate if the data on clipboard is from Excel. I looked at the online MSDN documentation page but did not notice a method that would give this information.

Edmund
  • 488
  • 4
  • 21

2 Answers2

1

You should be able to use GetText(TextDataFormat.CommaSeparatedValue) instead to determine whether the clipboard contents are actually CSV. That way you don't have to care at all whether Excel copied it, or Google Docs, or OpenOffice Calc.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • Is there a `TabSeparatedValue` enum? I did not notice one on the `TextDataFormat` MSDN page but there are sometimes tricks. My concern is that paste can include text cells and these could have commas in them so I worry that these will create extra columns. But a quick test seems to indicate that it might be handling this okay. But I am skeptical. – Edmund Nov 21 '17 at 17:34
  • CSV has provisions for quoting and I'd be surprised if that wasn't used for cells with the separator or a line break in them. In general Mathematica shouldn't care whether the separator is a comma or a tab, I guess. It's just that there is a predefined and supported way of getting CSV from the clipboard, but not for TSV, so CSV should be easier here. – Joey Nov 22 '17 at 09:09
  • Seems to be cooperating with different types of Excel copied data. Thanks. – Edmund Nov 22 '17 at 12:59
1

System.Windows.Clipboard does not track the source.

You can get around this by using Form.ActiveControl to see if Excel is active, and then listen for a CTRL+C command or a copy from the context menu. So instead of looking for the source of copied data, you're watching Excel to see if any data is being copied.

If you don't care the source and just want TSV's, you can parse the copied data to see if it is a TSV and handle it appropriately.

JRodge01
  • 280
  • 1
  • 7
  • Monitoring Excel is a bit much as I am not coding in .Net (using Wolfram Language) and don't want to go too deep into it. I currently try parsing whatever is in the clipboard as TSV but am seeking to be more disciplined about it. Thanks for your answer. (+1) – Edmund Nov 21 '17 at 17:20
  • If you know the type of values you're expecting, you can start to parse the values then break out of parsing if it doesn't match your schema after X characters. – JRodge01 Nov 21 '17 at 17:42
  • A lot of applications do however put content in the clipboard in their own internal formats which contain more data than the classic exposed text (like, for excel, cell properties etc). If you can figure out what the clipboard ID string of that internal object is, I guess you can track it. – Nyerguds Jun 13 '18 at 20:11