Do not print blank form fields

J

Jack F

I have made Word documents for test reports. In conclusion there are free
text and drop down form fields. How do I remove spacing on the printed
verison from form fields that are left blank?
 
G

Graham Mayor

You need a macro

Dim i As Integer
Dim bProtected As Boolean
Dim bHidden As Boolean
Dim sPassword As String
sPassword = ""
bHidden = ActiveWindow.View.ShowHiddenText
ActiveWindow.View.ShowHiddenText = False
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=sPassword
End If
With ActiveDocument
For i = .FormFields.Count To 1 Step -1
If Len(.FormFields(i).Result) = 0 Then
'Hide the field
.FormFields(i).Range.Font.Hidden = True
'or Hide the paragraph containing the field
'.FormFields(i).Range.Paragraphs(1) _
.Range.Font.Hidden = True
End If
Next i
End With
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, _
Password:=sPassword
End If
ActiveWindow.View.ShowHiddenText = bHidden

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

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

My web site www.gmayor.com

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

Graham Mayor

Oops! :blush:( Forgot to include the command to print the document with the
fields hidden

Dim i As Integer
Dim bProtected As Boolean
Dim bHidden As Boolean
Dim sPassword As String
sPassword = ""
bHidden = ActiveWindow.View.ShowHiddenText
ActiveWindow.View.ShowHiddenText = False
With ActiveDocument
If .ProtectionType <> wdNoProtection Then
bProtected = True
.Unprotect Password:=sPassword
End If
For i = .FormFields.Count To 1 Step -1
If Len(.FormFields(i).Result) = 0 Then
'Hide the field
.FormFields(i).Range.Font.Hidden = True
'or Hide the paragraph containing the field
'.FormFields(i).Range.Paragraphs(1) _
.Range.Font.Hidden = True
End If
Next i
If bProtected = True Then
.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=sPassword
End If
.PrintOut
End With
ActiveWindow.View.ShowHiddenText = bHidden

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

My web site www.gmayor.com

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

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