Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / May 2008

Tip: Looking for answers? Try searching our database.

Find/Replace plain text with a text form field

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
CR Geissler - 22 May 2008 18:11 GMT
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?

Thanks,
Colin
Jean-Guy Marcil - 22 May 2008 19:31 GMT
> Hi,
>
[quoted text clipped - 9 lines]
> 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
CR Geissler - 22 May 2008 20:42 GMT
Wow!

Thanks, that is great.  This is exactly what I was looking for.

Cheers,
Colin

>> Hi,
>>
[quoted text clipped - 73 lines]
>
> End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.