0

I am currently using System.Windows.Clipboard to: set some text, call SendInput with control+V, finally restoring the previous clipboard.

This works in most applications, though in Microsoft's Office applications the send input is causing the previous clipboard to be restored. I am assuming it is something to do with a delay in the SendInput method, so I added a wait before restoring and resolved the issue for most users, but Outlook for a user is still restoring the previous clipboard.

is there a better way of doing it than below?

 try
    {
      //method that uses GetDataObject and stores it to be restored
      _clipboard.Acquire();

      try
         {
            _clipboard.SetText(ClipboardText);

            //a wrapper for SendInput()
            SendInputWrapper.SendKeyCombination(new KeyCode(VirtualKeyShort.Control),
                                        new KeyCode(VirtualKeyShort.KeyV));
         }
         finally
         {
          //without this pause word and outlook would paste the original clipboard
          await Task.Delay(TimeSpan.FromMilliseconds(200));
          
          //wrapper method for Clipboard.SetDataObject() using the above stored 
            clipboard
          _clipboard.Restore();
          }
     }
     catch (Exception ex)
     {
       _log.Error("Error caught pasting text", ex);
     }
  • What problem are you trying to solve? – IInspectable Feb 18 '22 at 17:44
  • @IInspectable as in the second paragraph: "in Microsoft's Office applications the send input is causing the previous clipboard to be restored" it is not sending the inserted clipboard. – David_Strachan Feb 21 '22 at 09:37
  • The clipboard belongs to the user. Whenever someone tries to use the clipboard as some sort of inter-process communication channel, the first question that would need to be asked is: Is this really the best solution to the problem? Since we still don't know what the *problem* is, that question cannot be answered. – IInspectable Feb 21 '22 at 11:28
  • I am working on a factory to operate the different process and handle them uniquely. With a potential injection of https://stackoverflow.com/questions/4539187/insert-text-into-the-textbox-of-another-application/4539229#4539229 Though my question still remained relevant, we are discussing MS Office and their clipboard buffer having a delay, and ways to accommodate that delay. If that is no longer sending the paste command and using interop, that is possible. Though for this question I'd like peoples confirmation on MS Office – David_Strachan Feb 21 '22 at 15:26
  • Still unclear why you are using the clipboard at all. Not knowing the ***problem*** you are trying to solve, I'm going to guess that [UI Automation](https://learn.microsoft.com/en-us/windows/win32/winauto/entry-uiauto-win32) is a better solution. – IInspectable Feb 21 '22 at 17:08

1 Answers1

0

Though it does not answer my direct question to Offices Clipboard buffer, the solution I have which now works is using Word.Interop:

        var currentSelection = _word.Selection;

        // Store the user's current Overtype selection
        var userOvertype = _word.Options.Overtype;

        // Make sure Overtype is turned off.
        if (_word.Options.Overtype) _word.Options.Overtype = false;

        // Test to see if selection is an insertion point.
        if (currentSelection.Type == WdSelectionType.wdSelectionIP)
            currentSelection.TypeText(text);
        else if (currentSelection.Type == WdSelectionType.wdSelectionNormal)
        {
            // Move to start of selection.
            if (_word.Options.ReplaceSelection)
            {
                object direction = WdCollapseDirection.wdCollapseStart;
                currentSelection.Collapse(ref direction);
            }

            currentSelection.TypeText(text);
        }

        // Restore the user's Overtype selection
        _word.Options.Overtype = userOvertype;