Excel 如何测试.txt文件是否已被任何人打开?

Excel 如何测试.txt文件是否已被任何人打开?,excel,vba,Excel,Vba,我试图测试是否有人已经打开了.txt或.ini文件。我有几个版本的IsFileOpen。这是一张直接取自 我需要通过VBA来完成这项工作。我无法使它在.txt或.ini文件中工作。如何检查txt或ini文件是否已被网络上的某个人打开 编辑:无论txt和ini文件是否打开,都返回false 如果你愿意的话,我正试图在客户端网络上构建一个分布式计算系统。我以前从未这样做过,我希望它尽可能简单,所以我想通过txt文件进行通信。MSMQ看起来不错,但它看起来像一条长长的学习曲线。我已经阅读了stacko

我试图测试是否有人已经打开了.txt或.ini文件。我有几个版本的IsFileOpen。这是一张直接取自

我需要通过VBA来完成这项工作。我无法使它在.txt或.ini文件中工作。如何检查txt或ini文件是否已被网络上的某个人打开

编辑:无论txt和ini文件是否打开,都返回false

如果你愿意的话,我正试图在客户端网络上构建一个分布式计算系统。我以前从未这样做过,我希望它尽可能简单,所以我想通过txt文件进行通信。MSMQ看起来不错,但它看起来像一条长长的学习曲线。我已经阅读了stackoverflow上关于分布式计算的所有文章。

OpenFiles命令使用
NetFileEnum

NetFileEnum函数根据指定的参数返回有关服务器上某些或所有打开文件的信息

它说它也做本地计算机,但Openfiles要求为本地文件设置一个标志。运行GFlags并勾选“维护每种类型的对象列表”

注意记事本打开、读取和关闭文件。因此,记事本没有打开的文件

对于本地计算机,您可以调用
enumwindows
,查看您的文件名是否在任何窗口的标题中。此示例使用
GetWindow
,但文档中说首选
EnumWindows

Public Declare Function GetTopWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
       Private Const GW_CHILD = 5
       Private Const GW_HWNDNEXT = 2
    Sub WindowList()
        Dim hwnd As Long
            hwnd = GetTopWindow(0)
            If hwnd <> 0 Then
                AddChildWindows hwnd, 0
            End If
    End Sub

    Private Function AddChildWindows(ByVal hwndParent As Long, ByVal Level As Long) As String
        Dim gwfnhwnd As Long, X As Long, WT As String, CN As String, Length As Long, hwnd As Long, TID As Long, PID As Long, MN As String, Ret As Long, Parenthwnd As Long
        Static Order As Long
        Static FirstTime As Long
        Parenthwnd = hwndParent
        If Level = 0 Then
                        hwnd = hwndParent
        Else
            hwnd = GetWindow(hwndParent, GW_CHILD)
        End If
        Do While hwnd <> 0
                  WT = Space(512)
                  Length = GetWindowText(hwnd, WT, 508)
                  WT = Left$(WT, Length)
                  If WT = "" Then WT = Chr(171) & "No Window Text " & Err.LastDllError & Chr(187)
                  CN = Space(512)
                  Length = GetClassName(hwnd, CN, 508)
                  CN = Left$(CN, Length)
                  If CN = "" Then CN = "Error=" & Err.LastDllError
                  MsgBox WT & " " & CN
                  hwnd = GetWindow(hwnd, GW_HWNDNEXT)
        Loop
    End Function
