Can't reach code from global template

G

GrodanBoll

Hi

I have trouble reaching the code in a global template.
I get the code to execute but then I get an error.

When I use the file test.dot to create a new dokument the message box appears as it should but when I tries to close it via the Ok button I get an error message:
[error]
Run-tiem error '91':
Object variable or With block variable not set
[/error]

When I press Debug it marks the line "msgBoxen = msgBox("Hello", vbCritical)" with yellow.

In a module in code.dot (the global template)
Code:
Public Sub TestMsg()
Dim msgBoxen As Variable
msgBoxen = msgBox("Hello", vbCritical)
End Sub

Test.dot:
Code:
Private Sub Document_New()
TestMsg
End Sub

What have I done wrong?

Thanks
Grodan
 
J

Jean-Guy Marcil

Hi Grodan,

Try this instead:
'_______________________________________
Public Sub TestMsg()
Dim msgBoxen As Variant
msgBoxen = MsgBox("Hello", vbCritical)
End Sub
'_______________________________________

Because "Dim" is always used to decalred a variable, using
Dim msgBoxen As Variable
is like saying: "I declare this vehicle as a vehicle" instead of something
like "I declare this vehicle as a 4WD" or "I declare this vehicle as a
mini-van"...

Because you declare "msgBoxen" as a "Variable" (which actually declares the
variable as being nothing, I think), you force "msgBoxen" to be something
the MsgBox function cannnot deal with when it tries to return an Integer
when you click on the OK button (vbOK = 1). You could also use:
Dim msgBoxen As Integer

HTH
--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org


GrodanBoll said:
Hi

I have trouble reaching the code in a global template.
I get the code to execute but then I get an error.

When I use the file test.dot to create a new dokument the message box
appears as it should but when I tries to close it via the Ok button I get an
error message:
[error]
Run-tiem error '91':
Object variable or With block variable not set
[/error]

When I press Debug it marks the line "msgBoxen = msgBox("Hello", vbCritical)" with yellow.

In a module in code.dot (the global template)
Code:
Public Sub TestMsg()
Dim msgBoxen As Variable
msgBoxen = msgBox("Hello", vbCritical)
End Sub

Test.dot:
Code:
Private Sub Document_New()
TestMsg
End Sub

What have I done wrong?

Thanks
Grodan
 
C

Charles Kenyon

Try

Application.Run MacroName:=TestMsg


--

Charles Kenyon

See the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.

GrodanBoll said:
Hi

I have trouble reaching the code in a global template.
I get the code to execute but then I get an error.

When I use the file test.dot to create a new dokument the message box
appears as it should but when I tries to close it via the Ok button I get an
error message:
[error]
Run-tiem error '91':
Object variable or With block variable not set
[/error]

When I press Debug it marks the line "msgBoxen = msgBox("Hello", vbCritical)" with yellow.

In a module in code.dot (the global template)
Code:
Public Sub TestMsg()
Dim msgBoxen As Variable
msgBoxen = msgBox("Hello", vbCritical)
End Sub

Test.dot:
Code:
Private Sub Document_New()
TestMsg
End Sub

What have I done wrong?

Thanks
Grodan
 

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