Changing bookmarks trought userforms

R

Robert

Hello,

I'm trying to create a template which will start with a userform where you
can fill in some data which will be added to bookmarks in a secure section
of the form.

The problem i am facing is as follows:

The adress is a multiline textbox in the userform, the lenght of this field
varies, because the user is allowed to change the adress using the userform.
The text from the textbox most be inserted into a bookmark which is placed
into a table. When the adress is placed in the bookmark i change the range
of that bookmark so i have always the correct lenght od the bookmark so that
i can select i easily again. When i want to change the adress the selection
is still extended whith 2 places which results that a linefeed (chr(10)) and
sometimes a enter(chr(13)) is selected. As a result of that selection the
bookmarks exseets the table, which as a result is deleted the next time the
adress is changed. I have added the code i used for filling and changing of
the userform fields and bookmarks.

I hope someone understands my problem and that i have writed this article
correctly while English isn't my native language

Thanks in advance
Robert


Code for filling and changing the bookmarks

Private Sub KnAkkoord_Click()
Dim Ctrl As Control, start As Long, Hdoc As ThisDocument

Set Hdoc = ThisDocument

For Each Ctrl In FrmAdressering.Controls
If Not (Ctrl.Tag = "") And Hdoc.Bookmarks.Exists(Ctrl.Tag) Then
With Hdoc.Bookmarks
.Item(Ctrl.Tag).Select
start = Selection.start
Selection.Delete
.Add (Ctrl.Tag)
.Item(Ctrl.Tag).Range.Text = Ctrl.Value
.Add Ctrl.Tag, Hdoc.Range(start, start + Len(Ctrl.Value))
End With
End If
Next Ctrl

Unload FrmAdressering
End Sub

Code for putting in the values from the bookmarks into the form

Dim Hdoc As ThisDocument, BM As Bookmark, Frm As FrmAdressering

Set Hdoc = ThisDocument
Load FrmAdressering
Set Frm = FrmAdressering



For Each BM In Hdoc.Bookmarks
BM.Select
Frm.Controls.Item(BM.Name).Value = BM.Range.Text
Next BM

FrmAdressering.Show
End Sub
 
J

Jezebel

The problem is not quite as you see it. If you select the Bookmark range and
delete or replace that range, you also delete the bookmark. Separately, you
don't need to do all the Selecting ...

Dim pRange as Word.Range

If len(Ctrl.Tag) <> 0 And Hdoc.Bookmarks.Exists(Ctrl.Tag) Then

set pRange = Hdoc.Bookmarks(ctrl.tag)
pRange = Ctrl.Value
HDoc.Bookmarks.Add Name:=ctrl.tag, Range:=pRange

End if


But simpler still is to use DocumentProperties. Define dummy values as
document properties and insert DocProperty fields in your document where
currently you have bookmarks. Then your code would be something like

If len(Ctrl.Tag) <> 0 then
HDoc.CustomDocumentProperties(Ctrl.Tag) = Ctrl.Value
End if
 
R

Robert

Jezebel said:
The problem is not quite as you see it. If you select the Bookmark range and
delete or replace that range, you also delete the bookmark. Separately, you
don't need to do all the Selecting ...

Dim pRange as Word.Range

If len(Ctrl.Tag) <> 0 And Hdoc.Bookmarks.Exists(Ctrl.Tag) Then

set pRange = Hdoc.Bookmarks(ctrl.tag)
pRange = Ctrl.Value
HDoc.Bookmarks.Add Name:=ctrl.tag, Range:=pRange

End if


But simpler still is to use DocumentProperties. Define dummy values as
document properties and insert DocProperty fields in your document where
currently you have bookmarks. Then your code would be something like

If len(Ctrl.Tag) <> 0 then
HDoc.CustomDocumentProperties(Ctrl.Tag) = Ctrl.Value
End if
 

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