View Single Post
 
Old 04-10-2012, 08:54 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi Ian,

Try:
Code:
Sub AddFormFields()
Dim pState As Boolean, Pwd As String, StrTmp As String, FFld As FormField
With ActiveDocument
  pState = False
  If .ProtectionType <> wdNoProtection Then
    Pwd = InputBox("Please enter the Password", "Password")
    pState = True
    .Unprotect Pwd
  End If
  With .Range
    With .Find
      .ClearFormatting
      .Text = "Student #: [0-9]@>"
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindStop
      .Format = True
      .MatchCase = False
      .MatchWholeWord = False
      .MatchWildcards = True
      .MatchSoundsLike = False
      .MatchAllWordForms = False
      .Execute
    End With
    Do While .Find.Found
      StrTmp = .Duplicate.Words.Last.Text
      Set FFld = ActiveDocument.FormFields.Add(Range:=.Duplicate.Words.Last, _
                Type:=wdFieldFormTextInput)
      FFld.Result = StrTmp
      .Start = FFld.Range.End
      .Find.Execute
    Loop
  End With
  If pState = True Then .Protect Type:=wdAllowOnlyFormFields, Noreset:=True, _
              Password:=Pwd
  pState = False
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote