STARTUP template not stamping registry

J

Janine_ribbonspace

Hi,
Anyone have any ideas how I can alter this code when template resides in
STARTUP to stamp registry with number of times accessed? It assumes AUTONEW
as a template would run but in STARTUP it is accessed not File New.
Can I change AutoNew to be AutoOpen? I don't have VBA help access available
on this machine as it is Technical Preview.
Crude but effective until it resides in STARTUP and is not used as an
autonew template.

Many thanks.

Option Explicit

Public Sub TrialTest()

'Pop up a message and close the document if it has
'been used more than x times or for more than x days:

On Error GoTo Err_AutoNew

Const iMaxRuns As Integer = 1
Const lMaxDays As Long = 1

Dim iTimesRun As Integer
Dim dtFirstRun As Date

Dim bOKToRun As Boolean

Dim sTimesRun As String
Dim sFirstRun As String
Dim sErrorMessage As String

sTimesRun = GetSetting("Word", "MyKeySection", "TimesRun")
sFirstRun = GetSetting("Word", "MyKeySection", "FirstRun")

If IsNumeric(sTimesRun) Then
iTimesRun = CInt(sTimesRun)
Else
iTimesRun = 0
End If

iTimesRun = iTimesRun + 1

If IsDate(sFirstRun) Then
dtFirstRun = CDate(sFirstRun)
Else
dtFirstRun = Now
Call SaveSetting("Word", "MyKeySection", "FirstRun",
CStr(dtFirstRun))
End If

bOKToRun = True

If iTimesRun > iMaxRuns Then
bOKToRun = False
sErrorMessage = "You have used this template " _
& iTimesRun & " times. The maximum limit is " _
& iMaxRuns & " uses. Visit www.com to " _
& "xxxxxxxx latest version."
End If

If DateDiff("d", dtFirstRun, Now) > lMaxDays Then
bOKToRun = False
sErrorMessage = "You have used this template since " _
& dtFirstRun & ". It can only be used for " _
& lMaxDays & " days. Visit www.com to " _
& "xxxxxxxx latest version."
End If

If bOKToRun Then
Call SaveSetting("Word", "MyKeySection", "TimesRun",
CStr(iTimesRun))
Else
MsgBox sErrorMessage
ActiveDocument.Close (wdDoNotSaveChanges)
End If

Exit_AutoNew:

Exit Sub

Err_AutoNew:

MsgBox Err.Description
Resume Exit_AutoNew

End Sub
 
P

Pesach Shelnitz

Hi Janine,

You also need to remove the outer parentheses around the parameters. The
lines with SaveSetting should be written as follows.

SaveSetting "Word", "MyKeySection", "FirstRun", CStr(dtFirstRun)

SaveSetting "Word", "MyKeySection", "TimesRun", CStr(iTimesRun)
 
J

Janine_ribbonspace

Pesach,

I am in 2010 Word on this machine so it behaves oddly at times.

If you run the macro from Editor it stamps the registry. But if it is in
startup or as file new off desktop it won't stamp registry - it does in
2003/2007.

I'll give it a shot and see how it behaves.
 
J

Janine_ribbonspace

Pesach, the brackets are removed it won't stamp the registry at all. Do you
know why?
 
G

Graham Mayor

I don't have access to Word 2010, but in 2007 configured as an add-in in
Word 2007, it certainly stamps the registry, prompts when the limits are
reached and turns off the add-in.

Option Explicit
Public Sub AutoExec()
'Pop up a message and close the document if it has
'been used more than x times or for more than x days:
On Error GoTo Err_AutoNew
Const iMaxRuns As Integer = 1
Const lMaxDays As Long = 1
Dim iTimesRun As Integer
Dim dtFirstRun As Date
Dim bOKToRun As Boolean
Dim sTimesRun As String
Dim sFirstRun As String
Dim sErrorMessage As String
sTimesRun = GetSetting("Word", "MyKeySection", "TimesRun")
sFirstRun = GetSetting("Word", "MyKeySection", "FirstRun")
If IsNumeric(sTimesRun) Then
iTimesRun = CInt(sTimesRun)
Else
iTimesRun = 0
End If
iTimesRun = iTimesRun + 1
If IsDate(sFirstRun) Then
dtFirstRun = CDate(sFirstRun)
Else
dtFirstRun = Now
SaveSetting "Word", "MyKeySection", "FirstRun", CStr(dtFirstRun)
End If
bOKToRun = True
If iTimesRun > iMaxRuns Then
bOKToRun = False
sErrorMessage = "You have used this template " _
& iTimesRun & " times. The maximum limit is " _
& iMaxRuns & " uses. Visit www.com to " _
& "xxxxxxxx latest version."
End If
If DateDiff("d", dtFirstRun, Now) > lMaxDays Then
bOKToRun = False
sErrorMessage = "You have used this template since " _
& dtFirstRun & ". It can only be used for " _
& lMaxDays & " days. Visit www.com to " _
& "xxxxxxxx latest version."
End If
If bOKToRun Then
SaveSetting "Word", "MyKeySection", "TimesRun", CStr(iTimesRun)
Else
MsgBox sErrorMessage
'ActiveDocument.Close (wdDoNotSaveChanges)
End If
Exit_AutoNew:
AddIns("C:\Word 2007 StartupPath\AddinTemplateName.dotm").Installed =
False
Exit Sub
Err_AutoNew:
MsgBox Err.Description
Resume Exit_AutoNew
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
P

Pesach Shelnitz

Hi Janine,

The logic of the macro lets it stamp the registry only one time. You need to
remove the registry values created before it will stamp the registry again.
This logic makes testing difficult. I suggest that you create a simpler macro
for testing.
 
G

Graham Mayor

That should have read

"but configured as follows as an add-in"

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

Janine_ribbonspace

Yes in 2007 it did. I have one machine with 2007/2003 Win XP

This machine has Win 64 Win7 RC1 and 2010 64 TP.

As stupid as it sounds it seems to be the Windows 64 bit that is creating
havoc - no software seems to have made allowances for it even to run.

The addin worked as a standalone File New template concept on 2003/2007. I
moved it into STARTUP on Word 2010 but 64 bit and it just won't stamp
(except if I open VBA and run the macro).
I have deleted the settings in registry. I like this macro I just want it to
work for me again in this particular environment for a community event.

So please bear with me and what sounds stupid is not necessarily so...

I'll do it again on the 2007 32 bit machine later and see what it does there
again. And then run it again 2010 Word 64 Bit machine. It was originally
used in Word 2003 32 bit.

Many thanks for assisting it is very much appreciated.
 
J

Janine_ribbonspace

I did Pesach.

Many thanks for assisting it is very much appreciated. I shall keep at it
and let you know how it turns out.
 
J

Janine_ribbonspace

Pesach and Graham thank you AUTOEXEC should do the trick except I get

"The requested member of the collection does not exist" on opening Word
after put the code and macro in startup .... Not sure why yet.

Janine
 
J

Janine_ribbonspace

Pesach and Graham works again (Graham AUTOEXEC) I forgot to rename it (so
sorry).

Thank you both for time and trouble you are both terrific and always on the
ball...

Janine
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Top