need macro to prompt user for initials & use if statement to plug inname, email, etc.

R

Ruth

Hi, I'm a former Wordperfect user.
this was easy to do in WP, but I need to do it in Word 2003.
Inside the letterhead template (probably using AutoNew?)
I need the macro to
1. prompt user for the attorney's initials
2. Then use an if statement to go to bookmarks and fill in fields
based on the initials.

For example, the user types in 'abc'.
the macro presumably would use some sort of 'if' statement: If
variable 'initials' equals 'abc'
then go bookmark 'name' and type 'Andrew B. Cooper.' then go to
bookmark 'email' and type '(e-mail address removed)'
However, if the user types in 'def', the macro would go to the 'name'
bookmark and type Donna E. Foxx and then go to the bookmark 'email'
and type '(e-mail address removed)'

Many thanks for any assistance!
 
J

Jay Freedman

Hi, I'm a former Wordperfect user.
this was easy to do in WP, but I need to do it in Word 2003.
Inside the letterhead template (probably using AutoNew?)
I need the macro to
1. prompt user for the attorney's initials
2. Then use an if statement to go to bookmarks and fill in fields
based on the initials.

For example, the user types in 'abc'.
the macro presumably would use some sort of 'if' statement: If
variable 'initials' equals 'abc'
then go bookmark 'name' and type 'Andrew B. Cooper.' then go to
bookmark 'email' and type '(e-mail address removed)'
However, if the user types in 'def', the macro would go to the 'name'
bookmark and type Donna E. Foxx and then go to the bookmark 'email'
and type '(e-mail address removed)'

Many thanks for any assistance!

The Word macro looks pretty much like your statement of the process:

Sub AutoNew()
Dim initials As String
initials = InputBox("Enter attorney's initials")
If Len(initials) = 0 Then Exit Sub ' user canceled

Select Case initials
Case "abc"
ActiveDocument.Bookmarks("name").Range.Text = "Andrew B. Cooper"
ActiveDocument.Bookmarks("email").Range.Text = _
"(e-mail address removed)"
Case "def"
ActiveDocument.Bookmarks("name").Range.Text = "Donna E. Fox"
ActiveDocument.Bookmarks("email").Range.Text = "(e-mail address removed)"
Case Else
MsgBox "Sorry, " & initials & " is not listed"
End Select
End Sub

A few notes on this:

- The Select Case structure is more efficient than an If/Then structure for more
than two or three possible choices, but the effect is the same -- only the case
that matches will be executed, or the "Case Else" if there is no match.

- Storing the names and addresses directly in the code this way is acceptable if
there are only a few, and if they don't change very often. If that isn't the
case, you don't want to have to edit the macro every time there's a change. Then
you want to store the names and addresses somewhere else -- in a Word document,
an Excel worksheet, a database, etc. -- and read that source when the macro
runs.

- Assigning text to a bookmark's range is preferable to moving the cursor to the
bookmark and "typing" the text. For one thing, it doesn't cause the document to
scroll on the screen while the macro is running, or leave the cursor in some odd
place when the user gets control. It does have one side effect, though: when the
text is inserted, the bookmark itself is deleted. If you need the bookmark for
some later operation, you need to change the code a bit; see
http://www.word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm.
 
R

Ruth

The Word macro looks pretty much like your statement of the process:

Sub AutoNew()
Dim initials As String
initials = InputBox("Enter attorney's initials")
If Len(initials) = 0 Then Exit Sub ' user canceled

Select Case initials
Case "abc"
ActiveDocument.Bookmarks("name").Range.Text = "Andrew B. Cooper"
ActiveDocument.Bookmarks("email").Range.Text = _
"(e-mail address removed)"
Case "def"
ActiveDocument.Bookmarks("name").Range.Text = "Donna E. Fox"
ActiveDocument.Bookmarks("email").Range.Text = "(e-mail address removed)"
Case Else
MsgBox "Sorry, " & initials & " is not listed"
End Select
End Sub

A few notes on this:

- The Select Case structure is more efficient than an If/Then structure for more
than two or three possible choices, but the effect is the same -- only the case
that matches will be executed, or the "Case Else" if there is no match.

- Storing the names and addresses directly in the code this way is acceptable if
there are only a few, and if they don't change very often. If that isn't the
case, you don't want to have to edit the macro every time there's a change. Then
you want to store the names and addresses somewhere else -- in a Word document,
an Excel worksheet, a database, etc. -- and read that source when the macro
runs.

- Assigning text to a bookmark's range is preferable to moving the cursor to the
bookmark and "typing" the text. For one thing, it doesn't cause the document to
scroll on the screen while the macro is running, or leave the cursor in some odd
place when the user gets control. It does have one side effect, though: when the
text is inserted, the bookmark itself is deleted. If you need the bookmark for
some later operation, you need to change the code a bit; seehttp://www.word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.

Many thanks! that's exactly what I was looking for. There are only a
few names & they shouldn't change often, so storing them in the code
is probably fine. I like the case option much better than the 'ifs';
on my few attempts I found Word to be very finicky about line endings,
tabs, etc and I have a hard time avoiding 'block if without end if'
messages.
Anyway, thank you again.
 
G

Gordon Bentley-Mix

Ruth,

There are a few ways you can help yourself avoid syntax errors like 'block
if without end if'.

First, always indent the code between the start and end of 'If' statements
like this:

If [condition to evaluate] Then
'DO STUFF
Else
'DO OTHER STUFF
End If

If you have nested 'If' statements then use further indention to keep things
in order like this:

If [condition to evaluate] Then
If [another condition to evaluate] Then
'DO STUFF
Else
'DO DIFFERENT STUFF
End If
Else
'DO OTHER STUFF
End If

Second, whenever I'm doing something like this I always type the 'If'
statement and then immediately type the 'End If' statement like this:

If [condition to evaluate] Then
End If

Then I go back and fill in the code in between. That way I don't forget to
close the 'If' statement. And if I know I'm going to have an 'Else', I put
that in straight away as well.

VBA is kind enough to supply 'End Sub' and 'End Function' statements
automatically, but unfortunately it won't supply Ends for other statements,
so the above works equally well with 'With' and 'Select Case' statements, too.
--
Cheers!
Gordon
The Kiwi Koder

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.

:
[snip]
 

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