Can I setup a timer in VBA

Discussion in 'AutoCAD' started by Stanley, Jan 19, 2005.

  1. Stanley

    Stanley Guest

    Hello all,

    Anyone know how to create a timer in VBA to execute codes periodically.

    Best regards.
     
    Stanley, Jan 19, 2005
    #1
  2. Stanley

    ClementZheng Guest

    ClementZheng, Jan 19, 2005
    #2
  3. Stanley

    Stanley Guest

    Hello ClementZheng,

    Thanks for your advice. I've done a search. There are 2 methods available.

    1. Use a Timer Control dll / ocx. But I can't do this
    because its hard to distribute the control to other machine.

    2. Use Windows SetTimer API. I've tried this method.

    But it cause serious problem, after a few trial run it cause AutoCad to
    terminate without any warning.

    Will you give a robust method to do this?

    Regards.
     
    Stanley, Jan 20, 2005
    #3
  4. Stanley

    antmjr Guest

    perhaps it's a stupid suggestion; why not to call your_function every, say,
    1000 commands with some event like AcadDocument_EndCommand and a counter, or
    everytime you have just saved with AcadDocument_EndSave (you can set
    Automatic Save programmatically with .AutoSaveInterval)
     
    antmjr, Jan 20, 2005
    #4
  5. Stanley

    ClementZheng Guest

    Stanley,
    If you like, you might also register an ActiveX control or DLL programmatically.


    Programmatically register an ActiveX control or DLL

    All ActiveX DLL or OCX export two functions: DllRegisterServer and DllUnregisterServer. They are used to register and unregister the ActiveX in the Windows registry, and are usually invoked from regsvr32.exe at registration time.
    However, you can register and unregister these files programmatically whenever you want to, provided that you know the name of the DLL or OCX at compile time. All you have to do is prepare two aliased functions, as in the following example, that shows how to register and unregister the COMCTL32.OCX file:

    'function to call to register the ActiveX
    Private Declare Function RegComCtl32 Lib "COMCTL32.OCX" Alias _
    "DllRegisterServer" () As Long

    ' function to call to unregister the ActiveX
    Private Declare Function UnRegComCtl32 Lib "COMCTL32.OCX" Alias _
    "DllUnregisterServer" () As Long
    Const ERROR_SUCCESS = &H0

    Note that the two functions work only if the DLL is in the system path or in the current directory. Therefore, if you want to register a DLL located elsewhere on your hard disk, you must use ChDrive and ChDir commands to make that directory the current one. For example, say that the you have a Test.DLL file in the C:\MyApp directory. Here's the code you need to register it:

    Private Declare Function RegisterTestDLL Lib "Test.Dll" Alias _
    "DllRegisterServer" () As Long
    Dim retCode As Long

    On Error Resume Next

    ' move to the DLL's directory
    ChDrive "C:"
    ChDir "C:\MyApp"
    ' register the DLL
    retCode = RegisterTestDLL()

    If Err <> 0 Then
    ' probably the DLL isn't there
    MsgBox "Unable to find the Test.Dll file"
    ElseIf retCode <> ERROR_SUCCESS Then
    ' the registration run and failed
    MsgBox "Registration failed"
    End If
     
    ClementZheng, Jan 20, 2005
    #5
  6. Not an option since this is vba and the Timer control is not legally
    redistributable - that's why he is having problems [re-read your EULA].


    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Jan 20, 2005
    #6
  7. Stanley

    ClementZheng Guest

    Of course not the Microsoft TIMER CONTROL .
    He might also write such a control by himself or find some 3rd party free control.
     
    ClementZheng, Jan 20, 2005
    #7
  8. Stanley

    Stanley Guest

    Hello all,

    Thanks for giving so much advices.

    At least I've learn something from that.

    Regards.
     
    Stanley, Jan 21, 2005
    #8
  9. Stanley

    ljb Guest

    control.

    This timer ocx is a few years old but should still work. Its free and was
    intended to replace Microsoft's Timer Control.

    http://ccrp.mvps.org/index.html?controls/ccrptimer6.htm
     
    ljb, Jan 21, 2005
    #9
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.