关于FindWindow:请问怎样判断某个Excel文件已经打开?(80分)

  • 主题发起人 主题发起人 wader
  • 开始时间 开始时间
W

wader

Unregistered / Unconfirmed
GUEST, unregistred user!
我用OpenDialog打开一个Excel文件,我想在操作这个Excel文件之前判断这个Excel文件是否已经打开?如果已经打开的话就退出。看了一些文章说可以用FindWindow()完成,但我用下面代码测试总是返回0,请各位指点一下,谢谢!!<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; p_handle: Integer;<br>begin<br>&nbsp; &nbsp;with OpenDialog1 do<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;if Execute = True then<br>&nbsp; &nbsp; &nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p_handle :=FindWindow('EXCEL7',PChar(FileName));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if p_handle&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;showmessage('请先关闭'+FileName);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp;except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //..<br>&nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp;end;<br>end;
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1681850
 
这方法不大合用
 
p_handle :=FindWindow('EXCEL7',PChar(FileName));<br>改为 &nbsp; &nbsp; &nbsp; &nbsp;<br>h := Findwindow(nil,'Microsoft Excel - FileName.xls');<br>FileName是文件名不包括路径<br>
 
搞定!Thanks!Microsoft Excel后面的"-"前后都要有空格!!
 
结束本帖,谢谢两位!
 
后退
顶部