Ruth
Tue May 06 22:09:17 PDT 2008
On May 6, 11:21 pm, Jay Freedman <jay.freed...@verizon.net> wrote:
> On Tue, 6 May 2008 19:35:36 -0700 (PDT), Ruth <moocatmoo...@yahoo.com> wrote:
> >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 'abcoo...@company.com'
> >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 'def...@company.com'
>
> >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 = _
> "abcoo...@company.com"
> Case "def"
> ActiveDocument.Bookmarks("name").Range.Text = "Donna E. Fox"
> ActiveDocument.Bookmarks("email").Range.Text = "de...@company.com"
> 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.
>
> --
> 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.