Group:  Microsoft Word ยป microsoft.public.word.vba.customization
Thread: How to add pre-set ( a variable?) text into txtBox

Geek News

How to add pre-set ( a variable?) text into txtBox
David <me[ at ]here.com> 11/12/2008 12:44:02 AM
Word 2003.
I have a protected document with three different text boxes in it.
I unprotect the document to add text to a text box. Then protect
again.
This works fine.

I now want to put some pre-set phrases at the end of some of my text
already typed in the text box. The pre-set text would come from chosen
phrases from another user form and be placed at the cursor position in
the text box.

It either does nothing, or inserts it into the (what was) protected
part of the document instead of into the text box.
Thank you for any assistance with the right direction to go.
Anthony



Re: How to add pre-set ( a variable?) text into txtBox
"Doug Robbins - Word MVP" <dkr[ at ]REMOVECAPSmvps.org> 11/12/2008 7:27:07 PM
Show us the code. By text boxes, do you mean text box form fields?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"David" <me[ at ]here.com> wrote in message
news:kq8kh410do1a495j8qbnbgg5khm0gnpdoe[ at ]4ax.com...
[Quoted Text]
> Word 2003.
> I have a protected document with three different text boxes in it.
> I unprotect the document to add text to a text box. Then protect
> again.
> This works fine.
>
> I now want to put some pre-set phrases at the end of some of my text
> already typed in the text box. The pre-set text would come from chosen
> phrases from another user form and be placed at the cursor position in
> the text box.
>
> It either does nothing, or inserts it into the (what was) protected
> part of the document instead of into the text box.
> Thank you for any assistance with the right direction to go.
> Anthony
>
>
>


Re: How to add pre-set ( a variable?) text into txtBox
David <me[ at ]here.com> 11/13/2008 1:46:24 AM
On Thu, 13 Nov 2008 05:27:07 +1000, "Doug Robbins - Word MVP"
<dkr[ at ]REMOVECAPSmvps.org> wrote:

[Quoted Text]
>Show us the code. By text boxes, do you mean text box form fields?
Thank you for replying.
How do I send the code to you?
and do I send the template that it is in?
Thanks

Re: How to add pre-set ( a variable?) text into txtBox
"Doug Robbins - Word MVP" <dkr[ at ]REMOVECAPSmvps.org> 11/13/2008 5:11:06 AM
Copy and paste the code into the body of a message that you post to the
newsgroup.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"David" <me[ at ]here.com> wrote in message
news:5l1nh4hkd456u7a8evs65e04vvc5hcakbs[ at ]4ax.com...
[Quoted Text]
> On Thu, 13 Nov 2008 05:27:07 +1000, "Doug Robbins - Word MVP"
> <dkr[ at ]REMOVECAPSmvps.org> wrote:
>
>>Show us the code. By text boxes, do you mean text box form fields?
> Thank you for replying.
> How do I send the code to you?
> and do I send the template that it is in?
> Thanks
>


Re: How to add pre-set ( a variable?) text into txtBox
David <me[ at ]here.com> 11/13/2008 8:04:32 PM
Thanks for the reply.
Description:
I have a protected document.
The user needs to add comments into three separate text boxes in this
document.
While adding these comments, I have another userform dialog box
(enabled from the userform that comes up for the three text boxes)
that is supposed to enable them to choose a phrase to add to their
comments, at the point where the cursor is at the time, to save typing
repetitive remarks. These phrases come from chosen radio controls.
This works OK if I run it on a new blank document. It types the phrase
into the blank no problem.
However, nothing happens when I run it in the protected document.
I am unprotecting the text boxes for this to happen and protecting
again at the end.

There is a box for the user to add a name also.
opt1, 2 and 3 are the names of the three radio choices of phrase.
Thanks for your help - I hope you can follow my rambling description!

Here's the code...
Private Sub cmdAddPhrase_Click()
AddPhrase
End Sub

Private Sub AddPhrase()

Dim rng As Range 'for text insertion
Dim sText As String 'text to insert


'check to see if text box is empty
' if so, warn user and exit
If txtName.Text = "" Then
MsgBox "Please fill in your name"

Exit Sub
End If


'Compose text based on choice of option button
If opt1.Value = True Then

sText = txtName & " has been a hard worker this year! (opt1) "

ElseIf opt2.Value = True Then
sText = txtName & " has had a reasonable year. (opt2)"

ElseIf opt3.Value = True Then
sText = txtName & " hasn't really concentrated much this year. (opt3)"

Else
'no option chosen. Warn the user and exit
MsgBox "Please choose a phrase"

Exit Sub
End If

'Insert the text.
Set rng = ActiveDocument.Range
rng.Collapse wdCollapseEnd
rng.Text = sText

'Dim myText As String
'
'myText = "Line 2: It Worked!"
'Selection.InsertParagraph
'Selection.InsertParagraph
'
'
'Selection.TypeText Text:="Line 1: This text is this text - Yeah!"
'With Selection
' .Collapse Direction:=wdCollapseStart
' .InsertParagraph
' .Collapse Direction:=wdCollapseEnd
'End With
'
'Selection.InsertParagraph
'Selection.TypeText Text:=myText


