0
votes

I am trying to create a countdown timer in word.

While running the code i am getting "Runtime error '70': permission denied" for a line in the code: time = time + TimeValue("00:00:01") in modtimer module.

I have created a userform and a separate module named modtimer, the code is given below:

The word document code: (ThisDocument) to show timer when word document opens

Sub Document_Open()
    UserForm1.time_left.Visible = False
    UserForm1.Label1.Visible = False
    UserForm1.Show
End Sub

The code given in userform is as follows:

Dim g_start As Variant
Dim start As Variant
Dim timeEnd As Variant
Dim g_time As Variant
Dim time As Variant
Dim g_timeEnd As Variant
Dim time_duration As Variant
Dim g_time_duration As Variant
Dim g_End As Boolean

Private Sub btnStart_Click()
     start = Now
     g_start = Format(start, "hh:mm:ss")
     timeEnd = Now + TimeValue("00:30:00")
     g_timeEnd = Format(timeEnd, "hh:mm:ss")
     time_duration = timeEnd - start
     g_time_duration = Format(time_duration, "hh:mm:ss")
     Label1.Visible = True
     time_left.Caption = g_time_duration
     time_left.Visible = True
     btnStart.Visible = False
     time = start + TimeValue("00:00:01")
     time_duration = timeEnd - time
     g_time_duration = Format(time_duration, "hh:mm:ss")
     time_left.Caption = g_time_duration
     modtimer.time_count

End Sub

The module code is as follows:

Sub time_count()
    If time_duration = TimeValue("00:05:00") Then
        MsgBox "Only 5 minutes remaining", vbInformation
    End If
    time = time + TimeValue("00:00:01")
    time_duration = timeEnd - time
    If time_duration = TimeValue("00:00:00") Then
        g_End = True
        End_Exam
    End If

    'If g_End = True Then
    '  End_Exam
    'End If
    g_time_duration = Format(time_duration, "hh:mm:ss")
    time_left.Caption = g_time_duration
    Call time_count

End Sub

Sub End_Exam()
    MsgBox "Examination Time has Expired, Click Ok to Submit", vbCritical
    'Documents.Save False, wdOriginalDocumentFormat
    Documents.Close wdPromptToSaveChanges, wdPromptUser

End Sub

What is the reason behind it? Can anybody help?

2

2 Answers

1
votes

Time is a vba function (and also a vba statement btw).

It is not a good idea to define a variable with the same name.

In your userform, you are overloading the function with your variable, but in the module every time you use Time your are referring to the standard Function (or statement), not the variable from the userform.

By the way, variables in userforms are private by default, so you cannot access the time variable in the userform from the module, you need to specify Public instead of Dim.

Once the variable is Public, you will be able to access it but you will need to use Userform1.time instead of only time.

But it still isn't a good idea to name it time


Additional explanation:

In the module, in the line

time = time + TimeValue("00:00:01")

The first time is the time statement (to change system time)

The second time is the time function (returns current time)

So you are trying to set the system time to the current time + 1 second.

This operation can be done only if you have elevated rights, hence the Runtime error '70': permission denied you get.

0
votes

Time is an VBA property, and it is read-only (it gives simple the actual time). If you need a variable, use a different name.