Can I remove form fields from my document but keep the text?

B

BonnieK

We are experiencing problems using a Word document with form fields. It
seems that you have to click inside the gray box, then use your arrow key to
go to the text you want to change then begin typing ...instead of just being
able to click in the appropriate place in the box and type. Our end users
are frustrated with this, so we would like to simply remove the form fields
from the word documents completely, but leave the text. There are a number
of documents, so we need an easy way to do it. Any ideas?
 
G

Graham Mayor

I assume that you have locked the forms? The fields don't work as intended
unless the form is locked.
In order to remove the form fields, you first need to unlock the form.

The following macro will then remove any form fields in the document.

Sub RemoveFFields()
ActiveDocument.ActiveWindow.View.ShowFieldCodes = True
With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^d FORMTEXT"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.Execute replace:=wdReplaceAll
End With
End With
ActiveDocument.ActiveWindow.View.ShowFieldCodes = False
End Sub

If you are changing the use of the document, you might want to consider
changing the form fields to macrobutton place markers.
eg

Sub ChangeFFields()
ActiveWindow.View.ShowFieldCodes = True
Dim oRng As Range
Dim iFld As Integer
For iFld = ActiveDocument.Fields.Count To 1 Step -1
With ActiveDocument.Fields(iFld)
.Code.Text = replace(.Code.Text, "FORMTEXT", "MACROBUTTON
NoMacro Enter Text")
.Update
End With
Next iFld
With ActiveWindow.View
.FieldShading = wdFieldShadingAlways
.ShowFieldCodes = False
End With
CommandBars("Forms").Visible = False
End Sub

You may even be able to ypdate all the forms at once if you put them in a
folder and run the following macro, though this would only work if none of
the forms were password protected or if they were not all protected with the
same password which you would enter between the quotes at:
ActiveDocument.Unprotect Password:=""

I recommend you test with copies of the documents!!!

Sub BatchFixFields()
Dim myFile As String
Dim PathToUse As String
Dim MyDoc As Document
Dim iFld As Integer
Dim bProtected As Boolean

'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If

With Dialogs(wdDialogCopyFile)
If .Display <> 0 Then
PathToUse = .Directory
Else
MsgBox "Cancelled by User"
Exit Sub
End If
End With
If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
If Left(PathToUse, 1) = Chr(34) Then
PathToUse = Mid(PathToUse, 2, Len(PathToUse) - 2)
End If
myFile = Dir$(PathToUse & "*.doc")
While myFile <> ""
Set MyDoc = Documents.Open(PathToUse & myFile)
ActiveWindow.View.ShowFieldCodes = True
For iFld = ActiveDocument.Fields.Count To 1 Step -1
With ActiveDocument.Fields(iFld)
.Code.Text = replace(.Code.Text, "FORMTEXT", "MACROBUTTON
NoMacro Enter Text")
.Update
End With
Next iFld
With ActiveWindow.View
.FieldShading = wdFieldShadingAlways
.ShowFieldCodes = False
End With
CommandBars("Forms").Visible = False
MyDoc.Close SaveChanges:=wdSaveChanges
myFile = Dir$()
Wend
End Sub

See http://www.gmayor.com/installing_macro.htm


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
B

BonnieK

Thanks for the information. I'll give it a try!

We had to unlock the forms because the first problem we discovered is that
there are links within some of the content and the hyperlink button is greyed
out when the lock is on. It's easy for us to remember to turn off and on the
lock as needed, but our end users need something simple. We are getting a
million support calls! Thanks again!
 
B

BonnieK

OK, I tried the macros and they worked to remove the form field, but they
also removed all the text that resided in the form fields. We just want to
cut the text out of the form fields - remove the fields - and paste the text
back in the right place in the document as plain text....

Thanks!
 
B

BonnieK

Wow - that did work! Now, is there a macro to perform that command on all my
documents in a folder without opening each one? I don't work very much with
macros....
 
G

Graham Mayor

To do this with a macro, the basic code would be -

Sub ChangeFFields()
Dim bProtected As Boolean
'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If
ActiveWindow.View.ShowFieldCodes = True
Dim oRng As Range
Dim iFld As Integer
For iFld = ActiveDocument.Fields.Count To 1 Step -1

With ActiveDocument.Fields(iFld)
If .Type = wdFieldFormTextInput Then
.Unlink
End If
End With

Next iFld
With ActiveWindow.View
.ShowFieldCodes = False
End With
CommandBars("Forms").Visible = False
End Sub

Which will unlink the form fields and retain the content.
As a batch process, with the same provisos as before:

Sub BatchFixFields()
Dim myFile As String
Dim PathToUse As String
Dim MyDoc As Document
Dim iFld As Integer
Dim bProtected As Boolean

'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If
With Dialogs(wdDialogCopyFile)
If .Display <> 0 Then
PathToUse = .Directory
Else
MsgBox "Cancelled by User"
Exit Sub
End If
End With
If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
If Left(PathToUse, 1) = Chr(34) Then
PathToUse = Mid(PathToUse, 2, Len(PathToUse) - 2)
End If
myFile = Dir$(PathToUse & "*.doc")
While myFile <> ""
Set MyDoc = Documents.Open(PathToUse & myFile)
ActiveWindow.View.ShowFieldCodes = True
For iFld = ActiveDocument.Fields.Count To 1 Step -1

With ActiveDocument.Fields(iFld)
If .Type = wdFieldFormTextInput Then
.Unlink
End If
End With

Next iFld
CommandBars("Forms").Visible = False
ActiveWindow.View.ShowFieldCodes = False
End With
MyDoc.Close SaveChanges:=wdSaveChanges
myFile = Dir$()
Wend
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
B

BonnieK

I am soooo close to getting this thanks to both of you!

Graham, when I run the macro, I get a compile error: End With without With .

It highlights the last "End With".

Just to make sure I'm running it right...I put all the documents in the same
folder on my server, then I opened one of them in Word and clicked the button
on the toolbar to run the macro.

Thanks so much!
 
G

Graham Mayor

Oops!
Sorry, my fault, I made some changes in the e-mail editor and didn't bother
to check. Scrub the last version and use the following. You don't need any
document open when you run the macro, as it will close any open document as
part of the process.

Sub BatchFixFields()
Dim myFile As String
Dim PathToUse As String
Dim MyDoc As Document
Dim iFld As Integer
Dim bProtected As Boolean

With Dialogs(wdDialogCopyFile)
If .Display <> 0 Then
PathToUse = .Directory
Else
MsgBox "Cancelled by User"
Exit Sub
End If
End With

If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If

If Left(PathToUse, 1) = Chr(34) Then
PathToUse = Mid(PathToUse, 2, Len(PathToUse) - 2)
End If

myFile = Dir$(PathToUse & "*.doc")

While myFile <> ""
Set MyDoc = Documents.Open(PathToUse & myFile)
'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If

ActiveWindow.View.ShowFieldCodes = True
For iFld = ActiveDocument.Fields.Count To 1 Step -1

With ActiveDocument.Fields(iFld)
If .Type = wdFieldFormTextInput Then
.Unlink
End If
End With

Next iFld
CommandBars("Forms").Visible = False
ActiveWindow.View.ShowFieldCodes = False
MyDoc.Close SaveChanges:=wdSaveChanges
myFile = Dir$()
Wend
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
B

BonnieK

OK - I'll try this one. How does the macro know which folder my documents
are stored in if I don't have one open or direct it to the right folder in
some way?
 
B

BonnieK

Now when I run the macro, I get the Copy dialog box. I browse to the folder
where my files are stored and click Open. The dialog box then closes, but
nothing has happened....not sure what I'm doing wrong!
 
G

Graham Mayor

The macro uses the Copy dialog to select the folder with the assembly of
files. When you click Open, the macro looks in that field for Word '.doc'
files, which it opens in turn, unprotects the form (provided it has no
password - or you'll get an error) then unlinks any form fields in the
document, which it saves and closes. The macro should be seen running in
that it quickly opens and closes each file. I take it you do have *.doc
format files in that folder?

I will be out for most of the day, but if you want to send me a sample
document via the link on my web site, I will see if I can locate the problem
when I return.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
B

BonnieK

Aha - that's the problem then. All the files are saved as a web page. They
have either the .htm or .mht extension.
 
G

Graham Mayor

If you change the line
myFile = Dir$(PathToUse & "*.doc")
to
myFile = Dir$(PathToUse & "*.htm")
it will open htm files instead of doc.


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

Glad we got there in the end :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top