Inserting text at a bookmark without deleting the bookmark

C

Charles

I have a User form with a document that is only ever printed, never saved.
Multiple documents are printed with different information inserted via the
User form. I wanted to be able to use the User form over again without
opening a new document.

I thought I had found the answer on Microsoft MVP under Inserting text at a
bookmark without deleting the bookmark using this:

Dim bmkAd1 As Range
Set bmkAd1 = ActiveDocument.Bookmarks("bmkAd1").Range
bmkAd1.Text = TxtAd1.Value
ActiveDocument.Bookmarks.Add "bmkAd1", bmkAd1

This works with one bookmark but if repeated, only the last bookmark is
updated. How do I do repetitive updates? By the way, I tried to use the
optional subroutine but do not have the knowledge or skills for this.
 
J

Jay Freedman

In the expression ActiveDocument.Bookmarks("bmkAd1"), the text within the
quotes is the name of a specific bookmark in the document. So if you use
this code, the value of the textbox will *always* be inserted into that
particular bookmark. Similarly, the last line of your code *always* places
the bookmark with that specific name at that spot.

If you need the code to deal with more than one bookmark, then you need the
subroutine in the article. To use it, copy the subroutine from the article
and paste it into your VBA editor at the very end, after the End Sub
statement of your macro. Then, inside your macro, use code like this instead
of the code you quoted:

UpdateBookmark "bmkAd1", TxtAd1.Value
UpdateBookmark "bmkAd2", TxtAd2.Value
UpdateBookmark "bmkAd3", TxtAd3.Value

for as many bookmark/textbox pairs as you need to work with.

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

Charles

Hi Jay

Thanks very for the solution, I am trying now. It is great having so much
expertise at one's finger tips.

Regards

Charles
 
C

Charles

Jay

I need some help with the subroutine from the article. Do I need a
subroutine for each bookmark, because that is how it appears from the wording
in the brackets.

This is how my macro looks at present:
Private Sub cmdOK_Click()
Dim strReason As String
Dim strType As String
If optReason1 = True Then strReason = "Actual Exit"
If optReason2 = True Then strReason = "Quotation Only"
If optType1 = True Then strType = "Leaving Service"
If optType2 = True Then strType = "Opting Out (Complete Form 6
Opting-Out)"
If optType3 = True Then strType = "Ill Health Early Retirement (Complete
Form 8 Member's Declaration - Ill Health Retirement)"
If optType4 = True Then strType = "Retirement"
If optType5 = True Then strType = "Death"

Application.ScreenUpdating = False
With ActiveDocument

.Bookmarks("bmkID").Range.Text = txtID.Value
.Bookmarks("bmkSur").Range.Text = txtSur.Value
.Bookmarks("bmkTtl").Range.Text = TxtTtl.Value
.Bookmarks("bmkInt").Range.Text = TxtInt.Value
.Bookmarks("bmkNI").Range.Text = txtNI.Value
.Bookmarks("bmkExit").Range.Text = txtExit.Value
.Bookmarks("bmkPay").Range.Text = txtPay.Value
.Bookmarks("bmkAd1").Range.Text = TxtAd1.Value
.Bookmarks("bmkAd2").Range.Text = TxtAd2.Value
.Bookmarks("bmkAd3").Range.Text = TxtAd3.Value
.Bookmarks("bmkAd4").Range.Text = TxtAd4.Value
.Bookmarks("bmkPC").Range.Text = TxtPc.Value
.Bookmarks("bmkSal").Range.Text = txtSal.Value
.Bookmarks("bmkAct").Range.Text = strReason
.Bookmarks("bmkOpt").Range.Text = strType

End With
Application.ScreenUpdating = True
' ActiveDocument.PrintOut Copies:=2
UpdateBookmark "bmkID", txtID.Value
UpdateBookmark "bmkSur", txtSur.Value
UpdateBookmark "bmkTtl", TxtTtl.Value
UpdateBookmark "bmkInt", TxtInt.Value
UpdateBookmark "bmkNI", txtNI.Value
UpdateBookmark "bmkExit", txtExit.Value
UpdateBookmark "bmkPay", txtPay.Value
UpdateBookmark "bmkAd1", TxtAd1.Value
UpdateBookmark "bmkAd2", TxtAd2.Value
UpdateBookmark "bmkAd3", TxtAd3.Value
UpdateBookmark "bmkAd4", TxtAd4.Value
UpdateBookmark "bmkPC", TxtPc.Value
UpdateBookmark "bmkSal", txtSal.Value
UpdateBookmark "bmkAct", strReason
UpdateBookmark "bmkOpt", strType
Unload Me
End Sub

Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
BMRange.Text = TextToUse
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
End Sub

Thanks

Charles
 
J

Jay Freedman

Hi Charles,

No, you don't need a subroutine for each bookmark. The idea is that there's
one subroutine named UpdateBookmark, and you "call" it once for each
bookmark. Each time you call it, you supply different information for it to
work with. The principle is explained in
http://www.word.mvps.org/FAQs/MacrosVBA/ProcArguments.htm.

For the code you showed, you should delete the group of lines starting with
"With ActiveDocument" and ending with "End With". The job that used to be
done by those lines is now done by all the lines that call UpdateBookmark.
Once you've done that, move the line "Application.ScreenUpdating = True" to
the point just before the line "Unload Me".

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

Charles

Hi Jay

Thanks very much for your help.

It's much appreciated, as I have been battling this in the dark.

Regards

Charles
 
C

Charles

Hi Jay

Another problem, when I amend the script I get the error:

Compile error: Expected Sub, Function, or Property.

I have tried various things but can not figure out what is wrong. This is my
amended macro:

Private Sub cmdOK_Click()
Dim UpdateBookmark As String
Dim strReason As String
Dim strType As String
If optReason1 = True Then strReason = "Actual Exit"
If optReason2 = True Then strReason = "Quotation Only"
If optType1 = True Then strType = "Leaving Service"
If optType2 = True Then strType = "Opting Out (Complete Form 6
Opting-Out)"
If optType3 = True Then strType = "Ill Health Early Retirement (Complete
Form 8 Member's Declaration - Ill Health Retirement)"
If optType4 = True Then strType = "Retirement"
If optType5 = True Then strType = "Death"

Application.ScreenUpdating = False

UpdateBookmark "bmkID", txtID.Value
UpdateBookmark "bmkSur", txtSur.Value
UpdateBookmark "bmkTtl", TxtTtl.Value
UpdateBookmark "bmkInt", TxtInt.Value
UpdateBookmark "bmkNI", txtNI.Value
UpdateBookmark "bmkExit", txtExit.Value
UpdateBookmark "bmkPay", txtPay.Value
UpdateBookmark "bmkAd1", TxtAd1.Value
UpdateBookmark "bmkAd2", TxtAd2.Value
UpdateBookmark "bmkAd3", TxtAd3.Value
UpdateBookmark "bmkAd4", TxtAd4.Value
UpdateBookmark "bmkPC", TxtPc.Value
UpdateBookmark "bmkSal", txtSal.Value
UpdateBookmark "bmkAct", strReason
UpdateBookmark "bmkOpt", strType

Application.ScreenUpdating = True
' ActiveDocument.PrintOut Copies:=2
Unload Me
End Sub
Sub UpdateBookmark()
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
BMRange.Text = TextToUse
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
End Sub

Thanks

Charles
*******************************************************
 
J

Jay Freedman

Hi Charles,

Somewhere along the way you lost part of the first line of the subroutine.
Look back at the code you posted a couple of messages ago, which is still at
the bottom of this post -- it should be

Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)

but now the parameters between the parentheses are missing.

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

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