(Msg. 1) Posted: Thu May 22, 2008 11:11 am
Post subject: Find/Replace plain text with a text form field Archived from groups: microsoft>public>word>vba>userforms (more info?)
Hi,
I have a system that can create Word documents where all the document
content comes out of a database.
I want to find/replace specific text in these Word documents with a form
field, for example, I have some text called "first_name" but want to replace
it with a text form field. Or I have some text "yes/no" that I want to
replace with a check box form field.
Can this be done?
If so, can it be done with a macro, or does it need something more heavy
duty?
(Msg. 2) Posted: Thu May 22, 2008 11:31 am
Post subject: RE: Find/Replace plain text with a text form field [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
"CR Geissler" wrote:
> Hi,
>
> I have a system that can create Word documents where all the document
> content comes out of a database.
>
> I want to find/replace specific text in these Word documents with a form
> field, for example, I have some text called "first_name" but want to replace
> it with a text form field. Or I have some text "yes/no" that I want to
> replace with a check box form field.
>
> Can this be done?
> If so, can it be done with a macro, or does it need something more heavy
> duty?
Yes, it can be done, and no, it is not heavy duty!
Here is my take on it:
Option Explicit
Const textFF As String = "First_Name"
Const checkFF As String = "Yes/No"
Sub FindText()
Dim rngFind As Range
Dim i As Long
i = 1
Set rngFind = ActiveDocument.Range
With rngFind.Find
.Text = textFF
Do While .Execute
AddFormField textFF, rngFind, textFF & i, "Enter your name"
i = i + 1
rngFind.Start = rngFind.End
rngFind.End = ActiveDocument.Range.End
Loop
End With
i = 1
Set rngFind = ActiveDocument.Range
With rngFind.Find
.Text = checkFF
Do While .Execute
AddFormField checkFF, rngFind, "Yes_No" & i
i = i + 1
rngFind.Start = rngFind.End
rngFind.End = ActiveDocument.Range.End
Loop
End With
End Sub
Sub AddFormField(strType As String, rngFF As Range, _
strName As String, Optional strDefault As String)
Dim frmfldReplace As FormField
Select Case strType
Case checkFF
Set frmfldReplace = rngFF.FormFields.Add(rngFF, wdFieldFormCheckBox)
With frmfldReplace
.Name = strName
End With
Case textFF
Set frmfldReplace = rngFF.FormFields.Add(rngFF, wdFieldFormTextInput)
With frmfldReplace
.Name = strName
.TextInput.Default = strDefault
.Result = .TextInput.Default
End With
End Select
(Msg. 3) Posted: Thu May 22, 2008 1:42 pm
Post subject: Re: Find/Replace plain text with a text form field [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
Wow!
Thanks, that is great. This is exactly what I was looking for.
Cheers,
Colin
"Jean-Guy Marcil" <JeanGuyMarcil.TakeThisOut@discussions.microsoft.com> wrote in message
news:6562F820-3F90-4F41-8992-6D247F49A34C@microsoft.com...
> "CR Geissler" wrote:
>
>> Hi,
>>
>> I have a system that can create Word documents where all the document
>> content comes out of a database.
>>
>> I want to find/replace specific text in these Word documents with a form
>> field, for example, I have some text called "first_name" but want to
>> replace
>> it with a text form field. Or I have some text "yes/no" that I want to
>> replace with a check box form field.
>>
>> Can this be done?
>> If so, can it be done with a macro, or does it need something more heavy
>> duty?
>
> Yes, it can be done, and no, it is not heavy duty!
> Here is my take on it:
>
>
> Option Explicit
>
> Const textFF As String = "First_Name"
> Const checkFF As String = "Yes/No"
>
> Sub FindText()
>
> Dim rngFind As Range
> Dim i As Long
>
> i = 1
> Set rngFind = ActiveDocument.Range
> With rngFind.Find
> .Text = textFF
> Do While .Execute
> AddFormField textFF, rngFind, textFF & i, "Enter your name"
> i = i + 1
> rngFind.Start = rngFind.End
> rngFind.End = ActiveDocument.Range.End
> Loop
> End With
>
> i = 1
> Set rngFind = ActiveDocument.Range
> With rngFind.Find
> .Text = checkFF
> Do While .Execute
> AddFormField checkFF, rngFind, "Yes_No" & i
> i = i + 1
> rngFind.Start = rngFind.End
> rngFind.End = ActiveDocument.Range.End
> Loop
> End With
>
> End Sub
>
> Sub AddFormField(strType As String, rngFF As Range, _
> strName As String, Optional strDefault As String)
>
> Dim frmfldReplace As FormField
>
> Select Case strType
> Case checkFF
> Set frmfldReplace = rngFF.FormFields.Add(rngFF,
> wdFieldFormCheckBox)
> With frmfldReplace
> .Name = strName
> End With
> Case textFF
> Set frmfldReplace = rngFF.FormFields.Add(rngFF,
> wdFieldFormTextInput)
> With frmfldReplace
> .Name = strName
> .TextInput.Default = strDefault
> .Result = .TextInput.Default
> End With
> End Select
>
> End Sub
>
All times are: Eastern Time (US & Canada) (change)
Page 1 of 1
You can post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum