VBA Access / Excel - 创建日志文件

时间:2015-09-14 22:12:18

标签: vba excel-vba ms-access access-vba ms-access-2010

在MS Access中,可以创建仅显示已修改工作簿的日志文件(或txt文件)

以下代码编辑多个excel工作簿,但在编辑工作簿之前,它首先检查工作簿是否处于读/写模式。如果不是那么它将关闭并打开工作簿,直到读/写处于活动状态。

For Each i In MyArray

xl.Workbooks.Open (i)
'If workbook in read only mode , close and open till read/write is active
Do Until xl.ActiveWorkbook.ReadOnly = False
    xl.ActiveWorkbook.Close (False)
    If GetAttr(i) = vbReadOnly Then _
        SetAttr i, vbNormal
    xl.Workbooks.Open (i)
If xl.ActiveWorkbook.ReadOnly = False Then Exit Do
Loop    'Loop above till read/write active

'''''More code here when workbook read/write mode
Next

1 个答案:

答案 0 :(得分:2)

我假设您要创建一个日志文件并写入您为R / W打开的每个Excel工作簿的名称。这是使用后期绑定的解决方案:

Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.CreateTextFile("c:\myLogFile.txt")

For Each i In MyArray
  xl.Workbooks.Open (i)
  Do Until xl.ActiveWorkbook.ReadOnly = False
    xl.ActiveWorkbook.Close (False)
    If GetAttr(i) = vbReadOnly Then SetAttr i, vbNormal
    xl.Workbooks.Open (i)
    If xl.ActiveWorkbook.ReadOnly = False Then
        ts.WriteLine "opened file: " & xl.ActiveWorkbook.Name
        Exit Do
    End If 
Loop    'Loop above till read/write active

'''''More code here when workbook read/write mode
Next