Help VBA CODE Command button Book marks? Please

K

Kenny

Okay maybe I went about this the wrong way. I am creating a letter using a
user form. Once user form is filled, press command button, fills form.
Problem is that I need to place the same book mark in multiple spots in the
document. When I place a bookmark with the same name as one I already have in
the document it erases the other.

1. How do I fix this using bookmarks?
2. Should I be using some kind of control in the word doc instead of
bookmarks to make this application work better? If so please provide code I
am very new.

Using MS Office 2003, Please Help, Here is my code.

Private Sub CommandButton1_Click()
Select Case ComboBox1.Text
Case "Army"
ActiveDocument.Bookmarks("Locator").Range.Text = "HRC - Indianapolis" _
& vbCr & "ATTN:AHRC-ERP" & vbCr & "8899 East 56th Street" _
& vbCr & "Indianapolis, IN 46249-5301"
Case "Navy"
ActiveDocument.Bookmarks("Locator").Range.Text = "World Wide Locator" _
& vbCr & "Bureau of Naval Personnel" & vbCr & "PERS 312F" _
& vbCr & "5720 Integrity Drive" & vbCr & "Millington, TN 38055-3120"
Case "Air Force"
ActiveDocument.Bookmarks("Locator").Range.Text = "HQ AFPC/DPDXIDL" _
& vbCr & "550 C. Street West, Suite 50" & vbCr & "Randolph AFB, TX
78150-4752"
Case "Marines"
ActiveDocument.Bookmarks("Locator").Range.Text = "Headquarters USMC" _
& vbCr & "Personnel Management Support Branch (MMSB-17)" _
& vbCr & "2008 Elliot Road" & vbCr & "Quantico, VA 22134-5030"
End Select
With ActiveDocument
.Bookmarks("CustomersName").Range _
.InsertBefore TextBox1
.Bookmarks("AccountNumber").Range _
.InsertBefore TextBox2
.Bookmarks("SocialNumber").Range _
.InsertBefore TextBox3
.Bookmarks("CSRName").Range _
.InsertBefore TextBox4
.Bookmarks("CSRPhone").Range _
.InsertBefore TextBox5
End With
UserForm1.Hide
End Sub

Private Sub UserForm_Initialize()
ComboBox1.AddItem "Army"
ComboBox1.AddItem "Navy"
ComboBox1.AddItem "Air Force"
ComboBox1.AddItem "Marines"
ComboBox1.Style = fmStyleDropDownList
ComboBox1.BoundColumn = 0
ComboBox1.ListIndex = 0
End Sub
 
G

Greg Maxey

Kenny

I thought I showed you how to code this last night.

Private Sub CommandButton1_Click()
Dim oBMs As Bookmarks
Dim oRng As Word.Range
Set oBMs = ActiveDocument.Bookmarks
Select Case ComboBox1.Text
Case "Army"
Set oRng = oBMs("Locator").Range
oRng.Text = "HRC - Indianapolis" _
& vbCr & "ATTN:AHRC-ERP" & vbCr & "8899 East 56th Street" _
& vbCr & "Indianapolis, IN 46249-5301"
oBMs.Add "Locator", oRng
Case "Navy"
Set oRng = oBMs("Locator").Range
oRng.Text = "World Wide Locator" _
& vbCr & "Bureau of Naval Personnel" & vbCr & "PERS 312F" _
& vbCr & "5720 Integrity Drive" & vbCr & "Millington, TN 38055-3120"
oBMs.Add "Locator", oRng
Case "Air Force"
Set oRng = oBMs("Locator").Range
oRng.Text = "HQ AFPC/DPDXIDL" _
& vbCr & "550 C. Street West, Suite 50" & vbCr & "Randolph AFB, TX
78150-4752"
oBMs.Add "Locator", oRng
Case "Marines"
Set oRng = oBMs("Locator").Range
oRng.Text = "Headquarters USMC" _
& vbCr & "Personnel Management Support Branch (MMSB-17)" _
& vbCr & "2008 Elliot Road" & vbCr & "Quantico, VA 22134-5030"
oBMs.Add "Locator", oRng
End Select
Set oRng = oBMs("CustomersName").Range
oRng.Text = Me.TextBox1.Text
oBMs.Add "CustomersNamer", oRng
Set oRng = oBMs("AccountNumber").Range
oRng.Text = Me.TextBox2.Text
oBMs.Add "AccountNumber", oRng
Set oRng = oBMs("SocialNumber").Range
oRng.Text = Me.TextBox3.Text
oBMs.Add "SocialNumber", oRng
Set oRng = oBMs("CSRName").Range
oRng.Text = Me.TextBox4.Text
oBMs.Add "CSRName", oRng
Set oRng = oBMs("CSRPhone").Range
oRng.Text = Me.TextBox5.Text
oBMs.Add "CSRPhone", oRng
UserForm1.Hide
End Sub
Private Sub UserForm_Initialize()
ComboBox1.AddItem "Army"
ComboBox1.AddItem "Navy"
ComboBox1.AddItem "Air Force"
ComboBox1.AddItem "Marines"
ComboBox1.Style = fmStyleDropDownList
ComboBox1.BoundColumn = 0
ComboBox1.ListIndex = 0
End Sub

In the places you want the data repeated add a Reference field to the
bookmark name.

See:
http://gregmaxey.mvps.org/Repeating_Data.htm
 
G

Greg Maxey

And I just noticed that you didn't bother to reply. A simple "Thank you"
goes a long way around here.
 
K

Kenny

Greg I am sorry. I thought it auto replied a thank you when I click this
answerd my question. I am very new. Thanks for your help. The ref is not
working. When I click the command button it exits the user form, all
bookmarks populate, but the REF CustomersName does not update and I get an
error?
 
G

Greg Maxey

Kenny,

Its been a long hard day and perhaps my remark was a bit abrupt. I
apologize.

Add this line just before the .Hide statement

ActiveDocument.Fields.Update

Your REF fields are inserted using CTRL+F9 and before toggling the field
code should look like this { REF "Locator" } { REF "CustomersName" } etc.
 
K

Kenny

Thanks Greg, Ill bet you have had a bad day. Well I added the stmt. And the
ref still does not show the customers name. I have your code entered just
right. The ref is after a page break? Can this cause this problem? I dont
know what else to do. The bookmarks themselfes populate great?
 
G

Greg Maxey

Kenny,

No. Send me feedback using the User Feedback link from the website below. I
will respond so you have a good address.
 

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

Similar Threads

Multiple Book Marks??? 2
Book Marks VB Add Line Returns? 2
User Form Problems 4
ComboBox Help 1
User From Combo box help 1
User Form Issues 2

Top