use loop to populate bookmarks or fields

R

rogge

is there a method to use docWord.Bookmarks(i).Range.Text or
docWord.FormFields(i).Range.Text without deleting the bookmark or field?
______________________________________________

Hear Ye!, Hear Ye! I am a bit lazy and do not like to type code. So I
thought, "What if i put the fields in a DAO.Recordset in the same order that
the data would appear in MS Word. Then i could loop through the bookmarks
and fields while using the same code to enter text. This would be great!"

I am using MS Access to place text into a word document. I have tried
bookmarks and fields, but when "docWord.Bookmarks(i).Range.Text =
rds.Fields(i).Value" then the bookmark or field is deleted causing the data
to be placed into the incorret spot and not completing the transfer.

I know i can use direct references:
docWord.Bookmarks("nameTo").Range.Text = rds.Fields("nameTo").Value
- or the bookmarks can be numbered sequentially:
docWord.Bookmarks("bkm" & i).Range.Text = rds.Fields(i).Value
- or one can just type the document from scratch,
But as i said earlier i am LAZY and would bery much prefer to use a loop and
not have to worry about properly naming bookmarks to coincide with field
names.

For i = 1 To docWord.Bookmarks.Count
docWord.Bookmarks(i).Range.Text = rds.Fields(i).Value
Next i
________________________________________________

For i = 1 To docWord.FormFields.Count
docWord.FormFields(i).Range.Text = rds.Fields(i).Value
Next i

This code runs, umm, err, 'fine (?)' except that the bookmarks or fields are
re-numbered as the bookmarks and fields are deleted and this causes data to
be placed improperly in the word document. Any "no", i do not run both of
these. I am only using fields or bookmarks.

Thank you veyr much.
 
J

Jay Freedman

See http://word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm. The
technique may have to be tweaked a bit. You can refer to a bookmark with
either a numeric index or its assigned name. Use the index to get the range,
then save off the bookmark's name to use in the last step.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
D

Doug Robbins - Word MVP

If the bookmark is created so that it contains a space so that with the
display of bookmarks turned on it looks like [] rather than |, then if you
use

Bookmarks(i).Range.InsertBefore "the text to be inserted"

the text will be inserted inside the [ ].

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

rogge

Talk about art. That is a good idea. Thanks.

Doug Robbins - Word MVP said:
If the bookmark is created so that it contains a space so that with the
display of bookmarks turned on it looks like [] rather than |, then if you
use

Bookmarks(i).Range.InsertBefore "the text to be inserted"

the text will be inserted inside the [ ].

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

rogge said:
Thank you Jay... That is helpful, i was hoping to not use the Add method.
 

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