• Launch time (VBA/Word/20003/SP3)

    Author
    Topic
    #454523

    I have been looking — unsuccessfully — for a method to determine when the current session of Word was launched. Is it possible?

    Viewing 1 reply thread
    Author
    Replies
    • #1128527

      You could have an add-in set a global variable in an AutoExec macro.
      Or create a small text file on the user’s hard disk; you can then inspect the FileDateTime of this file.
      Or you could look at the FileDateTime of the ~$Normal.dot file in the user templates folder. But if Word crashes, this file isn’t deleted, so I don’t know whether its FileDateTime is always dependable.

      • #1128595

        Thank you Hans.
        Of course, your response begged another question, but since it had nothing to do with VBA, I have posted it here.

    • #1128606

      Try this:

      Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
      Function ProcStartTime() As Date
         Dim objWMIService As Object, objProcess As Object, colProcess As Object
         Dim strComputer As String, strTime As String
         Dim colOutput As Collection
         Dim lngIndex As Long
         Dim lngProcId As Long
         ' local machine
         strComputer = "."
         
         lngProcId = GetCurrentProcessId
         
         Set objWMIService = GetObject("winmgmts:" _
              & "{impersonationLevel=impersonate}!" _
              & strComputer & "rootcimv2")
              
         ' Note: can't use 'LIKE' keyword in Win 2000;
         ' can in XP, 2003 and, presumably, Vista
         Set colProcess = objWMIService.ExecQuery _
                  ("Select * from Win32_Process Where ProcessID = " & lngProcId)
         ' check it's running at all!
         For Each objProcess In colProcess
         strTime = objProcess.properties_("CreationDate")
         Next objProcess
         ProcStartTime = DateSerial(Left(strTime, 4), Mid(strTime, 5, 2), Mid(strTime, 7, 2)) _
                  + TimeSerial(Mid(strTime, 9, 2), Mid(strTime, 11, 2), Mid(strTime, 13, 2))
      End Function
      
      • #1128646

        Hi Rory
        I have tested this on Word 2003 and 2007. I cannot find a way to fool your code. Superb! thankyou

      • #1128733

        >post 737,202
        I disagree with “W.D. (Don) “. (grin)
        “Superb” is not the right word.
        Nor “Thank You” the right icon.
        trophy for icons.
        Words fail me.

    Viewing 1 reply thread
    Reply To: Launch time (VBA/Word/20003/SP3)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: