Greg
Fri Oct 12 04:04:55 PDT 2007
I don't really know. Something to do with txtFieldName.Text referring to an
object that hasn't been properly referenced I guess.
Assuming that the bookmark names corresponde with the textfield name then
this might work:
Private Sub CommandButton1_Click()
Dim oCtr As Control
Dim oRng As Range
Dim oBMs As Bookmarks
Set oBMs = ActiveDocument.Bookmarks
For Each oCtr In Me.Controls
On Error GoTo Err_Handler
Set oRng = oBMs(oCtr.Name).Range
oRng.Text = Me.Controls(oCtr.Name).Value
oBMs.Add oCtr.Name, oRng
Err_Resume:
Next
Unload Me
Exit Sub
Err_Handler:
If Err.Number = 5941 Then
Resume Err_Resume
Else
MsgBox Err.Number
End If
End Sub
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
Martin Cameron wrote:
> Hi Greg
>
> Thanks for your speedy reply. Your code is direct and to he point,
> but I wonder if you could help me a little more specifically with the
> following questions:
> 1. When I MsgBox(txtFieldName) the correct name appears but the
> invalid qualifier still appears for the name of the textbox when
> tryin to assign its vale to the bookmark.
> 2. have to keep the namesas per the form because I initialize the
> form from an ini file that has the fieldname as the key
>
> "Greg Maxey" wrote:
>
>> If I understand your objective, you want to populate 20 variously
>> named bookmarks with the content of 20 textboxes in a userform.
>> Assuming that the texboxes are TextBox1, TextBox2, etc. then:
>>
>> Private Sub CommandButton1_Click()
>> Dim i As Long
>> Dim Count As Long
>> Dim pBM As String
>> Dim oRng As Range
>> Count = ActiveDocument.Bookmarks.Count
>> For i = 1 To Count
>> pBM = ActiveDocument.Bookmarks(i).Name
>> Set oRng = ActiveDocument.Bookmarks(i).Range
>> oRng.Text = Me.Controls("TextBox" & i).Text
>> ActiveDocument.Bookmarks.Add pBM, oRng
>> Next i
>>
>> End Sub
>>
>>
>> --
>> Greg Maxey/Word MVP
>> See:
>>
http://gregmaxey.mvps.org/word_tips.htm
>> For some helpful tips using Word.
>>
>>
>> Martin Cameron wrote:
>>> I have a word document with approx 20 bookmarks in it. I have a
>>> UserForm that populates those bookmarks. I would like the code to be
>>> generic but am having a problem - the following code returns an
>>> error "Invalid Qualifier" with reference to the textBox field name.
>>> I would really appreciate somehelp on this
>>>
>>> <code>
>>> Dim Bmk() As String
>>> Dim y As Integer, J As Integer
>>> Dim txtFieldName As String
>>> y = ActiveDocument.Bookmarks.Count
>>> ReDim iniarray(y)
>>> For J = 1 To y
>>> Bmk(J) = ActiveDocument.Bookmarks(J).Name
>>> Trim(txtFieldName) = CStr(Bmk(J))
>>> ActiveDocument.Bookmarks(Bmk(J)).Range.Text = txtFieldName.Text
>>> Next J
>>> </code>