如何在程序里向Word发送字符 ( 积分: 200 )

  • 主题发起人 主题发起人 小虫77
  • 开始时间 开始时间

小虫77

Unregistered / Unconfirmed
GUEST, unregistred user!
我试过这样,但不行,请各位高手指点<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;Wnd,AimWindow : HWND;<br> &nbsp;St,WindowCaption : String;<br>begin<br> &nbsp;WindowCaption := Edit2.Text;<br> &nbsp;St := Edit1.Text;<br> &nbsp;AimWindow := FindWindow('OpusApp',nil);<br> &nbsp;if AimWindow &amp;lt;&amp;gt; 0 then<br> &nbsp;begin<br> &nbsp; &nbsp;Wnd := GetWindow(AimWindow, GW_CHILD);<br> &nbsp; &nbsp;if Wnd &amp;lt;&amp;gt; 0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;SendMessage(Wnd, WM_SETTEXT, 256, LongInt(PChar(st)));<br> &nbsp; &nbsp; &nbsp;SetForegroundWindow(wnd);<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>end;
 
我试过这样,但不行,请各位高手指点<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;Wnd,AimWindow : HWND;<br> &nbsp;St,WindowCaption : String;<br>begin<br> &nbsp;WindowCaption := Edit2.Text;<br> &nbsp;St := Edit1.Text;<br> &nbsp;AimWindow := FindWindow('OpusApp',nil);<br> &nbsp;if AimWindow &amp;lt;&amp;gt; 0 then<br> &nbsp;begin<br> &nbsp; &nbsp;Wnd := GetWindow(AimWindow, GW_CHILD);<br> &nbsp; &nbsp;if Wnd &amp;lt;&amp;gt; 0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;SendMessage(Wnd, WM_SETTEXT, 256, LongInt(PChar(st)));<br> &nbsp; &nbsp; &nbsp;SetForegroundWindow(wnd);<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>end;
 
没有人知道这一个问题吗?还是分太少了?
 
你还不如自己打开那个WORD文档wApp := CreateOleObject('Word.Application');<br>,然后再往里面写内容简单一些。<br>
 
你参考下如下代码:<br><br>procedure TForm1.SendKeys(sSend:string);<br>var<br> &nbsp; &nbsp;i:integer;<br> &nbsp; &nbsp;Sstr:string;<br> &nbsp; &nbsp;focushld,windowhld:hwnd;<br> &nbsp; &nbsp;threadld:dword;<br> &nbsp; &nbsp;ch: byte;<br>begin<br> &nbsp;windowhld:=GetForegroundWindow;<br> &nbsp;threadld:=GetWindowThreadProcessId(Windowhld,nil);<br> &nbsp;AttachThreadInput(GetCurrentThreadId,threadld,true);<br> &nbsp;Focushld:=getfocus;<br> &nbsp;getcursorpos(p); //查鼠标坐标<br> &nbsp;Focushld:=WindowFromPoint(p); //返回句柄<br> &nbsp;AttachThreadInput(GetCurrentThreadId,threadld,false);<br> &nbsp;if (focushld=0) or<br> &nbsp; &nbsp; (focushld=Self.Memo1.Handle) or<br> &nbsp; &nbsp; (focushld=Self.Edit1.Handle) or<br> &nbsp; &nbsp; (focushld=Self.Edit2.Handle) or<br> &nbsp; &nbsp; (focushld=SpinEdit1.Handle) &nbsp;then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; Exit;<br> &nbsp; &nbsp;end;<br> &nbsp;i := 1;<br> &nbsp;while i &amp;lt;= Length(sSend) do<br> &nbsp;begin<br> &nbsp; &nbsp;ch := byte(sSend);<br> &nbsp; &nbsp;if Windows.IsDBCSLeadByte(ch) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; Inc(i);<br> &nbsp; &nbsp; &nbsp; &nbsp; SendMessage(focushld, WM_IME_CHAR, MakeWord(byte(sSend), ch), 0);<br> &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; SendMessage(focushld, WM_IME_CHAR, word(ch), 0);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;Inc(i);<br> &nbsp;end;<br> &nbsp;SendMessage(focushld, WM_IME_CHAR, word(13), 0);<br>end;<br><br>//定时器定时发送字符<br>procedure TForm1.Timer1Timer(Sender: TObject);<br>begin<br> &nbsp; SendKeys('ABCD1234大话西游');<br>end;<br>
 
直接用ole不行么?<br>肯定没问题的。
 
谢谢各位的热心,等我试试如果可行,那分就是您们的了.
 
to: jianguobu<br>你的方法基本解决了问题,但还有几个地方有点问题(我自己已经解决了),谢谢各位
 

Similar threads

后退
顶部