"Ask" to modify existing fields

L

Lopaka

Hello: I work with documents which are created automatically by a different
program, which I then modify in Word 2007.
I can see various fields such as {REF CONTACTSFSTNAME \* Caps} {REF
CONTACTLSTNAME \* Caps}, and at the bottom of the page, {SET CONTACTLSTNAME
"LAVENDER"} {SET CONTACTFSTNAME "JOHN"} .
Can you help with a Macro which will let me update the information in the
field so I can change names, addresses, etc.? Would be helpful if I could
change both first and last names with one procedure, but two is fine also.
Thanks very much for any help you can give -- you are all awesome! (I'm
still very novice as far as VBA)
Bob, 55, California
 
J

Jean-Guy Marcil

Lopaka was telling us:
Lopaka nous racontait que :
Hello: I work with documents which are created automatically by a
different program, which I then modify in Word 2007.
I can see various fields such as {REF CONTACTSFSTNAME \* Caps} {REF
CONTACTLSTNAME \* Caps}, and at the bottom of the page, {SET
CONTACTLSTNAME "LAVENDER"} {SET CONTACTFSTNAME "JOHN"} .
Can you help with a Macro which will let me update the information in
the field so I can change names, addresses, etc.? Would be helpful
if I could change both first and last names with one procedure, but
two is fine also. Thanks very much for any help you can give -- you
are all awesome! (I'm still very novice as far as VBA)
Bob, 55, California

This would be one way of doing it:

Sub ChangeName()

Dim strLastName As String
Dim strFirstName As String
Dim fldDoc As Fields
Dim i As Long
Dim x As Long

strFirstName = InputBox("Type the first name:", "First Name")
strLastName = InputBox("Type the last name:", "last Name")

Set fldDoc = ActiveDocument.Fields

x = 0
For i = 1 To fldDoc.Count
If InStr(1, fldDoc(i).Code.Text, "SET CONTACTFSTNAME") > 0 Then
fldDoc(i).Code.Text = "SET CONTACTFSTNAME " & Chr(34) & strFirstName
& Chr(34)
x = x + 1
End If
If InStr(1, fldDoc(i).Code.Text, "SET CONTACTLSTNAME") > 0 Then
fldDoc(i).Code.Text = "SET CONTACTLSTNAME " & Chr(34) & strLastName
& Chr(34)
x = x + 1
End If
If x = 2 Then Exit For
Next

fldDoc.Update
fldDoc.Update

End Sub
 

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