Inserting Text at a Bookmark - Follow Up Question

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 - (BookmarkToUpdate As String, TextToUse As String)

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
 
A

Astrid

Hi Charles,

I didn't read the earlier messages on this topic, but viewing the code tells
me you don't need a subroutine for every bookmark:

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

The text between the brackets tells what arguments are used for this
routine. In this case it needs an string variable that represents the
BookMarkToUpdate and another string variable that represents the TextToUse:

UpdateBookmark "bmkID", txtID.Value
Here "bmkID" is the name of the bookmark for the argument BookMarkToUpdate
and the value of the control txtID.value represents the text that needs to
be written to the bookmark (TextToUse)

The routine UpdateBookmark writes the text to the bookmark and recreates it
so you can use it later on if you still need it. If you don't recreate the
bookmark in your code, it will be lost as soon as you've written some text
to it. This shouldn't have to be a problem but depends on if you want to use
the bookmark later on as well.

If I have a quick look at your code, I see you write the text to all the
bookmarks twice, once with the line:
.Bookmarks("bmkID").Range.Text = txtID.Value
and the other time with:
UpdateBookmark "bmkID", txtID.Value

Just delete one. No need to do it twice ;-)

Hope this helps,
kind regards,
Astrid
 

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


Top