Auto-Populate Email Address in Word 2007

P

Peter L

In word 2007, I am trying to create a fax cover sheet template that will
auto-populate the user's name, title and email address as soon as they open
the template.
I know how to insert today's date but i cannot figure out how to have the
user's email address get filled in the template as soon as it opens.
 
G

Graham Mayor

I take it that you want this template to apply to whoever uses it rather
than for your personal use?

The following macro will read and enter the current user's name and e-mail
address from Outlook at a bookmark location "Email" or at the cursor if that
location does not exist. You will need to add the Outlook object library to
the vba editor tools > references.

Sub InsertCurrentUsersEmailAddress()
Dim olook As Outlook.Application
Dim sEAddress As String
Dim sEName As String
Dim bStarted As Boolean
On Error Resume Next
Set olook = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set olook = CreateObject("Outlook.Application")
bStarted = True
End If
sEAddress = olook.Session.CurrentUser.Address
sEName = olook.Session.CurrentUser.name
' Close Outlook if it was started by this macro.
If bStarted Then
oOutlookApp.Quit
End If
With Selection
.GoTo What:=wdGoToBookmark, name:="EMail"
.TypeText Text:=sEName & vbTab & sEAddress
End With
End Sub

http://www.gmayor.com/installing_macro.htm

Obtaining the title is rather more complicated as the user's title is not
stored in Word or Outlook. I suppose it would be possible to write code to
populate custom document property fields and read those into the document,
but it all starts to get a tad complicated if the template is to be shared.

See also http://www.gmayor.com/Macrobutton.htm

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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

Trying to keep things as simple as possible the following stores the user
details in an ini file in the user templates folder. The first time the
macro is run it will prompt for the user details. These are then stored and
used to insert the data subsequently. The macro uses a message box to
confirm the details are correct and thus allows the user to change incorrect
details. You can save this as an autonew macro in the fax document template
or add it to a toolbar button.

Dim SettingsFile As String
Dim sUserName As String
Dim sEmail As String
Dim sTitle As String
Dim sCheck As String
On Error Resume Next

SettingsFile = Options.DefaultFilePath(wdUserTemplatesPath) &
"\Settings.ini"

sUserName = System.PrivateProfileString(SettingsFile, _
"ThisUser", "Name")
sEmail = System.PrivateProfileString(SettingsFile, _
"ThisUser", "Email")
sTitle = System.PrivateProfileString(SettingsFile, _
"ThisUser", "Title")
If sUserName = "" Then
GetUser:
sUserName = InputBox("Enter user's Name", "User Name", sUserName)
sTitle = InputBox("Enter user's title, if any", "User Title", sTitle)
sEmail = InputBox("Enter user's e-mail address", "User E-mail", sEmail)
System.PrivateProfileString(SettingsFile, "ThisUser", _
"Name") = sUserName
System.PrivateProfileString(SettingsFile, "ThisUser", _
"Email") = sEmail
System.PrivateProfileString(SettingsFile, "ThisUser", _
"Title") = sTitle
End If

sCheck = MsgBox("User is " & sUserName & vbCr & sTitle & _
vbCr & sEmail, vbYesNo, "Confirm User Details")
If sCheck = vbNo Then GoTo GetUser:

With Selection
.GoTo What:=wdGoToBookmark, name:="EMail"
.TypeText Text:=sUserName & vbTab & sEmail & vbCr & sTitle
End With



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

My web site www.gmayor.com

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

Peter L

Wow, this is complex. I was able to follow your instructions and I was able
to have to macro return a result. It does not return the correct email
address though. It returns the X400 email and what groups i belong to, not
the smtp email address.

I could not get the ini file instructions to work without an error.
 
P

Peter L

Correction: I was able to to get it to work using the ini file. I really like
this now. I just have to customized it to return to the correct result.

Thank you.
 

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