如何判断文件是否已经打开

bbschat 2002-06-26 02:22:55
例如在excel中已经打开此文件,在VB中如何判断此文件已经被打开?
...全文
72 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lvjack 2002-06-26
  • 打赏
  • 举报
回复
可不可以根据同一目录下是否有临时文件生成判断

access和word都有文件生成而且名字跟打开的文件名有关系,excel好像也有,没太注意
qhzxcz 2002-06-26
  • 打赏
  • 举报
回复
更精确一点:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Sub Command1_Click()
Dim MyXL As Object
Set MyXL = GetObject("C:\My Documents\Book2.xls")

MyXL.Application.Visible = True
MyXL.Parent.Windows(1).Visible = True
Dim hWnd As Long

hWnd = FindWindow("XLMAIN", "Microsoft Excel - Book2.xls")

If hWnd <> 0 Then '0 表示没有 Excel 在运行。
MsgBox "已在运行"
End If
End Sub
qhzxcz 2002-06-26
  • 打赏
  • 举报
回复
主要是使用FindWindow
qhzxcz 2002-06-26
  • 打赏
  • 举报
回复
先在工程中引用"excel8.0",在参考如下代码

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Sub Command1_Click()
Dim MyXL As Object
Set MyXL = GetObject("C:\My Documents\Book2.xls")

MyXL.Application.Visible = True
MyXL.Parent.Windows(1).Visible = True
Dim hWnd As Long

hWnd = FindWindow(vbNullString, "Microsoft Excel - Book2.xls")

If hWnd <> 0 Then '0 表示没有 Excel 在运行。
MsgBox "已在运行"
End If
End Sub

billkay 2002-06-26
  • 打赏
  • 举报
回复
关注
chenyu5188 2002-06-26
  • 打赏
  • 举报
回复
UP…!!!
bbschat 2002-06-26
  • 打赏
  • 举报
回复
up
suntt 2002-06-26
  • 打赏
  • 举报
回复
同意楼上
Sean918 2002-06-26
  • 打赏
  • 举报
回复
使用api函数createfile:
1. 如果文件是以独占方式打开,则系统会自动报提示。
2. 如果不是以独占方式打开,可以尝试修改文件并保存。系统不会给权限保存,所以会报错,这时可以用错误捕获来获得文件处理信息。


=======仅供参考========

7,757

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