Automating Word with Access

M

MSU Sptn2

I have written code in Access to insert text from a form in Access to a
bookmark in Word. I would like to reference the bookmark later in the Word
document using a reference field, but it seems the bookmark is deleted after
the text is inserted. I am using the following code for each bookmark name
in the document:

.ActiveDocument.Bookmarks("PIC").Select
If [Form_ODDAPP FORM].PIC <> 1 Then
.Selection.Text = (CStr([Form_ODDAPP FORM].PIC))
Else
.Selection.Text = .Selection.Text & (CStr(""))
End If

Any ideas on how to recreate the bookmark containing the inserted text?
Thanks.
 
A

aidan.heritage

the bookmark needs to be a single point (not a selection) and I would
use

activedocument.bookmarks("pic").range.text=etc

rather than selecting the bookmark.
 
A

aidan.heritage

Put brain in gear before typing - sorry, obviously you need a block
bookmark to REFERENCE it - but that WILL result in the bookmark being
lost - could you use a text field from the forms toolbar instead - this
way, you have a permanent item to reference.
the bookmark needs to be a single point (not a selection) and I would
use

activedocument.bookmarks("pic").range.text=etc

rather than selecting the bookmark.
MSU said:
I have written code in Access to insert text from a form in Access to a
bookmark in Word. I would like to reference the bookmark later in the Word
document using a reference field, but it seems the bookmark is deleted after
the text is inserted. I am using the following code for each bookmark name
in the document:

.ActiveDocument.Bookmarks("PIC").Select
If [Form_ODDAPP FORM].PIC <> 1 Then
.Selection.Text = (CStr([Form_ODDAPP FORM].PIC))
Else
.Selection.Text = .Selection.Text & (CStr(""))
End If

Any ideas on how to recreate the bookmark containing the inserted text?
Thanks.
 
J

Jean-Guy Marcil

MSU Sptn2 was telling us:
MSU Sptn2 nous racontait que :
I have written code in Access to insert text from a form in Access to
a bookmark in Word. I would like to reference the bookmark later in
the Word document using a reference field, but it seems the bookmark
is deleted after the text is inserted. I am using the following code
for each bookmark name in the document:

.ActiveDocument.Bookmarks("PIC").Select
If [Form_ODDAPP FORM].PIC <> 1 Then
.Selection.Text = (CStr([Form_ODDAPP FORM].PIC))
Else
.Selection.Text = .Selection.Text & (CStr(""))
End If

Any ideas on how to recreate the bookmark containing the inserted
text? Thanks.


If you are going to replace text in many bookmarks, than a function is the
way to go (Or a Private Sub):
Avoid the Selection object as much as possible, it is unreliable, slow and
can cause errors.

'_______________________________________
Sub Main()

Dim docCurrent As Document

'I presume you alredy have your Word.Application object at this point
Set docCurrent = WordApp.ActiveDocument

With docCurrent
If [Form_ODDAPP FORM].PIC <> 1 Then
PopulateBookmarks docCurrent, "PIC", CStr([Form_ODDAPP FORM].PIC)
Else
'I do not understand your code here, you were replacing the
'bookmark text with itself, why bother?
PopulateBookmarks docCurrent, "PIC", .Bookmarks("PIC").Range.Text
End If
End With

End Sub
'_______________________________________
'_______________________________________
Function PopulateBookmarks(doc As Document, _
strBookName As String, strValue As String)

Dim rgeBook As Range

Set rgeBook = doc.Bookmarks(strBookName).Range

rgeBook.Text = strValue
doc.Bookmarks.Add strBookName, rgeBook

End Function
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
M

MSU Sptn2

You're right, I used the following code for each bookmark and the refernce
fields worked:

'Identify current Bookmark range and insert text
Set BMRange = ActiveDocument.Bookmarks("PIC").Range
If [Form_ODDAPP FORM].PIC <> "" Then
BMRange.Text = (CStr([Form_ODDAPP FORM].PIC))
Else
BMRange.Text = (CStr(""))
End If
'Re-insert the bookmark
ActiveDocument.Bookmarks.Add "PIC", BMRange

Put brain in gear before typing - sorry, obviously you need a block
bookmark to REFERENCE it - but that WILL result in the bookmark being
lost - could you use a text field from the forms toolbar instead - this
way, you have a permanent item to reference.
the bookmark needs to be a single point (not a selection) and I would
use

activedocument.bookmarks("pic").range.text=etc

rather than selecting the bookmark.
MSU said:
I have written code in Access to insert text from a form in Access to a
bookmark in Word. I would like to reference the bookmark later in the Word
document using a reference field, but it seems the bookmark is deleted after
the text is inserted. I am using the following code for each bookmark name
in the document:

.ActiveDocument.Bookmarks("PIC").Select
If [Form_ODDAPP FORM].PIC <> 1 Then
.Selection.Text = (CStr([Form_ODDAPP FORM].PIC))
Else
.Selection.Text = .Selection.Text & (CStr(""))
End If

Any ideas on how to recreate the bookmark containing the inserted text?
Thanks.
 

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