View Single Post
  #11   Report Post  
Posted to microsoft.public.word.docmanagement
BonnieK BonnieK is offline
external usenet poster
 
Posts: 9
Default Can I remove form fields from my document but keep the text?

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?

"Graham Mayor" wrote:

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org


BonnieK wrote:
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!

"Graham Mayor" wrote:

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 befo

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org


BonnieK wrote:
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....

"Suzanne S. Barnhill" wrote:

In that case, you can unlink the fields by selecting the document
(Ctrl+A) and pressing Ctrl+Shift+F9, but see also
http://word.mvps.org/FAQs/TblsFldsFms/HLinksInForms.htm

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA
Word MVP FAQ site: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

"BonnieK" wrote in message
...
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!

"Graham Mayor" wrote:

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