VBA Track file usage

后端 未结 2 839
礼貌的吻别
礼貌的吻别 2021-01-23 02:40

I currently have an excel file that produces client statements. I need to track who has run their statements. Currently, whenever statements are produced I have a macro that sen

相关标签:
2条回答
  • 2021-01-23 03:05

    If there is a location on your network that everyone has access to you can write a log file. Most likely a spot on the Sharepoint server.

    Something like this called from the code that is currently sending out the email.

    In you VBA IDE go to the tools menu and select references. Select "Microsoft scripting runtime"

    Private Sub LogUsage()
    
        Dim ts As TextStream
        Dim fs As FileSystemObject
        Dim strLogFile As String
    
        strLogFile = "\\servername\sharename\log\Usage.txt"
    
        'Check if the file exists, if it does, open it, if it doesn't create it
        Set fs = New FileSystemObject
        If fs.FileExists(strLogFile) = True Then
            Set ts = fs.OpenTextFile(strLogFile, ForAppending)
        Else
            Set ts = fs.CreateTextFile(strLogFile, True, False)
        End If
    
        'Log your entry
        ts.WriteLine "Used by " & Environ$("Username") & " at " & Now & " on computer " & Environ$("Computername")
    
         'Clean up
         ts.Close: Set ts = Nothing
         Set fs = Nothing
    
     End Sub
    
    0 讨论(0)
  • 2021-01-23 03:14

    I'd use a shared database, like SQL server or Access on a network share, rather than an e-mail. It's easier to work with than separate e-mails.

    If you must use e-mail, you can use a CDO object in your Excel macro, but your users must have access to an SMTP server on your network (usually an Exchange server works for this; look at your Outlook settings and see what server it's connected to). Generally this is not a problem if everyone has access to the same LAN resources.

    Add a reference in the VBA editor to Microsoft CDO for Windows 2000 Library (Tools->References in VBA. Don't worry about the "Windows 2000"; it should be available on your system).

    Example code

    Dim iMsg As CDO.Message
    Dim iConf As CDO.Configuration
    Dim Flds As ADODB.Fields
    
    Set iMsg = New CDO.Message
    Set iConf = New CDO.Configuration
    
    Set Flds = iConf.Fields
    
    With Flds
        .Item(cdoSendUsingMethod) = cdoSendUsingPort
        'Put the address of your SMTP server here
        .Item(cdoSMTPServer) = "smtp.example.com"
        .Item(cdoSMTPConnectionTimeout) = 10
        .Item(cdoSMTPAuthenticate) = cdoBasic
        .Item(cdoSendUserName) = "Username To Authenticate SMTP Server With"
        .Item(cdoSendPassword) = "Password To Authenticate SMTP Server With"
        .Item(cdoURLGetLatestVersion) = True
        .Update
    End With
    
    With iMsg
        Set .Configuration = iConf
        .From = "from@example.com"
        .ReplyTo = "replyto@example.com"
        .MimeFormatted = False
        .AutoGenerateTextBody = False
        .To = "to@example.com"
        .CC = "cc@example.com"
        .BCC = "bcc@example.com"
        .Subject = "Subject of Email"
        .HTMLBody = "<body>HTML text to send</body>"
    
        'If you need to add attachments
        .AddAttachment "C:\Local\Path\To\Attachment.xlsx"
    
        .Send
    End With
    
    0 讨论(0)
提交回复
热议问题