公共声明函数GetTopWindow Lib“user32”(ByVal hwnd作为Long)作为Long
公共声明函数getwindowlib“user32”(ByVal hwnd为Long,ByVal wCmd为Long)为Long
公共声明函数GetClassName Lib“user32”别名“GetClassNameA”(ByVal hwnd为Long,ByVal lpClassName为String,ByVal nMaxCount为Long)为Long
公共声明函数GetWindowText Lib“user32”别名“GetWindowTextA”(ByVal hwnd为长,ByVal lpString为字符串,ByVal cch为长)为长
私家侦探GW_CHILD=5
私人建筑GW_HWNDNEXT=2
子窗口列表()
暗淡的hwnd尽可能长
hwnd=GetTopWindow(0)
如果hwnd为0,则
AddChildWindows hwnd,0
如果结束
端接头
私有函数AddChildWindows(ByVal hwndParent为长,ByVal级别为长)为字符串
Dim gwfnhwnd为长、X为长、WT为字符串、CN为字符串、长度为长、hwnd为长、TID为长、PID为长、MN为字符串、Ret为长、Parenthwnd为长
静态顺序尽可能长
第一次一样长
Parenthwnd=hwndParent
如果级别=0,则
hwnd=hwndParent
其他的
hwnd=GetWindow(hwndParent,GW_CHILD)
如果结束
当hwnd 0时执行
WT=空间(512)
长度=GetWindowText(hwnd、WT、508)
WT=左$(WT,长度)
如果WT=”“,则WT=Chr(171)&“无窗口文本”&Err.LastDllError&Chr(187)
CN=空间(512)
长度=GetClassName(hwnd,CN,508)
CN=左$(CN,长度)
如果CN=”“,则CN=“Error=”&Err.LastDllError
MsgBox WT&&&CN
hwnd=GetWindow(hwnd,GW\U HWNDNEXT)
环
端函数

COM可以跨计算机和进程进行通信。只需使用类模块在进程之间构建接口。

一种方法是尝试使用相同的名称重命名文件:

Public Function IsFileLocked(file_path As String) As Boolean
  Dim num As Long

  On Error Resume Next
  Name file_path As file_path
  num = Err.Number
  On Error GoTo 0

  If num <> 0 And num <> 75 Then Error num
  IsFileLocked = num <> 0
End Function
公共函数IsFileLocked(文件路径为字符串)为布尔值
Dim num尽可能长
出错时继续下一步
将文件路径命名为文件路径
num=错误编号
错误转到0
如果为num 0和num 75,则为Error num
IsFileLocked=num 0
端函数

该示例有什么不适用?你是如何测试它的?请扩展到“不工作”。如果分布式计算系统的用户告诉您它“不工作”,您将如何修复它?您是否得到错误或意外的结果?对于初学者来说,该函数在错误时有
,在顶部继续下一步
,这是个坏消息。该函数确实有很多关于“坏文件的结果”的复杂代码@Comintern我添加了一个编辑。你确定打开文件的进程正在锁定它吗?大多数文本实用程序不会锁定文件,除非它们正在主动写入文件。也就是说,您考虑过内存映射文件吗?从这个问题来看,您似乎在寻找一种进程间通信的形式。内存映射文件的使用方式与共享内存类似。我在一个论坛上发布了一些(非常陈旧和不完整的)代码。我不确定这对OP有什么影响,但是:
Public Declare Function GetTopWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
       Private Const GW_CHILD = 5
       Private Const GW_HWNDNEXT = 2
    Sub WindowList()
        Dim hwnd As Long
            hwnd = GetTopWindow(0)
            If hwnd <> 0 Then
                AddChildWindows hwnd, 0
            End If
    End Sub

    Private Function AddChildWindows(ByVal hwndParent As Long, ByVal Level As Long) As String
        Dim gwfnhwnd As Long, X As Long, WT As String, CN As String, Length As Long, hwnd As Long, TID As Long, PID As Long, MN As String, Ret As Long, Parenthwnd As Long
        Static Order As Long
        Static FirstTime As Long
        Parenthwnd = hwndParent
        If Level = 0 Then
                        hwnd = hwndParent
        Else
            hwnd = GetWindow(hwndParent, GW_CHILD)
        End If
        Do While hwnd <> 0
                  WT = Space(512)
                  Length = GetWindowText(hwnd, WT, 508)
                  WT = Left$(WT, Length)
                  If WT = "" Then WT = Chr(171) & "No Window Text " & Err.LastDllError & Chr(187)
                  CN = Space(512)
                  Length = GetClassName(hwnd, CN, 508)
                  CN = Left$(CN, Length)
                  If CN = "" Then CN = "Error=" & Err.LastDllError
                  MsgBox WT & " " & CN
                  hwnd = GetWindow(hwnd, GW_HWNDNEXT)
        Loop
    End Function
Public Function IsFileLocked(file_path As String) As Boolean
  Dim num As Long

  On Error Resume Next
  Name file_path As file_path
  num = Err.Number
  On Error GoTo 0

  If num <> 0 And num <> 75 Then Error num
  IsFileLocked = num <> 0
End Function