Unload Me

End Sub




Private Sub cmdCancel_Click()
Unload Me
End Sub

Private Sub UserForm_Click()

End Sub


On Thu, 13 Nov 2008 15:11:06 +1000, "Doug Robbins - Word MVP"
<dkr[ at ]REMOVECAPSmvps.org> wrote:

[Quoted Text]
>Copy and paste the code into the body of a message that you post to the
>newsgroup.

Re: How to add pre-set ( a variable?) text into txtBox
"Doug Robbins - Word MVP" <dkr[ at ]REMOVECAPSmvps.org> 11/14/2008 1:39:33 AM
Instead of

[Quoted Text]
> 'Insert the text.
> Set rng = ActiveDocument.Range
> rng.Collapse wdCollapseEnd
> rng.Text = sText

use

'Insert the text.
Dim bm as String
bm = Selection.Bookmarks(1).Name
ActiveDocument.FormFields(bm).Result = sText

You should not then need to unprotect/re-protect the document.


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"David" <me[ at ]here.com> wrote in message
news:m01ph49mcluc03adu6rv28892loj7b2i1q[ at ]4ax.com...
> Thanks for the reply.
> Description:
> I have a protected document.
> The user needs to add comments into three separate text boxes in this
> document.
> While adding these comments, I have another userform dialog box
> (enabled from the userform that comes up for the three text boxes)
> that is supposed to enable them to choose a phrase to add to their
> comments, at the point where the cursor is at the time, to save typing
> repetitive remarks. These phrases come from chosen radio controls.
> This works OK if I run it on a new blank document. It types the phrase
> into the blank no problem.
> However, nothing happens when I run it in the protected document.
> I am unprotecting the text boxes for this to happen and protecting
> again at the end.
>
> There is a box for the user to add a name also.
> opt1, 2 and 3 are the names of the three radio choices of phrase.
> Thanks for your help - I hope you can follow my rambling description!
>
> Here's the code...
> Private Sub cmdAddPhrase_Click()
> AddPhrase
> End Sub
>
> Private Sub AddPhrase()
>
> Dim rng As Range 'for text insertion
> Dim sText As String 'text to insert
>
>
> 'check to see if text box is empty
> ' if so, warn user and exit
> If txtName.Text = "" Then
> MsgBox "Please fill in your name"
>
> Exit Sub
> End If
>
>
> 'Compose text based on choice of option button
> If opt1.Value = True Then
>
> sText = txtName & " has been a hard worker this year! (opt1) "
>
> ElseIf opt2.Value = True Then
> sText = txtName & " has had a reasonable year. (opt2)"
>
> ElseIf opt3.Value = True Then
> sText = txtName & " hasn't really concentrated much this year. (opt3)"
>
> Else
> 'no option chosen. Warn the user and exit
> MsgBox "Please choose a phrase"
>
> Exit Sub
> End If
>
> 'Insert the text.
> Set rng = ActiveDocument.Range
> rng.Collapse wdCollapseEnd
> rng.Text = sText
>
> 'Dim myText As String
> '
> 'myText = "Line 2: It Worked!"
> 'Selection.InsertParagraph
> 'Selection.InsertParagraph
> '
> '
> 'Selection.TypeText Text:="Line 1: This text is this text - Yeah!"
> 'With Selection
> ' .Collapse Direction:=wdCollapseStart
> ' .InsertParagraph
> ' .Collapse Direction:=wdCollapseEnd
> 'End With
> '
> 'Selection.InsertParagraph
> 'Selection.TypeText Text:=myText
>
>
> Unload Me
>
> End Sub
>
>
>
>
> Private Sub cmdCancel_Click()
> Unload Me
> End Sub
>
> Private Sub UserForm_Click()
>
> End Sub
>
>
> On Thu, 13 Nov 2008 15:11:06 +1000, "Doug Robbins - Word MVP"
> <dkr[ at ]REMOVECAPSmvps.org> wrote:
>
>>Copy and paste the code into the body of a message that you post to the
>>newsgroup.
>


Re: How to add pre-set ( a variable?) text into txtBox
David <me[ at ]here.com> 11/16/2008 7:03:03 PM
Thank you very much Doug.
I'll try that.
Anthony

On Fri, 14 Nov 2008 11:39:33 +1000, "Doug Robbins - Word MVP"
<dkr[ at ]REMOVECAPSmvps.org> wrote:

[Quoted Text]
>Instead of
>
>> 'Insert the text.
>> Set rng = ActiveDocument.Range
>> rng.Collapse wdCollapseEnd
>> rng.Text = sText
>
>use
>
>'Insert the text.
>Dim bm as String
>bm = Selection.Bookmarks(1).Name
>ActiveDocument.FormFields(bm).Result = sText
>
>You should not then need to unprotect/re-protect the document.

Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net