• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 450
  • Last Modified:

Hide Unused Fields and Buttons - Word 2007

Hi guys,

A later rendition on an earlier problem I had;

I would like to create a button on the quick access toolbar that will run through an entire document, find the unused form fields (caption text and grey highlight remains), and hide all of these fields, along with a number of command buttons too.

Thanks in advance!

Milos
0
RThreeOne
Asked:
RThreeOne
  • 7
  • 6
1 Solution
 
GrahamSkanCommented:
Can you confirm that you are talking about Form Fields, and not Content Controls?  Neither type of control has a caption text property, but Content controls do have a PlaceholderText property. Perhaps that is what you mean.

Actually, I think that there are two errors in Word design. Firstly, there should have been a setting to stop PlaceholderText being printed. Secondly, it shouldn't be possible to put ActiveX controls on to a document for this very reason. They are incongruous in a printed document.
0
 
GrahamSkanCommented:
Thinking about it, as I recall, you are using form fields. This will delete all empty form fields in an unprotected document.
Sub DelFormFields()
Dim ffld As FormField
Dim bDeleted As Boolean

Do
    bDeleted = False
    For Each ffld In ActiveDocument
        If ffld.Type = wdFieldFormTextInput Then
            If ffld.Result = "" Then
                ffld.Delete
                bDeleted = True
            End If
        End If
    Next ffld
Loop While bDeleted
End Sub

Open in new window

If, by captions, you mean some text alongside the fields, we would need some way to locate the exact text. Is it a rule that the caption and the field are always in the same paragraph, and nothing else is?
0
 
RThreeOneAuthor Commented:
I am using both form fields and content controls. There are two command buttons which are currently successfully linked to delete as a bookmark " " to a button in the quick access toolbar. other than that, there is one drop down menu, and one calendar date picker in the document, the rest of the items are form fields.

I guess what I meant by caption is the initial text displayed in the greyed background of a form field. The drop down box and calendar picker however do have captions. I don't think these are an issue however as the document will be populated by the user and these components are necessary, thus will not be left untouched. It would only be some of the other form fields which may be left unused in the populating of the document.

Currently, your solution faults on the following line;

For Each ffld In ActiveDocument

Thanks again!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
GrahamSkanCommented:
Sorry, that should have been;
For Each ffld In ActiveDocument.FormFields

I think that we might need to see a sample document.
0
 
RThreeOneAuthor Commented:
Sure. I'll try that and/or give a sample document in a couple days when I go back to work.

Cheers,

Milos
0
 
RThreeOneAuthor Commented:
I have attached a sample document containing the main functions I'm after.

In this case, I am wanting to erase the following and/or more bodies of text should they be left blank by the document's user;

[Street]
[Suburb State Postcode]

Click here to choose proposal type

[Customer site name]

[Insert other details/brief inclusions of proposal etc]

As you can see, some are ActiveX while others are form fields.

Thanks again
Sample-Doc.dotx
0
 
GrahamSkanCommented:
You can delete unfilled content controls in a similar way, but you would have to check that the text placeholder text was still visible,
Sub DeleteUnusedCCs()
    Dim cc As ContentControl
    For Each cc In ActiveDocument.ContentControls
        If cc.ShowingPlaceholderText Then
            cc.Delete
        End If
    Next cc
End Sub

Open in new window

0
 
GrahamSkanCommented:
You can delete the buttons with this code
Sub DelButtons()
Dim ilsh As InlineShape

For Each ilsh In ActiveDocument.InlineShapes
    If ilsh.Type = wdInlineShapeOLEControlObject Then
        ilsh.Delete
    End If
Next ilsh

End Sub

Open in new window

0
 
RThreeOneAuthor Commented:
Thanks those work great, although I will have to change the Legacy forms into content controls, but thats fine. My only other question is, is it possible to shift the text up too?

And on that note, is it possible to delete and shift regulr text up based on the selection from a dropdown box? At the moment the dropdown box only input text elsewhere.

EDIT: To elaborate, the dropdown box;

"Choice 1" - Somebookmarks are updated with "Choice 1" text but nothing more.
"Choice 2" - The same bookmarks are updated with "Choice 2", as well as, a a seperate body of text elswhere in the document is deleted and the remaining text is shifted up

Thanks..
0
 
GrahamSkanCommented:
If you want, you can delete the whole paragraph, instead of just the control. This shows how to delete the paragraph for some form fields.
Sub DelFormFields()
Dim ffld As FormField
Dim bDeleted As Boolean 'deletion disturbs the collection and the For Each walk-through, so run loop until nothing is deleted

Do
    bDeleted = False
    For Each ffld In ActiveDocument
        If ffld.Type = wdFieldFormTextInput Then
            If ffld.Result = "" Then
                Select Case ffld.Name
                    Case "Text1", "Text3"
                        ffld.Range.Paragraphs.First.Range.Delete
                    Case Else
                        ffld.Delete
                End Select
                bDeleted = True
            End If
        End If
    Next ffld
Loop While bDeleted
End Sub

Open in new window

0
 
RThreeOneAuthor Commented:
Thanks again Graham, however i must say that that last macro does nothing from what I can see.

So far I have the document deleting the commandbuttons and one date picker cc whoeevr that is all.
0
 
GrahamSkanCommented:
You haven't mentioned it, so it might be because you haven't modified the code to list the relevant form fields by name. Fairly obviously you need to put your choice of fields in the Case line of the Select block.

(In line with the philosophy of this forum, I am only showing how it is done, not actually doing it for you)
0
 
RThreeOneAuthor Commented:
Yes sorry, I realised that after I typed it and forgot to edit the comment. I had not however had a chance to try it out.

I'll try it out next week, and thanks a lot for persisting!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now