Thread: [Solved] Adding current time to VBA
View Single Post
 
Old 01-13-2018, 12:31 AM
blackroz blackroz is offline Windows XP Office 2007
Novice
 
Join Date: Nov 2015
Posts: 5
blackroz is on a distinguished road
Exclamation Adding current time to VBA

I have the following which works fine but I'm trying to add hour,minute and second to dd MMM yyyy but I don't know how:

Code:
Sub SaveNumberedVersion()
            'Graham Mayor 15 Jan 2006
            'Completely Revised 18 January 2011
            'to store count in a document variable
            'and improve document type handling
            Dim strVer As String
            Dim strDate As String
            Dim strPath As String
            Dim strFile As String
            Dim oVars As Variables
            Dim strFileType As WdDocumentType
            Dim strVersionName As String
            Dim intPos As Long
            Dim sExt As String
            Set oVars = ActiveDocument.Variables
            strDate = Format((Date), "dd MMM yyyy")
            With ActiveDocument
            On Error GoTo CancelledByUser
            If Len(.Path) = 0 Then
            'No path means document not saved
            .Save 'So save it
            End If
            strPath = .Path 'Get path
            strFile = .Name 'Get document name
            End With
            intPos = InStr(strFile, " - ") 'Mark the version number
            sExt = Right(strFile, Len(strFile) - InStrRev(strFile, ".do"))
            If intPos = 0 Then
            'No version number
            intPos = InStrRev(strFile, ".do") 'Mark the extension instead
            End If
            strFile = Left(strFile, intPos - 1) 'Strip the extension or version number
            Select Case LCase(sExt)
            'Determine file type by extension
            Case Is = "doc"
            strFileType = 0
            Case Is = "docx"
            strFileType = 12
            Case Is = "docm"
            strFileType = 13
            Case Is = "dot"
            strFileType = 1
            Case Is = "dotx"
            strFileType = 14
            Case Is = "dotm"
            strFileType = 15
            End Select
            Start: 'Get Registry Data
            On Error Resume Next 'No entry in registry will flag an error
            strVer = oVars("varVersion").Value
            If strVer = "" Then
            'Variable does not exist
            oVars("VarVersion").Value = "0" 'So create it
            GoTo Start:
            End If
            On Error GoTo 0
            strVer = Val(strVer) + 1 'Increment number
            oVars("varVersion").Value = strVer
            'Define the new version filename
            strVersionName = strPath & "\" & strFile & " - " & strDate & _
            " - Version " & Format(Val(strVer), "00#") _
            & Chr(46) & sExt
            'and save a copy of the file with that name
            ActiveDocument.SaveAs strVersionName, strFileType
            Exit Sub
            CancelledByUser: 'Error handler
            MsgBox "Cancelled By User", , "Operation Cancelled"
            End Sub
Reply With Quote