views:

931

answers:

3

I have the following code which works fine in notepad but not in WORD!!

 [DllImport("user32.dll")]
 public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

 [DllImport("user32.dll")]
 public static extern IntPtr GetForegroundWindow();

 [DllImport("user32.dll", SetLastError = true)]
 public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

 [DllImport("kernel32.dll")]
 public static extern uint GetCurrentThreadId();

 [DllImport("user32.dll")]
 public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);

 [DllImport("user32.dll")]
 public static extern IntPtr GetFocus();

 [DllImport("user32.dll")]
 public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);

 // second overload of SendMessage
 [DllImport("user32.dll")]
 public static extern int SendMessage(IntPtr hWnd, uint Msg, out int wParam, out int lParam);

 [DllImport("user32.dll")]
 public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

 public const uint WM_GETTEXT = 0x0D;
 public const uint WM_GETTEXTLENGTH = 0x0E;
 public const uint EM_GETSEL = 0xB0;

        IntPtr hWnd = WinUser.GetForegroundWindow();
        uint processId;
        uint activeThreadId = WinUser.GetWindowThreadProcessId(hWnd, out processId);
        uint currentThreadId = WinUser.GetCurrentThreadId();

        WinUser.AttachThreadInput(activeThreadId, currentThreadId, true);
        IntPtr focusedHandle = WinUser.GetFocus();
        WinUser.AttachThreadInput(activeThreadId, currentThreadId, false);

        int len = WinUser.SendMessage(focusedHandle, WinUser.WM_GETTEXTLENGTH, 0, null);
        StringBuilder sb = new StringBuilder(len);
        int numChars = WinUser.SendMessage(focusedHandle, WinUser.WM_GETTEXT, len + 1, sb);

        int start, next;
        string selectedText = "";
        WinUser.SendMessage(focusedHandle, WinUser.EM_GETSEL, out start, out next);
        try
        {
            selectedText = sb.ToString().Substring(start, next - start);
        }

unfortunately the above returns "{Microsoft Word Document}" when selecting a text in WORD or any "richtextbox". How does CTRL+C do it?

NOTE: This works fine in notepad or on any simple text editor.

A: 

I think you should look into this tutorial on C# Clipboard Copy and Pasting. Using Copy paste in C# is not actualy as hard as you might think.

Copy

Clipboard.SetText(txtClipboard.Text);

Paste

txtClipboard.Text = Clipboard.GetText();

Check the Above link for more information and examples. You should also look at the MSDN page for Clipboard.

Filip Ekberg
ya using Clipboard is easy, but am looking to copy the text without the use of CTRL+C, am trying to simulate it - not use it. Therefore the clipboard would be empty.
Anand
well priyank's comment helped cause in that link they send a message to simulate ctrc+c and then i could use the clipboard object like u suggested.. thanks
Anand
priyanks comment worked fine, but i completely forgot my main need in doing this was not to save the data in the windows clipboard. that is the reason i needed to simulate Ctrl+c and not use it. :)Do u have an idea where iam going wrong in my code above.
Anand
And this was downvoted why?
Filip Ekberg
exactly what i want to know??
Anand
+3  A: 

Is this you are looking for: http://stackoverflow.com/questions/235972/copy-and-modify-selected-text-in-different-application

Priyank Bolia
this could be! thanks let me check it out. in fact i got the base code from that site
Anand
Hi, this worked fine, but i completely forgot my main need in doing this was not to save the data in the windows clipboard. that is the reason i needed to simulate Ctrl+c and not use it. :)
Anand
copy the clipboard contents, simulate Ctrl+c read again, and restore the original.
Priyank Bolia
A: 

I'm pretty sure Word is not going to respond to EM_ messages. These messages are specific to Windows edit controls; it just to happens that Notepad uses a plain edit control for its text.

You might be able to achieve what you want using the Word COM automation interfaces. There's no 100% guaranteed means to retrieve text from another application.

Edit: I'm no expert on this, but you might have more success with the accessibility APIs. It's possible for an app (such as Notepad or Word) to expose a set of objects representing its UI that you can query from your app.

Tim Robinson
here's what iam trying:from the active window i need to copy the text that has been selected.then parse that text to do something in my service. yes you are right this works fine on any plain edit control.
Anand
OK, either the Word automation API or the more general-purpose accessibility APIs would be the way to go.
Tim Robinson