Access Option Group Value to Word Bookmarks- Need Help with Redundancy

C

Carlos

I have a many forms with numerous option groups and their values, which
I then put into bookmarks on a Word document. Each option group is
named optNAME1, optNAME2, optNAME3, etc. and each bookmark is named
Bkmrk1, Bkmrk2, Bkmrk3, etc. I have created a button on the form which
opens up the word document, inserts the corresponding value, saves, and
closes. The coding for the button, however, seems like it could be
simplified. Here is an example:

With ActiveDocument.Bookmarks
.Item("Bkmrk1").Range.Text = Me.optNAME1.Value
.Item("Bkmrk2").Range.Text = Me.optNAME2.Value

....etc.

This is a lot of writing, which I don't mind since it works; however, I
have ventured into trying to simplify this code, since the only thing
that changes is the number. So far, I have not succeeded. For example:

I defined intx as an Integer. Then, I tried to concatenate and loop
optNAME with intx, and Bkmrk with intx.

Dim intx as integer
Dim optionNAME as String
Dim BOOKmrk as String

Do Until intx = NumberofOptionGroups
intx = intx + 1
optionNAME = "Me.optNAME" & intx & ".Value"
BOOKmrk = """Bkmrk" & intx & """"

With ActiveDocument.Bookmarks
..Item(BOOKmrk).Range.Text = optionNAME
Loop

This methods does not work. Perhaps someone has some suggestions on the
errors that I run into with this piece of code, or suggestions on a
method to accomplish the same goal. I would love to know what I'm
doing wrong because I tried inserting a real bookmark, "Bkmrk1", for
BOOKmrk and I simply got the string optionNAME and not the value.

Carlos
 
M

Marshall Barton

Carlos said:
I have a many forms with numerous option groups and their values, which
I then put into bookmarks on a Word document. Each option group is
named optNAME1, optNAME2, optNAME3, etc. and each bookmark is named
Bkmrk1, Bkmrk2, Bkmrk3, etc. I have created a button on the form which
opens up the word document, inserts the corresponding value, saves, and
closes. The coding for the button, however, seems like it could be
simplified. Here is an example:

With ActiveDocument.Bookmarks
.Item("Bkmrk1").Range.Text = Me.optNAME1.Value
.Item("Bkmrk2").Range.Text = Me.optNAME2.Value

...etc.

This is a lot of writing, which I don't mind since it works; however, I
have ventured into trying to simplify this code, since the only thing
that changes is the number. So far, I have not succeeded. For example:

I defined intx as an Integer. Then, I tried to concatenate and loop
optNAME with intx, and Bkmrk with intx.

Dim intx as integer
Dim optionNAME as String
Dim BOOKmrk as String

Do Until intx = NumberofOptionGroups
intx = intx + 1
optionNAME = "Me.optNAME" & intx & ".Value"
BOOKmrk = """Bkmrk" & intx & """"

With ActiveDocument.Bookmarks
.Item(BOOKmrk).Range.Text = optionNAME
Loop


The syntax for this kind of thing is:

Dim intx as integer
With ActiveDocument.Bookmarks
For intx = 1 To NumberofOptionGroups
.Item("Bkmrk" & intx).Range.Text = Me("optNAME" & intx)
Next intx
End With
 

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