VBA创建日志文件

时间:2016-08-19 10:06:15

标签: excel vba excel-vba

您好,我可以帮助我使用VBA中的代码吗?当我按下按钮“zadat”时,我想从单元格中的文本创建一个日志文件(“C2”和“C3”+日期和时间)谢谢

我的实施代码是:

第1单元

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub zadat()

Dim reg, check As String
Dim i, j, done As Integer
reg = Cells(2, 3).Value
check = Cells(4, 3).Value

If check = "True" Then

    i = 2
    j = 1
    done = 0
    Do While Sheets("data").Cells(i, j) <> ""
        If Sheets("data").Cells(i, j) = reg Then
            vytisteno = ZkontrolovatAVytiskoutSoubor()

            done = Sheets("data").Cells(i, j + 3)
            done = done + 1
            Sheets("data").Cells(i, j + 3) = done
            Exit Do
        End If
        i = i + 1

    Loop
Else
    MsgBox ("Opravit, špatný štítek!!!")
End If

Cells(3, 3) = ""

Cells(3, 3).Select
ActiveWindow.ScrollRow = Cells(1, 1).row


End Sub

第2单元:

Option Explicit
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long

Public Function PrintThisDoc(formname As Long, FileName As String)
On Error Resume Next
Dim x As Long
x = ShellExecute(formname, "Print", FileName, 0&, 0&, 3)
End Function

Public Function ZkontrolovatAVytiskoutSoubor() As Boolean
Dim printThis
Dim strDir As String
Dim strFile As String
strDir = "W:\Etikety\Štítky\Krabice\Testy"
strFile = Range("C2").Value & ".lbe"
If Not FileExists(strDir & "\" & strFile) Then
    MsgBox "soubor neexistuje!"
ZkontrolovatAVytiskoutSoubor = False
Else
printThis = PrintThisDoc(0, strDir & "\" & strFile)
ZkontrolovatAVytiskoutSoubor = True
End If
End Function

Private Function FileExists(fname) As Boolean
    'Returns TRUE if the file exists
    Dim x As String
    x = Dir(fname)
    If x <> "" Then FileExists = True _
       Else FileExists = False
End Function

2 个答案:

答案 0 :(得分:5)

如果您不想使用FSO,可以使用仅使用VBA语句的简单解决方案:OpenPrint #Close

Sub Log2File(Filename As String, Cell1, Cell2)
    Dim f As Integer
    f = FreeFile
    Open Filename For Append Access Write Lock Write As #f
    Print #f, Now, Cell1, Cell2
    Close #f
End Sub

我已将文件名和单元格引用作为sub的参数以用于可重用性目的。我也使用默认(本地)格式,但这可以很容易地更改。 请注意,您不必检查文件是否存在,如果文件不存在,则会创建该文件。

答案 1 :(得分:3)

试试这个。下面的代码将每次创建一个新的日志文件

Public Function LogDetails()
  Dim fso As Object
  Set fso = CreateObject("Scripting.FileSystemObject")

  Dim logFile As Object
  Dim logFilePath As String
  Dim logFileName As String

  'Replace 'TestLog' with your desired file name
  logFileName = "TestLog" & ".txt"
  myFilePath = "C:\Users\..\Desktop\" & logFileName 'Modify the path here

  If fso.FileExists(myFilePath) Then
    Set logFile = fso.OpenTextFile(myFilePath, 8)
  Else
    ' create the file instead
    Set logFile = fso.CreateTextFile(myFilePath, True)
  End If

  logFile.WriteLine "[" & Date & " " & Time & "] " & Worksheet("yoursheetnamehere").Cells(2, 3) & " " &  Worksheet("yoursheetnamehere").Cells(3, 3)

  logFile.Close ' close the file
End Function