开发者

How to send text to Winword in C#?

开发者 https://www.devze.com 2023-04-03 14:35 出处:网络
[DllImport(\"user32.dll\", EntryPoint = \"FindWindowEx\")] public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
private void button1_Click(object sender, EventArgs e)
{
    Process [] words=Process.GetProcessesByName("winword");
    if(words.Length==0)return;            
    if (words[0] != null)
    {
        IntPtr child= FindWindowEx(words[0].MainWindowHandle, new IntPtr(0), "Edit", null);
        SendMessage(child, 0x000C, 0, textBox1.Text);
    }
}

It is not working for Microsoft word..is there an开发者_StackOverflowy other way?


You can't use this method to poke text into Word. It uses custom controls and just won't yield to this approach. Instead you should use UIAutomation or Office PIA.

0

精彩评论

暂无评论...
验证码 换一张
取 消