getting a macro to run when doc is opened

C

cindy

Hi-
I need a little help to get this macro to run
automatically when the the document is opened.
I got this macro from a newsgroup/web and it works to
grab the userID - BUT I have to run it manually.
Can I add something to it, so it will run when they open
the file. thanks in advance for your help!!!!

Here's the macro:
*******************************
'Macro to grab Network ID and set it in Word

Option Explicit

'Declare for call to mpr.dll.
Declare Function WNetGetUser Lib "mpr.dll" _
Alias "WNetGetUserA" (ByVal lpName As String, _
ByVal lpUserName As String, lpnLength As Long) As
Long

Const NoError = 0 'The Function call was successful

'-----------------------------------------------

Function GetUserName() As String

'Buffer size for the return string.
Const lpnLength As Long = 255

'Get return buffer space.
Dim status As Integer

'For getting user information.
Dim lpName, lpUserName As String

'Assign the buffer size constant to lpUserName.
lpUserName = Space$(lpnLength + 1)

'Get the log-on name of the person using product.
status = WNetGetUser(lpName, lpUserName, lpnLength)

'See whether error occurred.
If status = NoError Then
'This line removes the null character. Strings in C
are null-
'terminated. Strings in Visual Basic are not null-
terminated.
'The null character must be removed from the C
strings to be used
'cleanly in Visual Basic.
lpUserName = Left$(lpUserName, InStr(lpUserName, Chr
(0)) - 1)
End If

'Display the name of the person logged on to the machine.
GetUserName = lpUserName

End Function

'Call it with this instead of original:

Sub SetWordUserName()
Application.UserName = GetUserName
End Sub


thanks again!!!! Cindy
 
P

Peter Hewett

Hi cindy

There's a couple of ways to do this you can use either AutoNew/AutoOpen macros or
Document_New/Document_Open macros. I have a preference for the Document_New/Document_Open
macros so I'll tell you how to set them up.

You need to add the following code to your templates ThisDocument module:

Private Sub Document_New()
SetWordUserName
End Sub

Private Sub Document_Open()
SetWordUserName
End Sub

HTH + Cheers - Peter
 
C

cindy

thanks for the help -
this still does not run when opened..I added it at the
bottom - I'll paste so you can look at it. Do I need to
place it in a different position? I can still manually
go to macros and run "SetWordUserName" and it works - but
it is not automated. I must be doing something wrong..
do you have a minute to look at it?

'Macro to grab Network ID and set it in Word
'Cindy Benedett - 3-31-04
'Used for LEC web Logs

Option Explicit

'Declare for call to mpr.dll.
Declare Function WNetGetUser Lib "mpr.dll" _
Alias "WNetGetUserA" (ByVal lpName As String, _
ByVal lpUserName As String, lpnLength As Long) As
Long

Const NoError = 0 'The Function call was successful

'-----------------------------------------------

Function GetUserName() As String

'Buffer size for the return string.
Const lpnLength As Long = 255

'Get return buffer space.
Dim status As Integer

'For getting user information.
Dim lpName, lpUserName As String

'Assign the buffer size constant to lpUserName.
lpUserName = Space$(lpnLength + 1)

'Get the log-on name of the person using product.
status = WNetGetUser(lpName, lpUserName, lpnLength)

'See whether error occurred.
If status = NoError Then
'This line removes the null character. Strings in C
are null-
'terminated. Strings in Visual Basic are not null-
terminated.
'The null character must be removed from the C
strings to be used
'cleanly in Visual Basic.
lpUserName = Left$(lpUserName, InStr(lpUserName, Chr
(0)) - 1)
End If

'Display the name of the person logged on to the machine.
GetUserName = lpUserName

End Function
Private Sub Document_New()
SetWordUserName
End Sub

Private Sub Document_Open()
SetWordUserName
End Sub

'Call it with this instead of original:
Sub SetWordUserName()
Application.UserName = GetUserName
End Sub

THANKS AGAIN---CINDY
-----Original Message-----
Hi cindy

There's a couple of ways to do this you can use either AutoNew/AutoOpen macros or
Document_New/Document_Open macros. I have a preference
for the Document_New/Document_Open
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < cindy > écrivait :
In this message, < cindy > wrote:

|| thanks for the help -
|| this still does not run when opened..I added it at the
|| bottom - I'll paste so you can look at it. Do I need to
|| place it in a different position? I can still manually
|| go to macros and run "SetWordUserName" and it works - but
|| it is not automated. I must be doing something wrong..
|| do you have a minute to look at it?

What is the name of the module in which you copied Peter's macros?
They must be in the "ThisDocument" module, as Peter stated.

From your post, I get the impression that they were not placed there....

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

cindy

Hi Peter-
thanks for your help... you also helped with a question
on posted on the General newsgroup about set curser
positon.
OK- here's what I did:
*****
Private Sub Document_Open()
SetWordUserName
GotoLine8
End Sub

Public Sub GotoLine8()
Selection.GoTo wdGoToLine, wdGoToFirst, 8, ""
End Sub

Sub SetWordUserName()
Application.UserName = GetUserName
End Sub
****

I put it at the bottom of the macro - both the GotoLine8
and the SetWordUserName run correctly manually - but
still do not run when I open the document. Should I
place it differently??? If I just list it as a sub
(instead of Private Sub) It shows in the macro list - I
can then run IT manually and its corectly calls the
other 2.. However - I stilll need it to run
automatically...

Any ideas???
thanks again - Cindy


-----Original Message-----
Hi cindy

There's a couple of ways to do this you can use either AutoNew/AutoOpen macros or
Document_New/Document_Open macros. I have a preference
for the Document_New/Document_Open
 
C

cindy

Yes - I did a right click on ThisDocument - insert-
module - it shows as module1. All parts of the macro are
posted there.
thanks-Cindy
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < cindy > écrivait :
In this message, < cindy > wrote:

| Yes - I did a right click on ThisDocument - insert-
| module - it shows as module1. All parts of the macro are
| posted there.
| thanks-Cindy

That's the problem! ;-)

From your description you will see that "Module1" is not part of
"ThisDocument", but is a standard module branching out under "Modules". You
have to double-click on "ThisDocument" (Which is itself a module branching
out from "Microsoft Word Objects") to access its code pane on the right and
paste the code Peter provided there. This way, Peter's code will be IN
"ThisDocument" which is a special class module. Once there, look at the top
of the code pane, you will see two dropdown lists, the one on the left says
"General", if you look at its content, you will see another choice called
"Document" (In a standard module, there are no other choices unless you
create your own class modules). You do not need to do this as Peter gave you
the full procedures, but you could select "Document" (In which case a
"Private Sub Document_New()" procedure is automatically added to the code
pane), and then the list on the right will give you access to three special
event procedures: New, Open, Close (If you did the same in Excel - from
"ThisWorksheet", you would see that Excel has more events than Word has...
Each Office application has a different object model built in...). The
procedures Peter provided will not fire automatically as expected if you put
them in a standard module as you did with Module1.

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

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