Advertisement
Guest User

Paste Text to active Word window

a guest
Jan 10th, 2014
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.76 KB | None | 0 0
  1. Clipboard.SetText(text);
  2. ...
  3. var foregroundWindow = GetForegroundWindow();
  4.  
  5. if (GetWindowText(foregroundWindow, aString, 256) > 0)
  6. {
  7.     appName = aString.ToString();
  8.     if (appName.IndexOf("Word") > 0)
  9.     {
  10.         Microsoft.Office.Interop.Word.Application wdApp = null;
  11.         wdApp = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
  12.         if (wdApp != null)
  13.         {
  14.             Microsoft.Office.Interop.Word.Selection currentSelection = wdApp.Selection;
  15.  
  16.             // Store the user's current Overtype selection
  17.             bool userOvertype = wdApp.Options.Overtype;
  18.  
  19.             // Make sure Overtype is turned off.
  20.             if (wdApp.Options.Overtype)
  21.             {
  22.                 wdApp.Options.Overtype = false;
  23.             }
  24.  
  25.             // Test to see if selection is an insertion point.
  26.             if (currentSelection.Type == Microsoft.Office.Interop.Word.WdSelectionType.wdSelectionIP)
  27.             {
  28.                 currentSelection.TypeText(aString);
  29.             }
  30.             else
  31.             {
  32.                 if (currentSelection.Type == Microsoft.Office.Interop.Word.WdSelectionType.wdSelectionNormal)
  33.                 {
  34.                     // Move to start of selection.
  35.                     if (wdApp.Options.ReplaceSelection)
  36.                     {
  37.                         object direction = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseStart;
  38.                         currentSelection.Collapse(ref direction);
  39.                     }
  40.                     currentSelection.TypeText(aString);
  41.                 }
  42.             }
  43.  
  44.             // Restore the user's Overtype selection
  45.             wdApp.Options.Overtype = userOvertype;
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement