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
"Jay Freedman" wrote:
> 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.
>
> > 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.
Astrid - 06 Nov 2006 00:26 GMT
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
> Jay
>
[quoted text clipped - 111 lines]
>> > use the optional subroutine but do not have the knowledge or skills
>> > for this.